python使用mediapiple+opencv识别视频人脸的实现
作者:拼命_小李 发布时间:2023-06-09 08:10:17
标签: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()
来源:https://blog.csdn.net/m0_43432638/article/details/123684319


猜你喜欢
- 今天对clubot进行了升级, 但是导入数据后中文乱码, 一开是找资料说是在创建引擎的时候添加编码信息:engine = create_en
- 1、建立socket建立socket对象需要搞清通信类型和协议家族。通信类型指明了用什么协议来传输数据。协议的例子包括IPv4、IPv6、I
- Python中的random模块用于生成随机数。下面介绍一下random模块中最常用的几个函数。random.randomrandom.ra
- 本文实例讲述了Python实现连接postgresql数据库的方法。分享给大家供大家参考,具体如下:python可以通过第三方模块连接pos
- 本文研究的主要是Python web开发框架Django的管理界面的相关内容,具体如下。admin界面的路径,通常在my_project中已
- 含义切片是一个种特殊的数组。是对数组的一个连续片段的引用,所以切片是一个引用类型。切片可以是数组中的一部分,也可以是由起始和终止索引标识的一
- 在官网下载python和pycharm的安装包python: https://www.python.org/downloads/window
- 一、简介开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能。二、参数说明s
- 最近在用vue做项目,学习了不少东西,但是有时候光顾着做项目却忘记要找个时间来整理一下最近的一些学习新得,因为是新手,所以可能会有错误的地方
- 怎样判断一个JavaScript变量是array还是obiect? 答案: 1、如果你只是用typeof来检查该变量,不论是array还是o
- 在电子产品的设计中,大家经常提到简洁是设计的重要元素。可是很多产品,不见得简洁就是第一要素。简洁的设计,必须是在对用户需求透彻理解,引导用户
- 在关闭mysql server的时候,可能会由于关闭的方式而出现各种问题。下面的一些步骤,可以减少问题的发生。1.停止复制在一些特殊环境下,
- 本文实例讲述了Python中DJANGO简单测试的用法。分享给大家供大家参考。具体如下:这里以facebook台湾的测试版为例。仅仅测试用户
- 1、说明装饰本质上是一个Python函数,它能使其他函数在没有任何代码变化的情况下增加额外的功能。有了装饰,我们可以抽出大量与函数功能无关的
- Flask web上传获取图像Image读取并使用图片上传界面后端@app.route('/upload')def uplo
- Python编程中raise可以实现报出错误的功能,而报错的条件可以由程序员自己去定制。在面向对象编程中,可以先预留一个方法接口不实现,在其
- 上期回顾上一次的图像清晰度评价没有成功,主要的原因是那几张图像清晰度评价函数都实际都采用了梯度求解,不同的场景灰度的明暗不同,梯度可能会很大
- 本文实例为大家分享了Python实现24点小游戏的具体代码,供大家参考,具体内容如下玩法:通过加减乘除操作,小学生都没问题的。源码分享:im
- mysql升级5.7版本以后,安全性大幅度上升。但是呢。。。带复杂的记不住。额额。。本来脑子就不好使,还记那么复杂,尤其是本地就更没必要,还
- SQL2005安装安装步骤安装Microsoft SQL Server 2005 数据库步骤:第一步:将Microsoft SQL Serv