AttributeError: module 'face_recognition' has no attribute 'face_encodings' if tried to use as REST API

33 views Asked by At

I have implemented a simple face recognition solution.

Inputs

  • Reference image
  • Frame to match
    both encoded in base64

In my function to recognize, I decode the reference image and frame to match as below

def decode_base64_image(base64_string):
    image_data = base64.b64decode(base64_string)
    image = Image.open(io.BytesIO(image_data))
    image_array = np.array(image)
    return image_array

def recognize_face(reference_image_base64, frame_to_match_base64):
    # Decode the reference image from base64
    reference_image = decode_base64_image(reference_image_base64)

    # Decode the frame to match from base64
    frame_to_match = decode_base64_image(frame_to_match_base64)

    known_face_encodings = face_recognition.face_encodings(reference_image)
    ...........

Executes fine without any issues.

Tried extending the code as an API, nd call the function as below

# Perform face recognition on the frame
    face_data = recognize_face(reference_image_base64, frame_to_match_base64)

But when I execute the API with flask, and call my test script as below

# Load the reference image and encode it to base64
reference_image_path = 'FileName.jpg'
with open(reference_image_path, 'rb') as f:
    reference_image_bytes = f.read()
reference_image_base64 = base64.b64encode(reference_image_bytes).decode('utf-8')
..............

# Encode the frame to base64
    _, frame_to_match_bytes = cv2.imencode('.jpg', frame)
    frame_to_match_base64 = base64.b64encode(frame_to_match_bytes).decode('utf-8')

    # Prepare the request data
    data = {
        'reference_image': reference_image_base64,
        'frame_to_match': frame_to_match_base64
    }

    # Send the POST request to the API
    response = requests.post('http://localhost:5000/match_face', json=data)
    result = response.json().get('result')

There is error thrown on the API flask console

known_face_encodings = face_recognition.face_encodings(reference_image)
AttributeError: module 'face_recognition' has no attribute 'face_encodings'

Confirmed by adding the below line in both cases (of normal function and an API), that same output is in both cases as 1076

print(f"size of ref image is : {len(reference_image)}")

Am I missing anything in following the procedure or calling sequence? Any suggestions to be tried?

Dev environment -Python 3.10.13 -cv2 4.9.0 -face_recognition 1.3.0

Tried face_recognition library in python as a function - success, Tried the same code as API with some modifications - Failure

0

There are 0 answers