本文介绍了用python和opencv数脸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 python 中使用opencv2来检测人脸的脚本.我将视频放在网络摄像头中,并使用Haar Cascade进行面部检测.我想摆脱在一帧中检测到的面部数量.我知道可以通过在发现面孔时计算矩形来完成.怎么做?如何在一帧中计算矩形?
I have script in python using opencv2 to detect face. I take video in my webcam and using Haar Cascade for detect faces. I want to get out of the number of detected faces in a one frame. I understand that this can be done by counting rectangles when a face is found. how to do it? How to count rectangles in one frame?
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()
推荐答案
简单使用 len(faces)
应该会返回面孔数.
Simple use of len(faces)
should return the number of faces.
这篇关于用python和opencv数脸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!