face_recognition library: TypeError: compute_face_descriptor(): incompatible function arguments

38 Views Asked by At

Working on python 3.10 I made face detection with cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt2.xml')

next I crop image of one face and with face_recognition want to compare face to check is it my or not.

img = cv2.imread('82.png')
detect_faces(img)
def detect_faces(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    face = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt2.xml')
    faces = face.detectMultiScale(img_gray, 1.1, 11)
    # print(faces)
    # print(len(faces))

    for (x, y, w, h) in faces:
        if x < 50: x = 50
        if y < 50: y = 50
        face = img[y - 50:y + h + 50, x - 50:x + w + 50]
        cv2.imshow('ree', face)
        cv2.waitKey()
        cv2.imwrite('84.png', face)
        if recognize_face(face):  # если вырезанное лицо совпадает с моим
            print('Vitya')
        else:
            print('not vitya')


def recognize_face(face):
    data = pickle.loads(open('my_encoding.pickle', 'rb').read())
    face2_img = face_recognition.load_image_file(f'84.png')
    face2_enc = face_recognition.face_encodings(face2_img)[0]
    print('face2_img:', face2_img)
    print('face:', face)
    print('face2_img type:', type(face2_img))  # >> face2_img type: <class 'numpy.ndarray'>
    print('face type:', type(face))  # >> face type: <class 'numpy.ndarray'>
    face_enc = face_recognition.face_encodings(face)[0]
    # print('face2_enc:', face2_enc)
    # print('face_enc:', face_enc)
    result = face_recognition.compare_faces(data['encoding'], face2_enc)
    if result[0]:
        print("Same person!")
    else:
        print("Another person!")

    return result[0]

and after launch I got an error in face_enc = face_recognition.face_encodings(face)[0] and I can't understand why. face is an numpy.ndarray as face2_img = face_recognition.load_image_file(f'84.png'), it's fully same picture and when i'm trying to

print('face2_img:', face2_img)
print('face:', face)

it print me same structure but with some different digits. (can upload it somewhere if needed)

python 3.10.11

face-recognition 1.3.0
face-recognition-models 0.3.0
opencv-python 4.9.0.80

Full error:

File "D:\c0n\projects\everyday-silfie\main.py", line 113, in recognize_face
    face_enc = face_recognition.face_encodings(face)[0]
  File "D:\c0n\projects\everyday-silfie\.venv\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 "D:\c0n\projects\everyday-silfie\.venv\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 0x000001FF4B49A2B0>, array([[[ 11,  11,  11],
        [  9,   9,   9],
        [  4,   4,   4],
        ...,
        [108, 117, 127],
        [111, 120, 131],
        [100, 108, 121]],

       [[ 10,  10,  10],
        [  9,   9,   9],
        [ 14,  14,  14],
        ...,
        [105, 113, 125],
        [103, 111, 124],
        [105, 113, 126]],

       [[  8,   8,   9],
        [  9,   8,  10],
        [  6,   6,   7],
        ...,
        [104, 112, 125],
        [111, 119, 132],
        [106, 114, 127]],

       ...,

       [[ 63,  56,  51],
        [ 58,  51,  49],
        [ 58,  51,  48],
        ...,
        [ 28,  86,  40],
        [ 27,  87,  39],
        [ 22,  83,  36]],

       [[ 68,  61,  58],
        [ 67,  60,  57],
        [ 66,  59,  55],
        ...,
        [ 27,  86,  38],
        [ 30,  90,  42],
        [ 19,  80,  31]],

       [[ 66,  60,  57],
        [ 56,  51,  48],
        [ 59,  53,  50],
        ...,
        [ 24,  85,  36],
        [ 27,  87,  39],
        [ 15,  77,  26]]], dtype=uint8), <_dlib_pybind11.full_object_detection object at 0x000001FF491BF330>, 1
0

There are 0 best solutions below