人脸识别和黑白图像

时间:2018-10-10 09:42:37

标签: python python-imaging-library face-recognition dlib

我正在使用face_recognition library来识别图像上的人物。 根据{{​​3}},它支持两种输入图像格式以进行进一步处理:RGB(8位,3通道)和L(黑白)。

我尝试使用

face_recognition.api.load_image_file(file, mode='RGB')
,没关系。但是我需要使用L模式,这就是重点。问题是mode ='RGB'生成numpy.array(x,y,3)而mode ='L'生成numpy.array(x,y)。

稍后应将数组输入face_recognition.face_locations和face_recognition.face_encodings函数。

如果将以L模式生成的数组放在face_encodings中,则会出现以下错误:


    TypeError: compute_face_descriptor(): incompatible function arguments. The following argument types are supported:
        1. (self: dlib.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),uint8], face: dlib.full_object_detection, num_jitters: int=0) -> dlib.vector
        2. (self: dlib.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),uint8], faces: dlib.full_object_detections, num_jitters: int=0) -> dlib.vectors
        3. (self: dlib.face_recognition_model_v1, batch_img: List[numpy.ndarray[(rows,cols,3),uint8]], batch_faces: List[dlib.full_object_detections], num_jitters: int=0) -> dlib.vectorss

有什么想法,我应该如何使用该库处理黑白图像以获得128维人脸图?

引发错误的完整列表(您可以将任何人的图像用作image.jpg):


    import face_recognition
    image = face_recognition.load_image_file('image.jpg', mode='L')
    face_locations = face_recognition.face_locations(image)
    face_encodings = face_recognition.face_encodings(image, face_locations)

跟踪:

File "D:/PythonProjects/face_recognition_grayscale_test.py", line 18, in face_encodings = face_recognition.face_encodings(image, face_locations)

File "C:\ProgramData\Anaconda3\lib\site-packages\face_recognition\api.py", line 200, in face_encodings return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, num_jitters)) for raw_landmark_set in raw_landmarks]

File "C:\ProgramData\Anaconda3\lib\site-packages\face_recognition\api.py", line 200, in return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, num_jitters)) for raw_landmark_set in raw_landmarks]

TypeError: compute_face_descriptor(): incompatible function arguments. The following argument types are supported: 1. (self: dlib.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),uint8], face: dlib.full_object_detection, num_jitters: int=0) -> dlib.vector 2. (self: dlib.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),uint8], faces: dlib.full_object_detections, num_jitters: int=0) -> dlib.vectors 3. (self: dlib.face_recognition_model_v1, batch_img: List[numpy.ndarray[(rows,cols,3),uint8]], batch_faces: List[dlib.full_object_detections], num_jitters: int=0) -> dlib.vectorss

Invoked with: , array([[167, 167, 167, ..., 172, 172, 170], [167, 167, 167, ..., 172, 172, 170], [167, 167, 167, ..., 172, 172, 170], ..., [188, 186, 181, ..., 201, 201, 198], [193, 189, 184, ..., 201, 201, 198], [181, 180, 178, ..., 201, 201, 198]], dtype=uint8), , 1

1 个答案:

答案 0 :(得分:0)

根据错误消息,它似乎不接受单通道图像。您需要一个(行,列,3)的ndarray。您可以尝试传入image.repeat(3, 2),它只会重复L值三次。

face_encodings = face_recognition.face_encodings(image.repeat(3, 2), face_locations)