python使用mediapiple+opencv識(shí)別視頻人臉的實(shí)現(xiàn)
1、安裝
pip install mediapipe
2、代碼實(shí)現(xiàn)
# -*- coding: utf-8 -*- """ @Time : 2022/3/18 14:43 @Author : liwei @Description: """ import cv2 import mediapipe as mp mp_drawing = mp.solutions.drawing_utils mp_face_mesh = mp.solutions.face_mesh mp_face_detection = mp.solutions.face_detection # 繪制人臉畫像的點(diǎn)和線的大小粗細(xì)及顏色(默認(rèn)為白色) drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1) cap = cv2.VideoCapture("E:\\video\\test\\test.mp4")# , cv2.CAP_DSHOW # For webcam input: # cap = cv2.VideoCapture(0) with mp_face_detection.FaceDetection( model_selection=0, min_detection_confidence=0.5) as face_detection: while cap.isOpened(): success, image = cap.read() if not success: print("Ignoring empty camera frame.") # If loading a video, use 'break' instead of 'continue'. break # To improve performance, optionally mark the image as not writeable to # pass by reference. image.flags.writeable = False image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = face_detection.process(image) # Draw the face detection annotations on the image. image.flags.writeable = True image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) if results.detections: box = results.detections[0].location_data.relative_bounding_box xmin = box.xmin ymin = box.ymin width = box.width height = box.height xmax = box.xmin + width ymax = ymin + height cv2.rectangle(image, (int(xmin * image.shape[1]),int(ymin* image.shape[0])), (int(xmax* image.shape[1]), int(ymax* image.shape[0])), (0, 0, 255), 2) # for detection in results.detections: # mp_drawing.draw_detection(image, detection) # Flip the image horizontally for a selfie-view display. cv2.imshow('MediaPipe Face Detection', cv2.flip(image, 1)) if cv2.waitKey(5) & 0xFF == 27: break cap.release()
效果
3、更新 mediapiple+threadpool+opencv實(shí)現(xiàn)圖片人臉采集效率高于dlib
# -*- coding: utf-8 -*- """ @Time : 2022/3/23 13:43 @Author : liwei @Description: """ import cv2 as cv import mediapipe as mp import os import threadpool mp_drawing = mp.solutions.drawing_utils mp_face_mesh = mp.solutions.face_mesh mp_face_detection = mp.solutions.face_detection savePath = "E:\\saveImg\\" basePath = "E:\\img\\clear\\20220301\\" def cut_face_img(file): # print(basePath + file) img = cv.imread(basePath + file) with mp_face_detection.FaceDetection( model_selection=0, min_detection_confidence=0.5) as face_detection: img.flags.writeable = False image = cv.cvtColor(img, cv.COLOR_RGB2BGR) results = face_detection.process(image) image = cv.cvtColor(image, cv.COLOR_RGB2BGR) image.flags.writeable = True if results.detections: box = results.detections[0].location_data.relative_bounding_box xmin = box.xmin ymin = box.ymin width = box.width height = box.height xmax = box.xmin + width ymax = ymin + height x1, x2, y1, y2 = int(xmax * image.shape[1]), int(xmin * image.shape[1]), int( ymax * image.shape[0]), int(ymin * image.shape[0]) cropped = image[y2:y1, x2:x1] if cropped.shape[1] > 200: cv.imwrite(savePath + file, cropped) print(savePath + file) if __name__ == '__main__': data = os.listdir(basePath) pool = threadpool.ThreadPool(3) requests = threadpool.makeRequests(cut_face_img, data) [pool.putRequest(req) for req in requests] pool.wait()
到此這篇關(guān)于python使用mediapiple+opencv識(shí)別視頻人臉的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)mediapiple opencv識(shí)別視頻人臉內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
opencv python 對(duì)指針儀表讀數(shù)識(shí)別的兩種方式
這篇文章主要介紹了opencv python 對(duì)指針儀表讀數(shù)識(shí)別的兩種方式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Django crontab定時(shí)任務(wù)模塊操作方法解析
這篇文章主要介紹了Django crontab定時(shí)任務(wù)模塊操作方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09詳解如何通過Python實(shí)現(xiàn)批量數(shù)據(jù)提取
每天面對(duì)成堆的發(fā)票,無論是發(fā)票還是承兌單據(jù),抑或是其他各類公司數(shù)據(jù)要從照片、PDF等不同格式的內(nèi)容中提取,我們都有必要進(jìn)行快速辦公的能力提升。本文就教你如何利用Python實(shí)現(xiàn)批量數(shù)據(jù)提取吧2023-03-03Pytorch使用Visdom進(jìn)行數(shù)據(jù)可視化的示例代碼
pytorch Visdom可視化,是一個(gè)靈活的工具,用于創(chuàng)建,組織和共享實(shí)時(shí)豐富數(shù)據(jù)的可視化,這個(gè)博客簡(jiǎn)要介紹一下在使用Pytorch進(jìn)行數(shù)據(jù)可視化的一些內(nèi)容,感興趣的朋友可以參考下2023-12-12python批量將excel內(nèi)容進(jìn)行翻譯寫入功能
這篇文章主要介紹了python批量將excel內(nèi)容進(jìn)行翻譯寫入功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10淺談python出錯(cuò)時(shí)traceback的解讀
這篇文章主要介紹了淺談python出錯(cuò)時(shí)traceback的解讀,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07使用Python遍歷文件夾實(shí)現(xiàn)查找指定文件夾
這篇文章主要為大家介紹了如何使用Python遍歷文件夾從而實(shí)現(xiàn)查找指定文件夾下所有相同名稱的文件、所有相同后綴名的文件,感興趣的可以了解一下2022-07-07Python實(shí)現(xiàn)給qq郵箱發(fā)送郵件的方法
這篇文章主要介紹了Python實(shí)現(xiàn)給qq郵箱發(fā)送郵件的方法,涉及Python郵件發(fā)送的相關(guān)技巧,需要的朋友可以參考下2015-05-05一篇文章帶你學(xué)習(xí)Python3的高級(jí)特性(2)
這篇文章主要為大家詳細(xì)介紹了Python3的高階函數(shù),主要介紹什么是高級(jí)特性,高級(jí)特性的用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01