我使用以下代码从早到晚统计面孔。
while True:
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
detections = faceCascade.detectMultiScale(gray, 1.15, 5)
current_viewers = len(detections)
if current_viewers > last_total_current_viewers:
people_count += current_viewers - last_total_current_viewers
last_total_current_viewers = current_viewers
for i in range(len(detections)):
face_i = detections[i]
x, y, w, h = face_i
cv2.rectangle(frame, (x, y),
(x + w, y + h),
(222,222,222), 2)
cv2.putText(frame, str(people_count - i ), (x, y - 10), font, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
以上代码工作正常,但不稳定。如果用户摇头或用户快速移动,则计数迅速增加。如何跟踪每个并保持计数不变?