face_recognition and black&white images

Question:

I’m using face_recognition library to recognise persons on images.
According to the library documentation, it supports two input image formats for further processing: RGB (8 bit, 3 channels) and L (black and white).

I tried to use

face_recognition.api.load_image_file(file, mode='RGB')

and it's ok. But I need to use L-mode and that's the point. Problem is that mode='RGB' generates numpy.array (x,y,3) and mode='L' generates numpy.array(x,y).

Array should be input later to face_recognition.face_locations and face_recognition.face_encodings functions.

If we put array, which was generated in L-mode to face_encodings, we get the following error:


    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

Any ideas, how should I use this library for black&white images to obtain 128-dimensional facemaps?

Full listing that throws an error (you can use any person's image as 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)

Traceback:

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

File "C:ProgramDataAnaconda3libsite-packagesface_recognitionapi.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:ProgramDataAnaconda3libsite-packagesface_recognitionapi.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

Asked By: SmartWaddles

||

Answers:

According to the error message it looks like it doesn't accept single channel images. You need an ndarray of (rows, cols, 3). You could try to pass in image.repeat(3, 2) which will just repeat the L values three times.

face_encodings = face_recognition.face_encodings(image.repeat(3, 2), face_locations)
Answered By: Håken Lid