opencv+mediapipe實(shí)現(xiàn)人臉檢測及攝像頭實(shí)時(shí)示例
單張人臉關(guān)鍵點(diǎn)檢測
定義可視化圖像函數(shù)
導(dǎo)入三維人臉關(guān)鍵點(diǎn)檢測模型
導(dǎo)入可視化函數(shù)和可視化樣式
讀取圖像
將圖像模型輸入,獲取預(yù)測結(jié)果
BGR轉(zhuǎn)RGB
將RGB圖像輸入模型,獲取預(yù)測結(jié)果
預(yù)測人人臉個(gè)數(shù)
可視化人臉關(guān)鍵點(diǎn)檢測效果
繪制人來臉和重點(diǎn)區(qū)域輪廓線,返回annotated_image
繪制人臉輪廓、眼睫毛、眼眶、嘴唇
在三維坐標(biāo)中分別可視化人臉網(wǎng)格、輪廓、瞳孔
import cv2 as cv
import mediapipe as mp
from tqdm import tqdm
import time
import matplotlib.pyplot as plt
# 定義可視化圖像函數(shù)
def look_img(img):
img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB)
plt.imshow(img_RGB)
plt.show()
# 導(dǎo)入三維人臉關(guān)鍵點(diǎn)檢測模型
mp_face_mesh=mp.solutions.face_mesh
# help(mp_face_mesh.FaceMesh)
model=mp_face_mesh.FaceMesh(
static_image_mode=True,#TRUE:靜態(tài)圖片/False:攝像頭實(shí)時(shí)讀取
refine_landmarks=True,#使用Attention Mesh模型
min_detection_confidence=0.5, #置信度閾值,越接近1越準(zhǔn)
min_tracking_confidence=0.5,#追蹤閾值
)
# 導(dǎo)入可視化函數(shù)和可視化樣式
mp_drawing=mp.solutions.drawing_utils
mp_drawing_styles=mp.solutions.drawing_styles
# 讀取圖像
img=cv.imread('img.png')
# look_img(img)
# 將圖像模型輸入,獲取預(yù)測結(jié)果
# BGR轉(zhuǎn)RGB
img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB)
# 將RGB圖像輸入模型,獲取預(yù)測結(jié)果
results=model.process(img_RGB)
# 預(yù)測人人臉個(gè)數(shù)
len(results.multi_face_landmarks)
print(len(results.multi_face_landmarks))
# 結(jié)果:1
# 可視化人臉關(guān)鍵點(diǎn)檢測效果
# 繪制人來臉和重點(diǎn)區(qū)域輪廓線,返回annotated_image
annotated_image=img.copy()
if results.multi_face_landmarks: #如果檢測出人臉
for face_landmarks in results.multi_face_landmarks:#遍歷每一張臉
#繪制人臉網(wǎng)格
mp_drawing.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_TESSELATION,
#landmark_drawing_spec為關(guān)鍵點(diǎn)可視化樣式,None為默認(rèn)樣式(不顯示關(guān)鍵點(diǎn))
# landmark_drawing_spec=mp_drawing_styles.DrawingSpec(thickness=1,circle_radius=2,color=[66,77,229]),
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style()
)
#繪制人臉輪廓、眼睫毛、眼眶、嘴唇
mp_drawing.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_CONTOURS,
# landmark_drawing_spec為關(guān)鍵點(diǎn)可視化樣式,None為默認(rèn)樣式(不顯示關(guān)鍵點(diǎn))
# landmark_drawing_spec=mp_drawing_styles.DrawingSpec(thickness=1,circle_radius=2,color=[66,77,229]),
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style()
)
#繪制瞳孔區(qū)域
mp_drawing.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_IRISES,
# landmark_drawing_spec為關(guān)鍵點(diǎn)可視化樣式,None為默認(rèn)樣式(不顯示關(guān)鍵點(diǎn))
landmark_drawing_spec=mp_drawing_styles.DrawingSpec(thickness=1,circle_radius=2,color=[128,256,229]),
# landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style()
)
cv.imwrite('test.jpg',annotated_image)
look_img(annotated_image)
# 在三維坐標(biāo)中分別可視化人臉網(wǎng)格、輪廓、瞳孔
mp_drawing.plot_landmarks(results.multi_face_landmarks[0],mp_face_mesh.FACEMESH_TESSELATION)
mp_drawing.plot_landmarks(results.multi_face_landmarks[0],mp_face_mesh.FACEMESH_CONTOURS)
mp_drawing.plot_landmarks(results.multi_face_landmarks[0],mp_face_mesh.FACEMESH_IRISES)



單張圖像人臉檢測
可以通過調(diào)用open3d實(shí)現(xiàn)3d模型建立,部分代碼與上面類似
import cv2 as cv
import mediapipe as mp
import numpy as np
from tqdm import tqdm
import time
import matplotlib.pyplot as plt
# 定義可視化圖像函數(shù)
def look_img(img):
img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB)
plt.imshow(img_RGB)
plt.show()
# 導(dǎo)入三維人臉關(guān)鍵點(diǎn)檢測模型
mp_face_mesh=mp.solutions.face_mesh
# help(mp_face_mesh.FaceMesh)
model=mp_face_mesh.FaceMesh(
static_image_mode=True,#TRUE:靜態(tài)圖片/False:攝像頭實(shí)時(shí)讀取
refine_landmarks=True,#使用Attention Mesh模型
max_num_faces=40,
min_detection_confidence=0.2, #置信度閾值,越接近1越準(zhǔn)
min_tracking_confidence=0.5,#追蹤閾值
)
# 導(dǎo)入可視化函數(shù)和可視化樣式
mp_drawing=mp.solutions.drawing_utils
# mp_drawing_styles=mp.solutions.drawing_styles
draw_spec=mp_drawing.DrawingSpec(thickness=2,circle_radius=1,color=[223,155,6])
# 讀取圖像
img=cv.imread('../人臉三維關(guān)鍵點(diǎn)檢測/dkx.jpg')
# width=img1.shape[1]
# height=img1.shape[0]
# img=cv.resize(img1,(width*10,height*10))
# look_img(img)
# 將圖像模型輸入,獲取預(yù)測結(jié)果
# BGR轉(zhuǎn)RGB
img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB)
# 將RGB圖像輸入模型,獲取預(yù)測結(jié)果
results=model.process(img_RGB)
# # 預(yù)測人人臉個(gè)數(shù)
# len(results.multi_face_landmarks)
#
# print(len(results.multi_face_landmarks))
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
mp_drawing.draw_landmarks(
image=img,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_CONTOURS,
landmark_drawing_spec=draw_spec,
connection_drawing_spec=draw_spec
)
else:
print('未檢測出人臉')
look_img(img)
mp_drawing.plot_landmarks(results.multi_face_landmarks[0],mp_face_mesh.FACEMESH_TESSELATION)
mp_drawing.plot_landmarks(results.multi_face_landmarks[1],mp_face_mesh.FACEMESH_CONTOURS)
mp_drawing.plot_landmarks(results.multi_face_landmarks[1],mp_face_mesh.FACEMESH_IRISES)
# 交互式三維可視化
coords=np.array(results.multi_face_landmarks[0].landmark)
# print(len(coords))
# print(coords)
def get_x(each):
return each.x
def get_y(each):
return each.y
def get_z(each):
return each.z
# 分別獲取所有關(guān)鍵點(diǎn)的XYZ坐標(biāo)
points_x=np.array(list(map(get_x,coords)))
points_y=np.array(list(map(get_y,coords)))
points_z=np.array(list(map(get_z,coords)))
# 將三個(gè)方向的坐標(biāo)合并
points=np.vstack((points_x,points_y,points_z)).T
print(points.shape)
import open3d
point_cloud=open3d.geometry.PointCloud()
point_cloud.points=open3d.utility.Vector3dVector(points)
open3d.visualization.draw_geometries([point_cloud])

這是建立的3d的可視化模型,可以通過鼠標(biāo)拖動(dòng)將其旋轉(zhuǎn)
攝像頭實(shí)時(shí)關(guān)鍵點(diǎn)檢測
定義可視化圖像函數(shù)
導(dǎo)入三維人臉關(guān)鍵點(diǎn)檢測模型
導(dǎo)入可視化函數(shù)和可視化樣式
讀取單幀函數(shù)
主要代碼和上面的圖像類似
import cv2 as cv
import mediapipe as mp
from tqdm import tqdm
import time
import matplotlib.pyplot as plt
# 導(dǎo)入三維人臉關(guān)鍵點(diǎn)檢測模型
mp_face_mesh=mp.solutions.face_mesh
# help(mp_face_mesh.FaceMesh)
model=mp_face_mesh.FaceMesh(
static_image_mode=False,#TRUE:靜態(tài)圖片/False:攝像頭實(shí)時(shí)讀取
refine_landmarks=True,#使用Attention Mesh模型
max_num_faces=5,#最多檢測幾張人臉
min_detection_confidence=0.5, #置信度閾值,越接近1越準(zhǔn)
min_tracking_confidence=0.5,#追蹤閾值
)
# 導(dǎo)入可視化函數(shù)和可視化樣式
mp_drawing=mp.solutions.drawing_utils
mp_drawing_styles=mp.solutions.drawing_styles
# 處理單幀的函數(shù)
def process_frame(img):
#記錄該幀處理的開始時(shí)間
start_time=time.time()
img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB)
results=model.process(img_RGB)
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
# mp_drawing.draw_detection(
# image=img,
# landmarks_list=face_landmarks,
# connections=mp_face_mesh.FACEMESH_TESSELATION,
# landmarks_drawing_spec=None,
# landmarks_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style()
# )
# 繪制人臉網(wǎng)格
mp_drawing.draw_landmarks(
image=img,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_TESSELATION,
# landmark_drawing_spec為關(guān)鍵點(diǎn)可視化樣式,None為默認(rèn)樣式(不顯示關(guān)鍵點(diǎn))
# landmark_drawing_spec=mp_drawing_styles.DrawingSpec(thickness=1,circle_radius=2,color=[66,77,229]),
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style()
)
# 繪制人臉輪廓、眼睫毛、眼眶、嘴唇
mp_drawing.draw_landmarks(
image=img,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_CONTOURS,
# landmark_drawing_spec為關(guān)鍵點(diǎn)可視化樣式,None為默認(rèn)樣式(不顯示關(guān)鍵點(diǎn))
# landmark_drawing_spec=mp_drawing_styles.DrawingSpec(thickness=1,circle_radius=2,color=[66,77,229]),
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style()
)
# 繪制瞳孔區(qū)域
mp_drawing.draw_landmarks(
image=img,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_IRISES,
# landmark_drawing_spec為關(guān)鍵點(diǎn)可視化樣式,None為默認(rèn)樣式(不顯示關(guān)鍵點(diǎn))
# landmark_drawing_spec=mp_drawing_styles.DrawingSpec(thickness=1, circle_radius=2, color=[0, 1, 128]),
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style())
else:
img = cv.putText(img, 'NO FACE DELECTED', (25 , 50 ), cv.FONT_HERSHEY_SIMPLEX, 1.25,
(218, 112, 214), 1, 8)
#記錄該幀處理完畢的時(shí)間
end_time=time.time()
#計(jì)算每秒處理圖像的幀數(shù)FPS
FPS=1/(end_time-start_time)
scaler=1
img=cv.putText(img,'FPS'+str(int(FPS)),(25*scaler,100*scaler),cv.FONT_HERSHEY_SIMPLEX,1.25*scaler,(0,0,255),1,8)
return img
# 調(diào)用攝像頭
cap=cv.VideoCapture(0)
cap.open(0)
# 無限循環(huán),直到break被觸發(fā)
while cap.isOpened():
success,frame=cap.read()
# if not success:
# print('ERROR')
# break
frame=process_frame(frame)
#展示處理后的三通道圖像
cv.imshow('my_window',frame)
if cv.waitKey(1) &0xff==ord('q'):
break
cap.release()
cv.destroyAllWindows()

到此這篇關(guān)于opencv+mediapipe實(shí)現(xiàn)人臉檢測及攝像頭實(shí)時(shí)的文章就介紹到這了,更多相關(guān)opencv 人臉檢測及攝像頭實(shí)時(shí)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python+mediapipe+opencv實(shí)現(xiàn)手部關(guān)鍵點(diǎn)檢測功能(手勢識別)
- OpenCV+MediaPipe實(shí)現(xiàn)手部關(guān)鍵點(diǎn)識別
- ?Python使用Mediapipe對圖像進(jìn)行手部地標(biāo)檢測
- Python+MediaPipe實(shí)現(xiàn)檢測人臉功能詳解
- 超好玩的"隔空操物"通過Python?MediaPipe庫實(shí)現(xiàn)
- 基于Mediapipe+Opencv實(shí)現(xiàn)手勢檢測功能
- OpenCV MediaPipe實(shí)現(xiàn)顏值打分功能
- MediaPipe API實(shí)現(xiàn)骨骼識別功能分步講解流程
相關(guān)文章
Python+Empyrical實(shí)現(xiàn)計(jì)算風(fēng)險(xiǎn)指標(biāo)
Empyrical 是一個(gè)知名的金融風(fēng)險(xiǎn)指標(biāo)庫。它能夠用于計(jì)算年平均回報(bào)、最大回撤、Alpha值等。下面就教你如何使用 Empyrical 這個(gè)風(fēng)險(xiǎn)指標(biāo)計(jì)算神器2022-05-05
Python圖像處理之識別圖像中的文字(實(shí)例講解)
今天小編就為大家分享一篇Python圖像處理之識別圖像中的文字(實(shí)例講解),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
python基礎(chǔ)練習(xí)之幾個(gè)簡單的游戲
這篇文章主要介紹了python基礎(chǔ)練習(xí)之幾個(gè)簡單的游戲,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
淺談python 導(dǎo)入模塊和解決文件句柄找不到問題
今天小編就為大家分享一篇淺談python 導(dǎo)入模塊和解決文件句柄找不到問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python設(shè)計(jì)模式之單例模式實(shí)例
這篇文章主要介紹了設(shè)計(jì)模式中的單例模式Python實(shí)例,需要的朋友可以參考下2014-04-04
基于python批量處理dat文件及科學(xué)計(jì)算方法詳解
今天小編就為大家分享一篇基于python批量處理dat文件及科學(xué)計(jì)算方法詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Python接口自動(dòng)化之淺析requests模塊get請求
這篇文章主要介紹了requests模塊get請求,在Python語言中,雖然提供了urllib2和urllib的庫,但是相比較而言,Requests仍然是實(shí)現(xiàn)接口測試最好的選擇,因?yàn)樗怯闷饋砀雍啽?/div> 2021-08-08
python實(shí)現(xiàn)外賣信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)外賣信息管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Python打包工具PyInstaller的安裝與pycharm配置支持PyInstaller詳細(xì)方法
這篇文章主要介紹了Python打包工具PyInstaller的安裝與pycharm配置支持PyInstaller詳細(xì)方法,需要的朋友可以參考下2020-02-02最新評論

