开发者

python使用mediapiple+opencv识别视频人脸的实现

开发者 https://www.devze.com 2022-12-13 13:16 出处:网络 作者: 拼命_小李
目录1、安装2、代码实现3、更新 mediapiple+threadpool+opencv实现图片人脸采集效率高于dlib1、安装
目录
  • 1、安装
  • 2、代码实现
  • 3、更新 mediapiple+threadpool+opencv实现图片人脸采集效率高于dlib

1、安装

pip install mediapipe

2、代码实现

# -*- 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
# 绘制人脸画像的点和线的大小粗细及颜色(默认为白色)
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()

效果

python使用mediapiple+opencv识别视频人脸的实现

3、更新 mediapiple+threadpool+opencv实现图片人脸采集效率高于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
 www.cppcns.com       image = cv.cvtColor(img, cv.COLOR_RGB2BGR)http://www.cppcns.com
        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()
 

到此这篇关于python使用mediapiple+opencv识别视频人脸的实现的文章就介绍到这了,更多相关mediapiple编程客栈 opencv识别视频人脸内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号