2016-05-24 60 views
-1

我在python腳本中使用opencv2來檢測臉部。我在我的攝像頭中拍攝視頻,並使用Haar Cascade檢測臉部。我想擺脫一幀中檢測到的人臉數量。我知道這可以通過在發現臉部時計算矩形來完成。怎麼做?如何計算一幀中的矩形?用python和opencv計數面孔

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() 
+0

從未嘗試過這個LIB的數量,但你有沒有嘗試過用LEN(面)? – KimKulling

+0

@KimKulling感謝它的工作! –

回答