简体   繁体   English

Python:在人脸识别中动态保存未知人脸不起作用

[英]Python : Dynamically saving unknown faces in face recognition not working

I've a script which runs on image and recognizes faces and return a list like : 我有一个在图像上运行的脚本,可识别人脸并返回类似以下的列表:

[('Mike', (142, 464, 365, 241)),('Garry', (42, 364, 65, 141)),('unknown', (242, 564, 465, 341))]

The second tuple is the bounding box of the faces recognized. 第二个元组是识别出的面的边界框。 I've another script which uses webcam, recognizes faces in frames and shows them in the video feed. 我还有一个使用网络摄像头的脚本,可以识别帧中的面孔并将其显示在视频供稿中。 I want to automatically save "unknown" labelled faces in each frames, whenever it appears. 我想在出现时自动在每个帧中保存带有“未知”标签的面孔。 My code : 我的代码:

from stat_face_recog import runonimage
video_capture = cv2.VideoCapture(0)
while True:
    ret, frame = video_capture.read()
    cv2.imwrite("new.png",frame)
    final_pred = runonimage(img_path = "new.png")
    read_img = cv2.imread("new.png")
    for name, (top, right, bottom, left) in final_pred:        
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
        unknown_counter = 0
        if name == "unknown":
            unknowns_name = "unknown" + str(unknown_counter) + ".png"
            (new_top, new_right, new_bottom, new_left) = (int(0.8 * top), int(1.2* right), int(1.2*bottom), int(0.8*left))
            cv2.imwrite(unknowns_name,read_img[new_top:new_bottom, new_left:new_right])
            unknown_counter += 1
    cv2.imshow('Video', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()

But the problem is while it is recognizing untrained people pictures unknown, it's not saving those unknown faces. 但是问题是,当它识别出未经训练的人物照片时,并没有保存那些未知的面孔。 Only one image named "unknonw0.png" is saved every time. 每次仅保存一个名为“ unknonw0.png”的图像。 What is wrong in my code? 我的代码有什么问题?

You are resetting the unknown_counter to zero in your for loop. 您正在for循环中将unknown_counter重置为零。 Therefore, the image is overwriting every time. 因此,图像每次都被覆盖。 Just move it outside the loops. 只需将其移出循环即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM