【发布时间】:2022-12-09 22:27:02
【问题描述】:
我使用这个程序来检测从我的网络摄像头拍摄的视频中的人脸,一切正常,并且一个矩形显示在框架中出现的任何人脸上。 我正在使用此代码将面部的 x 值发送到 Arduino 微控制器以操纵伺服。当没有人脸时,x 值与上次有人脸时的值相同。 我怎么知道框架中没有人脸,所以我可以告诉伺服器保持在同一位置?
这是代码
import cv2
import sys
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(1)
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)
if x>=300:
print("right")
elif x<=240:
print("left")
elif x<300 and x>240:
print('mid')
else:
print('no face detected')
# 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()
我正在尝试打印“中”当脸在画面中间时,“剩下”当它在左边时“正确的”当它在右边时。 它工作正常但如果脸在右边并且消失, "right" 仍然会被打印出来并且“找不到面孔”永远不会被打印出来。 我期待有什么东西告诉我框架中没有面孔。
【问题讨论】:
-
欢迎。 tour、How to Ask、minimal reproducible example。 MRE 意味着使用调试器调试您自己的代码。当列表为空时,您会注意到。
标签: python