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

 更新时间:2022年03月24日 15:36:06   作者:拼命_小李  
本文主要介绍了python使用mediapiple+opencv识别视频人脸,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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()

效果

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
        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()
 

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

相关文章

  • 解决Python下json.loads()中文字符出错的问题

    解决Python下json.loads()中文字符出错的问题

    今天小编就为大家分享一篇解决Python下json.loads()中文字符出错的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • 教你实现Ubuntu安装Python

    教你实现Ubuntu安装Python

    这篇文章主要为大家介绍了Ubuntu安装Python的实现过程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • pandas常用表连接merge/concat/join/append详解

    pandas常用表连接merge/concat/join/append详解

    使用python的pandas库可以很容易帮你搞定,而且性能也是很出色的;百万级的表关联,可以秒出,本文给大家分享pandas常用表连接merge/concat/join/append详解,感兴趣的朋友跟随小编一起看看吧
    2023-02-02
  • python飞机大战pygame游戏之敌机出场实现方法详解

    python飞机大战pygame游戏之敌机出场实现方法详解

    这篇文章主要介绍了python飞机大战pygame游戏之敌机出场实现方法,结合实例形式详细分析了Python使用pygame模块实现飞机大战游戏中敌机出场相关实现技巧,需要的朋友可以参考下
    2019-12-12
  • Pandas中GroupBy具体用法详解

    Pandas中GroupBy具体用法详解

    pandas中的DF数据类型可以像数据库表格一样进行groupby操作。通常来说groupby操作可以分为三部分:分割数据,应用变换和和合并数据。本文就详细的来介绍一下,感兴趣的可以了解一下
    2021-07-07
  • 仅用50行Python代码实现一个简单的代理服务器

    仅用50行Python代码实现一个简单的代理服务器

    这篇文章主要介绍了仅用50行Python代码实现一个简单的代理服务器,利用最简单的client->proxy->forward原理在socket模块下编写,需要的朋友可以参考下
    2015-04-04
  • Python及Django框架生成二维码的方法分析

    Python及Django框架生成二维码的方法分析

    这篇文章主要介绍了Python及Django框架生成二维码的方法,结合实例形式分析了Python及Django框架使用qrcode包实现二维码生成功能的相关操作技巧,需要的朋友可以参考下
    2018-01-01
  • Python执行JS代码的三种方式

    Python执行JS代码的三种方式

    以前的数据靠买,现在的数据靠爬”,越来越多的学者通过网络爬虫来获取数据,但是做爬虫的人都知道,现在的很多网站都在和我们斗智斗勇,防护普遍越来越好,破解JS加密只是第一步,之后就是如何在我们的Python代码中直接执行JS,下面介绍一下几种Python中执行JS代码的方法
    2024-01-01
  • Python应用实现处理excel数据过程解析

    Python应用实现处理excel数据过程解析

    这篇文章主要介绍了Python应用实现处理excel数据过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • python 画条形图(柱状图)实例

    python 画条形图(柱状图)实例

    这篇文章主要介绍了python 画条形图(柱状图)实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04

最新评论