我想比较两张照片。首先是一个人的面孔。第二张是多张面孔的合影。我想看看第一张照片中的人是否出现在第二张照片中。
我尝试通过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保存它们,然后尝试将其传递到深脸中时,也会遇到同样的错误。任何人都可以推荐任何其他方法来实现此功能吗?还是解决我目前的尝试?非常感谢!
答案 0 :(得分:3)
deepface旨在比较两张脸,但您仍然可以比较一对多的脸部识别。
您有两张照片。一个人只有一张脸部照片。我称之为img1.jpg。第二个有很多面孔。我称这个为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"])
因此,您可以按照自己的情况调整表情。