Python : 人脸识别动态保存未知面孔无效
Python : Dynamically saving unknown faces in face recognition not working
我有一个在图像上运行并识别面孔的脚本和 return 一个列表,例如:
[('Mike', (142, 464, 365, 241)),('Garry', (42, 364, 65, 141)),('unknown', (242, 564, 465, 341))]
第二个元组是识别出的人脸的边界框。我有另一个脚本,它使用网络摄像头,识别帧中的面孔并在视频源中显示它们。我想在每个帧中自动保存 "unknown" 标记的面孔,无论何时出现。我的代码:
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()
但问题是,它在识别未经训练的未知人物照片时,并没有保存那些未知的面孔。每次只保存一张名为"unknonw0.png"的图片。我的代码有什么问题?
您正在将 for 循环中的 unknown_counter
重置为零。因此,图像每次都会被覆盖。只需将其移出循环即可。
我有一个在图像上运行并识别面孔的脚本和 return 一个列表,例如:
[('Mike', (142, 464, 365, 241)),('Garry', (42, 364, 65, 141)),('unknown', (242, 564, 465, 341))]
第二个元组是识别出的人脸的边界框。我有另一个脚本,它使用网络摄像头,识别帧中的面孔并在视频源中显示它们。我想在每个帧中自动保存 "unknown" 标记的面孔,无论何时出现。我的代码:
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()
但问题是,它在识别未经训练的未知人物照片时,并没有保存那些未知的面孔。每次只保存一张名为"unknonw0.png"的图片。我的代码有什么问题?
您正在将 for 循环中的 unknown_counter
重置为零。因此,图像每次都会被覆盖。只需将其移出循环即可。