使用正确图片时人脸识别测试失败问题排查
解决face_recognition库与numpy 2.0版本不兼容的图像格式错误
问题场景
刚探索人脸识别技术,运行入门测试代码时出现异常,测试代码如下:
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")
测试图片为含有人脸的JPEG文件,代码中已通过img = Image.open(image_path).convert('RGB')将图片转为RGB格式,但仍报错。
错误信息
初始错误
运行代码后抛出如下错误:
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.
修改参数后的错误
将传入face_recognition.face_locations()的参数从img_np改为img后,出现新错误:
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
解决方法
问题源于numpy 2.0版本的破坏性更新,导致其与face_recognition库不兼容。执行以下命令将numpy降级至1.26.4即可解决问题:
pip install numpy==1.26.4
内容的提问来源于stack exchange,提问作者Paulo Santos
相关产品推荐
相关产品推荐

