【人体关键点定位】mediapipe_人脸识别(三)

该博客介绍了如何利用Python和MediaPipe库实现一个实时的人脸检测系统。代码分为函数式和模块式两种实现方式,通过OpenCV显示检测到的人脸,并在界面上显示FPS和边界框。MediaPipe提供了解决方案,可以高效地处理视频帧,检测人脸并返回其位置信息。
摘要由CSDN通过智能技术生成

函数式

import cv2
import mediapipe as mp
import time
import os
import random

def video_ope(file):
    Frame = 0
    switch = True
    cap = cv2.VideoCapture(file)
    myface_detection = mp.solutions.face_detection
    myDraw = mp.solutions.drawing_utils
    face_detection = myface_detection.FaceDetection()
    
    while(True):
        ret, img = cap.read()
        img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        results = face_detection.process(img_rgb)
        if (results.detections):
            for id,detection in enumerate(results.detections):
                print(detection.location_data.relative_bounding_box)
                bboxc = detection.location_data.relative_bounding_box
                h,w,c  = img.shape
                bbox = int(bboxc.xmin*w),int(bboxc.ymin*h),int(bboxc.width*w),int(bboxc.height*h)
                cv2.rectangle(img,bbox,(255,255,0),2)
                cv2.putText(img,f'FPS:{int(detection.score[0]*100)}%',(bbox[0],bbox[1]-20),cv2.FONT_HERSHEY_PLAIN,3,(0,255,0),2)            

        Frame+=1
        cv2.putText(img,str(int(Frame)),(10,70),cv2.FONT_HERSHEY_PLAIN,3,(255,0,255),3)
        cv2.imshow("Frame", img)

        key = cv2.waitKey(1) & 0xFF
        if key== ord('q'):
            cv2.waitKey(0)
        if key== 27:
            break

    cap.release()
    cv2.destroyAllWindows()

def main():
    file = "video/eye.mp4"
    video_ope(file)

if __name__=="__main__":
    main()

模块式

import cv2
import mediapipe as mp
import time
import os
import random

class FaceDetector:
    def __init__(self,minDetectionCon=0.5):
        self.minDetectionCon = minDetectionCon
        self.mpFaceDetection = mp.solutions.face_detection
        self.mpDraw = mp.solutions.drawing_utils
        self.faceDetection = self.mpFaceDetection.FaceDetection(0.75)
    def findFace(self, img, draw=True):
        img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        results = self.faceDetection.process(img_rgb)
        return results.detections
    def drawFace(self, location,img , draw=True):
        for id,detection in enumerate(location):
            print(detection.location_data.relative_bounding_box)
            bboxc = detection.location_data.relative_bounding_box
            h,w,c  = img.shape
            bbox = int(bboxc.xmin*w),int(bboxc.ymin*h),int(bboxc.width*w),int(bboxc.height*h)
            cv2.rectangle(img,bbox,(255,255,0),2)
            cv2.putText(img,f'FPS:{int(detection.score[0]*100)}%',(bbox[0],bbox[1]-20),cv2.FONT_HERSHEY_PLAIN,3,(12,255,12),2)    
                  
   
def main(file):
    obj = FaceDetector()
    cap = cv2.VideoCapture(file)
    Frame = 0
    while True:
        success, img = cap.read()
        location = obj.findFace(img)
        if location:
            obj.drawFace(location,img)
        Frame = Frame + 1
        cv2.putText(img, str(int(Frame)), (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3)
        cv2.imshow("Image", img)
        key = cv2.waitKey(1) & 0xFF
 
        if key== ord('q'):
            cv2.waitKey(0)
        if key== 27:
            break
    cap.release()
    cv2.destroyAllWindows()

if __name__=="__main__":
    file = "video/eye.mp4"
    main(file)

在这里插入图片描述

参考:https://google.github.io/mediapipe/solutions/face_mesh.html

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佐倉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值