user1966460
user1966460

Reputation: 21

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

I have implemented a simple face recognition solution.

Inputs

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

Upvotes: 0

Views: 79

Answers (1)

Nitin Ashutosh
Nitin Ashutosh

Reputation: 131

Are you running face-recognition on GPU? In GPU it almost take 5-10 msec.

Try run facial recognition on GPU using dlib==19.24, below I am sharing Dockerfile to build dlib on GPU. After building dlib u can use this as base docker image and u can install face-recognition on this base image.

I have spent year on this recognition, happy to help :)

FROM nvidia/cuda:12.0.0-cudnn8-devel-ubuntu22.04
# Set environment variables for GPU acceleration
ENV CUDA_HOME /usr/local/cuda
ENV LD_LIBRARY_PATH \ $LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64
ENV PATH $PATH:/usr/local/cuda/bin
# Install Python 3.10 and required dependencies
RUN apt-get update && apt-get install -y python3  python3-dev python3-pip
RUN apt install -y \
cmake \
build-essential \
libopenblas-dev \
libjpeg-dev \
libpng-dev \
libx11-dev \
libgtk-3-dev \
&& rm -rf /var/lib/apt/lists/*

# Install dlib and its dependencies
RUN pip3 install dlib==19.24.0

# Set the working directory
WORKDIR /app

# Copy your application code into the image
COPY . /app

# Run your application (replace "your_script.py" with your 
actual Python script)
CMD ["bash"]

Upvotes: 0

Related Questions