【发布时间】:2021-01-30 21:07:42
【问题描述】:
我正在使用两种方法在 python 中使用 facenet pytorch (https://github.com/timesler/facenet-pytorch) 做一个人脸识别应用程序。
第一种方法 代码 -
resnet = InceptionResnetV1(pretrained='vggface2').eval()
mtcnn = MTCNN(image_size=96)
img = Image.open(image_path)
image_prep = mtcnn(img)
plt.figure()
plt.axis('off')
plt.imshow(image_prep.permute(1, 2, 0))
if image_prep is not None:
image_embedding = resnet(image_prep.unsqueeze(0))
在这段代码中,我从给定的图像中提取人脸,并获得用于识别人脸的 512 个编码。
在这个例子中,我使用了两个不同的面,并绘制了面之间的距离
a.jpg b.jpg
a.jpg 0.000000 1.142466
b.jpg 1.142466 0.000000
效果很好……
第二种方法 代码-
img = Image.open(image)
boxes, probs = mtcnn.detect(img) # Gives the coordinates of the face in the given image
face = img.crop((boxes[0][0], boxes[0][1], boxes[0][2], boxes[0][3])) # Cropping the face
plt.figure()
plt.imshow(face)
pil_to_tensor = transforms.ToTensor()(face).unsqueeze_(0) # Converting to tensor type
image_embedding = resnet(pil_to_tensor)
在这段代码中,我曾经先获取人脸的坐标,然后是嵌入。两张脸之间的距离 -
a.jpg b.jpg
a.jpg 0.000000 0.631094
b.jpg 0.631094 0.000000
在第一种方法中,我直接将图像输入到 mtcnn 并获得更好的结果,两个人脸之间的距离大于 1.0。
在第二种方法中,我使用mtcnn.detect() 获取人脸的坐标,从给定的图像中裁剪人脸,然后输入 resnet。这种方法使两个不同的面之间的距离更小。
然后,我通过在馈送到 resnet 之前绘制结果(人脸)来找到第一种方法比第二种方法执行得更好的原因。
在第二种方法中,我通过使用mtcnn.detect() 裁剪人脸来提供与输入图像(清晰图像)中相同的人脸。
但是,在第一种方法中,我直接将输入提供给mtcnn(img),它返回黑暗中的面部张量而不是输入图像中的张量。这个较暗的图像不是清晰的图像(眼睛周围的区域较暗,我用很多照片测试过),看不清眼睛。这就是原因,第一种方法显示两个人脸之间的距离更大。
我的疑问是,为什么 mtcnn 在黑暗中返回张量,如何解决,帮助我解决这个问题,
谢谢
【问题讨论】:
标签: python pytorch face-recognition facenet