[[Solved]] Face recognition test failing with correct image [duplicate]

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:UsersUsersourcereposballot-boxflasktest_face_recognition.py", line 25, in <module>
    test_face_recognition("test_image.jpg")
  File "C:UsersUsersourcereposballot-boxflasktest_face_recognition.py", line 15, in test_face_recognition
    face_locations = face_recognition.face_locations(img_np)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersUsersourcereposballot-boxflaskvenvLibsite-packagesface_recognitionapi.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:UsersUsersourcereposballot-boxflaskvenvLibsite-packagesface_recognitionapi.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:UsersPauloSantossourcereposballot-boxflasktest_face.py", line 25, in <module>
    test_face_recognition("test_image.jpg")
  File "C:UsersPauloSantossourcereposballot-boxflasktest_face.py", line 15, in test_face_recognition
    face_locations = face_recognition.face_locations(img)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersPauloSantossourcereposballot-boxflaskvenvLibsite-packagesface_recognitionapi.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:UsersPauloSantossourcereposballot-boxflaskvenvLibsite-packagesface_recognitionapi.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.

5

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật