Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

face_recognition problem with face_encodings function

I am a newbie and having difficulty on resolving this issue.

What I am trying to do is run the sample code from face_recognition using a webcam. Both of the two example doesn't work on me and keeps on throwing this error.

Traceback (most recent call last):
  File "C:\Users\...\Desktop\face_recognition\demo_webcam.py", line 55, in <module>
    face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\...\AppData\Local\Programs\Python\Python311\Lib\site-packages\face_recognition\api.py", line 214, 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:\Users\...\AppData\Local\Programs\Python\Python311\Lib\site-packages\face_recognition\api.py", line 214, in <listcomp>  
    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_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),numpy.uint8], face: _dlib_pybind11.full_object_detection, num_jitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vector
    2. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),numpy.uint8], num_jitters: int = 0) -> _dlib_pybind11.vector
    3. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),numpy.uint8], faces: _dlib_pybind11.full_object_detections, num_jitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vectors
    4. (self: _dlib_pybind11.face_recognition_model_v1, batch_img: List[numpy.ndarray[(rows,cols,3),numpy.uint8]], batch_faces: List[_dlib_pybind11.full_object_detections], num_jitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vectorss
    5. (self: _dlib_pybind11.face_recognition_model_v1, batch_img: List[numpy.ndarray[(rows,cols,3),numpy.uint8]], num_jitters: int = 0) -> _dlib_pybind11.vectors

Invoked with: <_dlib_pybind11.face_recognition_model_v1 object at 0x000001DAB486B7B0>, array([[[208, 223, 240],
        [204, 213, 234],
        [191, 208, 229],
        ...,
        [ 87,  76,  74],
        [ 94,  78,  77],
        [ 82,  72,  70]],

       [[214, 223, 245],
        [208, 221, 240],
        [197, 217, 235],
        ...,
        [100,  63,  68],
        [104,  74,  71],
        [ 87,  78,  75]],

       [[220, 231, 249],
        [218, 228, 245],
        [208, 224, 239],
        ...,
        [ 96,  86,  84],
        [102,  83,  82],
        [ 86,  83,  80]],

       ...,

       [[ 36,  36,  35],
        [ 41,  37,  36],
        [ 40,  35,  33],
        ...,
        [107,  65,  41],
        [109,  67,  44],
        [109,  68,  50]],

       [[ 41,  39,  38],
        [ 42,  36,  35],
        [ 44,  39,  38],
        ...,
        [107,  64,  45],
        [106,  62,  42],
        [108,  64,  42]],

       [[ 46,  42,  41],
        [ 45,  39,  38],
        [ 43,  38,  36],
        ...,
        [104,  67,  51],
        [100,  64,  46],
        [108,  64,  40]]], dtype=uint8), <_dlib_pybind11.full_object_detection object at 0x000001DAB4EF0370>, 1 

I am using Python 3.11.2 running on Windows 11. I have face_recognition v1.3.0 and dlib v19.24.1.

I already tried reinstalling everything based on the installation guide from face_recognition.

like image 458
Bukayo Avatar asked Aug 23 '26 16:08

Bukayo


2 Answers

Please replace the code line,

rgb_small_frame = frame_process[:, :, ::-1]

with below code line,

rgb_small_frame = numpy.ascontiguousarray(frame_process[:, :, ::-1])
like image 113
Sahil Bandar Avatar answered Aug 26 '26 23:08

Sahil Bandar


Load the reference face image:

reference_image = face_recognition.load_image_file("/home/computer/Imagens/profile.jpeg")
reference_encoding = face_recognition.face_encodings(reference_image)[0]

Open a connection to the webcam (you may need to adjust the index based on your system):

cap = cv2.VideoCapture(0)

Video capture read loop:

while True:
    ret, frame = cap.read()

Convert the frame to RGB for face recognition:

    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

Perform face detection:

    face_locations = face_recognition.face_locations(rgb_frame)
    face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

Compare the current face with the reference face:

    for face_encoding in face_encodings:
        results = face_recognition.compare_faces([reference_encoding], face_encoding)
        name = "Match" if True in results else "No Match"
...
like image 40
Guilherme Avatar answered Aug 26 '26 22:08

Guilherme



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!