Pytorch Facenet MTCNN Image output



我正在使用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 之前绘制结果(面)来找到第 1 种方法比第 2 种方法表现良好的原因。

在第二种方法中,我通过使用mtcnn.detect()裁剪面部来喂入与输入图像(清晰图像)中给出的相同的面部。

但是,在第一种方法中,我直接将输入提供给mtcnn(img),后者在黑暗中返回面部张量,而不是在输入图像中。这个较暗的图像不是清晰的图像(眼睛周围的区域较暗,我用很多照片测试过),无法清楚地看到眼睛。这就是原因,第一种方法显示两个面之间的距离更高。

我的疑问是,为什么mtcnn在黑暗中返回张量,如何解决它,在这个问题上帮助我,

谢谢

也许 besause 张量返回在 [-1:1] 范围内? 你必须把它转换回uint8 [0-255]。

最新更新