I'm beginning to explore face recognition but even my simple "hello world" is blowing up on my face.
Here's the code:
import face_recognition
from PIL import Image
import numpy as np
import base64
from io import BytesIO
def test_face_recognition(image_path):
# Open the image and ensure it's in RGB format
img = Image.open(image_path).convert('RGB')
# Convert image to numpy array
img_np = np.array(img)
# Check face detection
face_locations = face_recognition.face_locations(img_np)
print(f"Faces detected: {len(face_locations)}")
if len(face_locations) > 0:
face_encodings = face_recognition.face_encodings(img_np, known_face_locations=face_locations)
print(f"Encodings found: {len(face_encodings)}")
else:
print("No faces detected.")
# Replace with the path to a local image file
test_face_recognition("test_image.jpg")
The test_image.jpg
can be any JPEG file with a face on it.
When run, the code above explodes with the following error message:
Traceback (most recent call last):
File "C:\Users\User\source\repos\ballot-box\flask\test_face_recognition.py", line 25, in <module>
test_face_recognition("test_image.jpg")
File "C:\Users\User\source\repos\ballot-box\flask\test_face_recognition.py", line 15, in test_face_recognition
face_locations = face_recognition.face_locations(img_np)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\source\repos\ballot-box\flask\venv\Lib\site-packages\face_recognition\api.py", line 121, in face_locations
return [_trim_css_to_bounds(_rect_to_css(face), img.shape) for face in _raw_face_locations(img, number_of_times_to_upsample, model)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\source\repos\ballot-box\flask\venv\Lib\site-packages\face_recognition\api.py", line 105, in _raw_face_locations
return face_detector(img, number_of_times_to_upsample)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Unsupported image type, must be 8bit gray or RGB image.
According to the error message, the image is in an invalid format but, before passing the face recognition function, I do convert to the appropriated format: img = Image.open(image_path).convert('RGB')
Edited:
I changed from img_np to img to no avail. Now it explodes with the following error:
Traceback (most recent call last):
File "C:\Users\PauloSantos\source\repos\ballot-box\flask\test_face.py", line 25, in <module>
test_face_recognition("test_image.jpg")
File "C:\Users\PauloSantos\source\repos\ballot-box\flask\test_face.py", line 15, in test_face_recognition
face_locations = face_recognition.face_locations(img)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\PauloSantos\source\repos\ballot-box\flask\venv\Lib\site-packages\face_recognition\api.py", line 121, in face_locations
return [_trim_css_to_bounds(_rect_to_css(face), img.shape) for face in _raw_face_locations(img, number_of_times_to_upsample, model)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\PauloSantos\source\repos\ballot-box\flask\venv\Lib\site-packages\face_recognition\api.py", line 105, in _raw_face_locations
return face_detector(img, number_of_times_to_upsample)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: __call__(): incompatible function arguments. The following argument types are supported:
1. (self: _dlib_pybind11.fhog_object_detector, image: numpy.ndarray, upsample_num_times: int = 0) -> _dlib_pybind11.rectangles
Invoked with: <_dlib_pybind11.fhog_object_detector object at 0x000001A8DB8E0370>, <PIL.Image.Image image mode=RGB size=1280x720 at 0x1A8FBD47110>, 1
Solved
The problem is the numpy
version 2.0. According to this answer the problem occurs due to a breaking change on numpy
. Reverting to numpy
1.26.4 solved the problem.
The problem is the numpy
version 2.0. According to this answer the problem occurs due to a breaking change on numpy
. Reverting to numpy
1.26.4 solved the problem.