face-recognition示例程序检测到人脸时崩溃问题求助
face-recognition检测人脸时程序崩溃的问题
运行face-recognition库的官方示例程序时,直播视频可正常显示,但人脸进入画面后程序立即崩溃,Windows 10和Ubuntu系统测试均出现相同错误。
示例代码
import face_recognition import cv2 import numpy as np # 获取默认摄像头(编号0)的引用 video_capture = cv2.VideoCapture(0) # 加载样本图片并学习识别 obama_image = face_recognition.load_image_file("obama.jpg") obama_face_encoding = face_recognition.face_encodings(obama_image)[0] # 加载第二张样本图片并学习识别 biden_image = face_recognition.load_image_file("biden.jpg") biden_face_encoding = face_recognition.face_encodings(biden_image)[0] # 创建已知人脸编码和对应名称的数组 known_face_encodings = [ obama_face_encoding, biden_face_encoding ] known_face_names = [ "Barack Obama", "Joe Biden" ] # 初始化变量 face_locations = [] face_encodings = [] face_names = [] process_this_frame = True while True: # 获取一帧视频 ret, frame = video_capture.read() # 每隔一帧处理一次以节省时间 if process_this_frame: # 将视频帧缩小到1/4尺寸,加快人脸识别速度 small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) # 将OpenCV的BGR格式转换为face-recognition需要的RGB格式 rgb_small_frame = small_frame[:, :, ::-1] # 查找当前视频帧中的所有人脸和人脸编码 face_locations = face_recognition.face_locations(rgb_small_frame) face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations) face_names = [] for face_encoding in face_encodings: # 检查人脸是否与已知人脸匹配 matches = face_recognition.compare_faces(known_face_encodings, face_encoding) name = "Unknown" # 选择与当前人脸距离最小的已知人脸作为最佳匹配 face_distances = face_recognition.face_distance(known_face_encodings, face_encoding) best_match_index = np.argmin(face_distances) if matches[best_match_index]: name = known_face_names[best_match_index] face_names.append(name) process_this_frame = not process_this_frame # 显示识别结果 for (top, right, bottom, left), name in zip(face_locations, face_names): # 将人脸位置缩放回原帧尺寸 top *= 4 right *= 4 bottom *= 4 left *= 4 # 绘制人脸边框 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) # 绘制姓名标签 cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1) # 显示视频画面 cv2.imshow('Video', frame) # 按下q键退出程序 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头资源 video_capture.release() cv2.destroyAllWindows()
报错信息
Traceback (most recent call last): File ".\main.py", line 55, in <module> face_encodings = face_recognition.face_encodings(rgb_small_frame, face_loca File "C:\Users\markc\AppData\Local\Programs\Python\Python38\lib\site-packages return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landm File "C:\Users\markc\AppData\Local\Programs\Python\Python38\lib\site-packages return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landm TypeError: compute_face_descriptor(): incompatible function arguments. The foll 1. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rowjitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vector 2. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(row 3. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rowm_jitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vectors 4. (self: _dlib_pybind11.face_recognition_model_v1, batch_img: List[numpy.nl_object_detections], num_jitters: int = 0, padding: float = 0.25) -> _dlib_pyb 5. (self: _dlib_pybind11.face_recognition_model_v1, batch_img: List[numpy.nd11.vectors
已安装库版本
- dlib==19.24.1
- face-recognition==1.3.0
- face-recognition-models==0.3.0
- numpy==1.24.2
- opencv-python==4.7.0.72
解决方案
核心问题是版本不兼容:numpy 1.24+移除了旧版dlib/face-recognition依赖的API,导致compute_face_descriptor调用参数不匹配。
方法1:降级numpy到兼容版本
执行以下命令降级numpy,这是最稳妥的解决方式:
pip install numpy==1.23.5
方法2:升级face-recognition到最新版本(可选)
如果不想降级numpy,可以尝试升级face-recognition到最新版(需确保dlib版本同步兼容,部分环境可能需要重新编译dlib):
pip install --upgrade face-recognition
内容的提问来源于stack exchange,提问作者MichaelLong102
相关产品推荐
相关产品推荐

