【问题标题】:face recognition library doesn't recognize faces that is detected by the library人脸识别库不识别库检测到的人脸
【发布时间】:2022-06-24 22:15:10
【问题描述】:

感谢ageitgey,我可以立即尝试face recognition。但是,我遇到了一个问题。库检测到的人脸无法被库识别。

我试过的是:

  1. 通过face_detection检测到图像中的人脸并保存:
from PIL import Image
import face_recognition

# Load the jpg file into a numpy array
image = face_recognition.load_image_file("biden.jpg")

# Find all the faces in the image using the default HOG-based model.
# This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
# See also: find_faces_in_picture_cnn.py
face_locations = face_recognition.face_locations(image)

print("I found {} face(s) in this photograph.".format(len(face_locations)))

for face_location in face_locations:

    # Print the location of each face in this image
    top, right, bottom, left = face_location
    print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

    # You can access the actual face itself like this:
    face_image = image[top:bottom, left:right]
    pil_image = Image.fromarray(face_image)
    pil_image.show()
  1. 对保存的图片执行face_recognition
mport face_recognition

# Load the jpg files into numpy arrays
biden_image = face_recognition.load_image_file("biden.jpg")
obama_image = face_recognition.load_image_file("obama.jpg")
unknown_image = face_recognition.load_image_file("obama2.jpg")

# Get the face encodings for each face in each image file
# Since there could be more than one face in each image, it returns a list of encodings.
# But since I know each image only has one face, I only care about the first encoding in each image, so I grab index 0.
try:
    biden_face_encoding = face_recognition.face_encodings(biden_image)[0]
    obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
    unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
except IndexError:
    print("I wasn't able to locate any faces in at least one of the images. Check the image files. Aborting...")
    quit()

known_faces = [
    biden_face_encoding,
    obama_face_encoding
]

# results is an array of True/False telling if the unknown face matched anyone in the known_faces array
results = face_recognition.compare_faces(known_faces, unknown_face_encoding)

print("Is the unknown face a picture of Biden? {}".format(results[0]))
print("Is the unknown face a picture of Obama? {}".format(results[1]))
print("Is the unknown face a new person that we've never seen before? {}".format(not True in results))

因此,它在图像中不返回任何人脸...

有人知道怎么解决吗?

【问题讨论】:

    标签: python face-recognition face-detection


    【解决方案1】:

    尝试检查图像是否真的在这里加载:

    biden_image = face_recognition.load_image_file("biden.jpg")
    obama_image = face_recognition.load_image_file("obama.jpg")
    unknown_image = face_recognition.load_image_file("obama2.jpg")
    

    然后你真的在这里得到了面部编码:

    biden_face_encoding = face_recognition.face_encodings(biden_image)[0]   
    obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
    unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
    

    这是我现在可以在您的代码中看到的唯一“瓶颈”。

    【讨论】:

    • 博丹,非常感谢。很抱歉这么晚才回复。
    • 经过测试,我发现这个库和代码工作正常;然而,尽管它可以很好地检测到任何面孔,但它不能很好地识别亚洲人的面孔(我是亚洲人)。我想我需要训练模型。目前我不知道该怎么做..
    【解决方案2】:

    你可以看看YooniK Face,这是一个用于人脸识别的python包,由 YooniK 提供。它非常易于使用,并且算法具有非常高的准确性。您可以获得免费订阅(每月 1000 次通话)here

    【讨论】: