OpenCV 当在实时提要中未检测到人脸时,退出

OpenCV When face not detected in live feed, exit

所以我正在尝试制作一个代码,当从网络摄像头检测到人脸时,它会在人脸周围显示一个绿色方块。那部分完成了。接下来我要做的是,当程序不再检测到人脸时,它会中断循环并退出程序。我尝试通过“if”或“else”或在网上找到一些东西,但我哪儿也去不了。有什么办法吗?这是我的代码:

import cv2
import os
import time

cascPath = os.path.dirname(
    cv2.__file__) + "/data/haarcascade_frontalface_alt2.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
while True:
    ret, frame = video_capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(gray,
                                         scaleFactor=1.1,
                                         minNeighbors=5,
                                         minSize=(60, 60),
                                         flags=cv2.CASCADE_SCALE_IMAGE)
    for (x,y,w,h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h),(0,255,0), 2)
    cv2.imshow('Video', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()

添加这个怎么样?统计没有检测到人脸的次数;如果超过阈值则中断:

iter_with_no_faces=0 #put this outside the main loop
### put the follwing after updating `faces`
if len(faces) ==0:
    iter_with_no_faces+=1
##  add break condition as this:
if iter_with_no_faces >100:
    break

您可以 iter_with_no_facesfaces 循环中:iter_with_no_faces=0

总而言之,稍作修改可能会奏效:

import cv2
import os
import time

cascPath = os.path.dirname(
    cv2.__file__) + "/data/haarcascade_frontalface_alt2.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
iter_with_no_faces=0 #put this outside the main loop
while True:
    ret, frame = video_capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(gray,
                                         scaleFactor=1.1,
                                         minNeighbors=5,
                                         minSize=(60, 60),
                                         flags=cv2.CASCADE_SCALE_IMAGE)
    for (x,y,w,h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h),(0,255,0), 2)

    if len(faces) ==0:
        iter_with_no_faces+=1
    else:
        iter_with_no_faces=0 # I assume you want to continue program when a face detected for a duration. you can omit else statement
    if iter_with_no_faces >100: #set this threshold to larger or smaller number
        break
    cv2.imshow('Video', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()
import cv2


def main():
    cascPath = './haarcascade_frontalface_alt2.xml'  # path to the xml file - change to your path
    faceCascade = cv2.CascadeClassifier(cascPath)
    video_capture = cv2.VideoCapture(0)
    for i in range(10 ** 10):
        ret, frame = video_capture.read()
        if ret:
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            faces = faceCascade.detectMultiScale(gray,
                                                 scaleFactor=1.1,
                                                 minNeighbors=5,
                                                 minSize=(60, 60),
                                                 flags=cv2.CASCADE_SCALE_IMAGE)
            if len(faces) > 0:
                for (x, y, w, h) in faces:
                    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
                cv2.imshow('Video', frame)
                k = cv2.waitKey(1)
                if k == ord('q'):
                    break
            else:
                print('No face detected on iter {}'.format(i))
                # add here break or whatever you want to do if no face detected
    video_capture.release()
    cv2.destroyAllWindows()
    return


if __name__ == '__main__':
    main()