我想比较两张照片。第一个有一个人的脸。第二张是许多面孔的合影。我想看看第一张照片中的人是否会出现在第二张照片中。
我尝试使用python中的deepface和face_recognition库来实现这一点,方法是从合影中逐个提取面孔,并将它们与原始照片进行比较。
face_locations = face_recognition.face_locations(img2_loaded)
for face in face_locations:
top, right, bottom, left = face
face_img = img2_loaded[top:bottom, left:right]
face_recognition.compare_faces(img1_loaded, face_img)
这会导致有关操作数不能与形状(3088,2316,3) (90,89,3)一起广播的错误。当我使用PIL保存从合影中拉出的面孔,然后尝试将它们传递到deepface中时,也会遇到同样的错误。有没有人能推荐一些替代方法来实现这个功能?或者修复我当前的尝试?非常感谢!
发布于 2020-06-27 16:49:44
deepface被设计用来比较两张脸,但你仍然可以比较一对多的人脸识别。
你有两张照片。其中一张只有一张脸部照片。我将其称为img1.jpg。而second有很多面孔。我将其称为img2.jpg。
您可以首先使用OpenCV检测img2.jpg中的人脸。
import cv2
img2 = cv2.imread("img2.jpg")
face_detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
faces = face_detector.detectMultiScale(img2, 1.3, 5)
detected_faces = []
for face in faces:
x,y,w,h = face
detected_face = img2[int(y):int(y+h), int(x):int(x+w)]
detected_faces.append(detected_face)
然后,您需要将faces变量的每一项与img1.jpg进行比较。
img1 = cv2.imread("img1.jpg")
targets = face_detector.detectMultiScale(img1, 1.3, 5)
x,y,w,h = targets[0] #this has just a single face
target = img1[int(y):int(y+h), int(x):int(x+w)]
for face in detected_faces:
#compare face and target in each iteration
compare(face, target)
我们应该设计比较函数
from deepface import DeepFace
def compare(img1, img2):
resp = DeepFace.verify(img1, img2)
print(resp["verified"])
因此,您可以将deepface调整为适合您的情况。
https://stackoverflow.com/questions/62585725
复制相似问题