用python和opencv计算面数

时间:2016-05-24 11:23:40

标签: python opencv counter face-detection

我在 python 中使用opencv2来检测face。我在我的网络摄像头中拍摄视频并使用Haar Cascade来检测面部。我想摆脱一帧中检测到的脸部数量。我知道这可以通过在找到面部时计算矩形来完成。怎么做?如何计算一帧中的矩形?

import cv2
import sys

faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

video_capture = cv2.VideoCapture(0)


while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)



    # Display the resulting frame
    cv2.imshow('Video', frame)


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

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:2)

好吧,我想len(faces)会返回面部的数量