使用mediapipe进行人脸识别

import cv2
import mediapipe as mp
import time

cap = cv2.VideoCapture('C:/Users/Curry\Desktop/Kobe.mp4',0)

mpFace_dec = mp.solutions.face_detection
mpdraw = mp.solutions.drawing_utils
face_dec = mpFace_dec.FaceDetection(0.75)

Pre_time = 0
while 1:
    res, img = cap.read()
    img_rgb = cv2.cvtColor(img, code=cv2.COLOR_BGR2RGB)
    Results = face_dec.process(img_rgb)
    # print(Results)
    if Results.detections:
        for id, detection in enumerate(Results.detections):
            # mpdraw.draw_detection(img, detection)
            # print(id, detection)
            # print(detection.score)
            # 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, 0, 255), 2)

    cur_Time = time.time()
    fps = 1/(cur_Time-Pre_time)
    Pre_time = cur_Time
    cv2.putText(img, 'FPS:{}'.format(int(fps)), (20, 70),
                cv2.FONT_HERSHEY_PLAIN, 3, (0, 0, 255), 2)
    cv2.imshow('kobe', img)
    cv2.waitKey(1)



使用mediapipe进行人脸识别_第1张图片

import cv2
import mediapipe as mp
import time

cap = cv2.VideoCapture('C:/Users/Curry\Desktop/kobe.mp4',0)

mpFace_dec = mp.solutions.face_detection
mpdraw = mp.solutions.drawing_utils
face_dec = mpFace_dec.FaceDetection(0.75)

Pre_time = 0
while 1:
    res, img = cap.read()
    img_rgb = cv2.cvtColor(img, code=cv2.COLOR_BGR2RGB)
    Results = face_dec.process(img_rgb)
    # print(Results)
    if Results.detections:
        for id, detection in enumerate(Results.detections):
            # mpdraw.draw_detection(img, detection)
            # print(id, detection)
            # print(detection.score)
            # 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, 0, 255), 2)
            cv2.putText(img, f'{int(detection.score[0]*100)}%',(bbox[0],bbox[1]-20),
                    cv2.FONT_HERSHEY_PLAIN,2,(255, 0, 255), 2)

    cur_Time = time.time()
    fps = 1/(cur_Time-Pre_time)
    Pre_time = cur_Time
    cv2.putText(img, 'FPS:{}'.format(int(fps)), (20, 70),
                cv2.FONT_HERSHEY_PLAIN, 3, (0, 0, 255), 2)
    cv2.imshow('kobe', img)
    cv2.waitKey(1)



使用mediapipe进行人脸识别_第2张图片

你可能感兴趣的:(深度学习)