集体照中的面部比较

2024-09-27 21:26:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我想比较两张照片。第一个有一个人的脸。第二张是多张脸的合影。我想看看第一张照片中的个人是否出现在第二张照片中

我尝试用python中的deepface和face_识别库来实现这一点,方法是从团体照片中逐个提取人脸,并将其与原始照片进行比较

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)

这导致操作数不能与形状(30882316,3)(90,89,3)一起广播的错误。当我从集体照片中取出脸,用PIL保存它们,然后尝试将它们传递到deepface时,我也会遇到同样的错误。有人能推荐实现此功能的其他方法吗?或者修复我当前的尝试?非常感谢你


Tags: 方法rightimgtop错误left照片face
1条回答
网友
1楼 · 发布于 2024-09-27 21:26:22

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"])

所以,你可以将deepface改编成这样的案例

相关问题 更多 >

    热门问题