python 中的 cvlib 库非常成熟,研究界的许多人都在使用它。我注意到,如果 threr 没有检测到人脸,则 (for) 循环停止,例如,如果我有以下代码:
cap = cv2.VideoCapture(0)
if not (cap.isOpened()):
print('Could not open video device')
#To set the resolution
vid_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
vid_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
while(True):
ret, frame = cap.read()
if not ret:
continue
faces, confidences = cv.detect_face(frame)
# loop through detected faces and add bounding box
for face in faces:
(startX,startY) = face[0],face[1]
(endX,endY) = face[2],face[3]
cv2.rectangle(frame, (startX,startY), (endX,endY), (0,255,0), 2)
crop_img = frame[startY-5:endY-5, startX-5:endX-5]```
print(faces)
cv2.imshow('object detected',frame)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
当我打印(面孔)时,输出将是这样的
[[392, 256, 480, 369]]
[[392, 256, 478, 369]]
[[392, 255, 478, 370]]
.
.
.
[[392, 255, 478, 370]]
但是,一旦我挡住相机或将头从相机上移开,因为没有检测到人脸,for 循环会冻结或暂停,直到它看到要检测的人脸。
我需要一个 if 语句或任何其他条件来检查此冻结或暂停以执行其他操作。