如何使用opencv建立一个人脸检测程序,只要人脸停留在屏幕上,就启动一个计时器来记录时间?

How to build a face detection program using opencv and start a timer to record the time as long as face stays on the screen?

我想构建一个人脸检测 python OpenCV 程序,它会在检测到人脸时立即启动计时器。如果网络摄像头前的人脸被移走,计时器设置为 0,并在检测到新人脸后重新启动。

到目前为止我已经尝试过。

import numpy as np
import cv2
import time

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

cap = cv2.VideoCapture(0)

start = "y"
timeLoop = True

while 1:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

        cv2.putText(img, "Number of faces detected: " + str(faces.shape[0]), (0,img.shape[0] -10), cv2.FONT_HERSHEY_TRIPLEX, 0.5,  (0,0,255), 1)

        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

    # Variables to keep track and display
    Sec = 0
    Min = 0        
    # Begin Process
    cv2.imshow('img',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

    if len(faces) > 0:  
        timeLoop = start  
        while timeLoop:
            Sec += 1
            print(str(Min) + " Mins " + str(Sec) + " Sec ")
            #cv2.putText(img, "Time: " + str(Min) + " Mins " + str(Sec) + " Sec ", (0,img.shape[0] -10), cv2.FONT_HERSHEY_TRIPLEX, 0.5,  (0,0,255), 1)
            time.sleep(1)
            if Sec == 60:
                Sec = 0
                Min += 1
                print(str(Min) + " Minute")

cap.release()
cv2.destroyAllWindows()

本程序适用于人脸检测和定时器。但是一旦定时器启动,人脸检测模块就会卡在循环中,不会继续前进。 但是计时器在后台运行。

如何解决这个问题?

我想你会喜欢它的。时间函数的循环和放置只有一些小问题。

在您的例子中,您放置了第二个 while 循环来启动计时器。因此代码一直在那里循环。这就是为什么(如 @api55 所述)计时器在后台 NOT 运行 的原因。相反,在检测到人脸时,它会开始循环并继续计算秒数。

代码如下:

import numpy as np
import cv2
import time

path = '/Desktop/Stack/face_detection/'

face_cascade = cv2.CascadeClassifier(path + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(path + 'haarcascade_eye.xml')

cap = cv2.VideoCapture(0)

Sec = 0
Min = 0 

while 1:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)     

    if len(faces) > 0:  

        Sec += 1
        print(str(Min) + " Mins " + str(Sec) + " Sec ")

        cv2.putText(img, "Time: " + str(Min) + " Mins " + str(Sec) + " Sec ", (0,img.shape[0] -30), cv2.FONT_HERSHEY_TRIPLEX, 0.5,  (0,0,255), 1)
        cv2.putText(img, "Number of faces detected: " + str(faces.shape[0]), (0,img.shape[0] -10), cv2.FONT_HERSHEY_TRIPLEX, 0.5,  (0,0,255), 1)    

        time.sleep(1)
        if Sec == 60:
            Sec = 0
            Min += 1
            print(str(Min) + " Minute")                

    if len(faces) == 0:

        print('No face detected')
        cv2.putText(img, "No face detected ", (0,img.shape[0] -10), cv2.FONT_HERSHEY_TRIPLEX, 0.5,  (0,0,255), 1)        
        Sec = 0
        Min = 0

    cv2.imshow('img',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break    

cap.release()
cv2.destroyAllWindows()

希望这就是您所期望的!!