欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

opencv+mediapipe實(shí)現(xiàn)人臉檢測(cè)及攝像頭實(shí)時(shí)示例

 更新時(shí)間:2021年12月30日 09:54:55   作者:墻縫里的草  
本文主要介紹了opencv+mediapipe實(shí)現(xiàn)人臉檢測(cè)及攝像頭實(shí)時(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

單張人臉關(guān)鍵點(diǎn)檢測(cè)

定義可視化圖像函數(shù)
導(dǎo)入三維人臉關(guān)鍵點(diǎn)檢測(cè)模型
導(dǎo)入可視化函數(shù)和可視化樣式
讀取圖像
將圖像模型輸入,獲取預(yù)測(cè)結(jié)果
BGR轉(zhuǎn)RGB
將RGB圖像輸入模型,獲取預(yù)測(cè)結(jié)果
預(yù)測(cè)人人臉個(gè)數(shù)
可視化人臉關(guān)鍵點(diǎn)檢測(cè)效果
繪制人來(lái)臉和重點(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)檢測(cè)模型
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ù)測(cè)結(jié)果

# BGR轉(zhuǎn)RGB
img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB)

# 將RGB圖像輸入模型,獲取預(yù)測(cè)結(jié)果

results=model.process(img_RGB)
# 預(yù)測(cè)人人臉個(gè)數(shù)
len(results.multi_face_landmarks)

print(len(results.multi_face_landmarks))

# 結(jié)果:1


# 可視化人臉關(guān)鍵點(diǎn)檢測(cè)效果

# 繪制人來(lái)臉和重點(diǎn)區(qū)域輪廓線,返回annotated_image
annotated_image=img.copy()
if results.multi_face_landmarks: #如果檢測(cè)出人臉
    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)

請(qǐng)?zhí)砑訄D片描述

請(qǐng)?zhí)砑訄D片描述

請(qǐng)?zhí)砑訄D片描述

單張圖像人臉檢測(cè)

可以通過(guò)調(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)檢測(cè)模型
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)檢測(cè)/dkx.jpg')
# width=img1.shape[1]
# height=img1.shape[0]
# img=cv.resize(img1,(width*10,height*10))
# look_img(img)

# 將圖像模型輸入,獲取預(yù)測(cè)結(jié)果

# BGR轉(zhuǎn)RGB
img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB)

# 將RGB圖像輸入模型,獲取預(yù)測(cè)結(jié)果

results=model.process(img_RGB)
# # 預(yù)測(cè)人人臉個(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('未檢測(cè)出人臉')
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])

請(qǐng)?zhí)砑訄D片描述

這是建立的3d的可視化模型,可以通過(guò)鼠標(biāo)拖動(dòng)將其旋轉(zhuǎn)

攝像頭實(shí)時(shí)關(guān)鍵點(diǎn)檢測(cè)

定義可視化圖像函數(shù)
導(dǎo)入三維人臉關(guān)鍵點(diǎn)檢測(cè)模型
導(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)檢測(cè)模型
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,#最多檢測(cè)幾張人臉
    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):
    #記錄該幀處理的開(kāi)始時(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)
# 無(wú)限循環(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)人臉檢測(cè)及攝像頭實(shí)時(shí)的文章就介紹到這了,更多相關(guān)opencv 人臉檢測(cè)及攝像頭實(shí)時(shí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論