Ubuntu与RaspbianOS下调用face_recognition.face_encodings参数不匹配报错
解决Linux下face_recognition传入face_locations时的TypeError问题
这个报错的核心原因是Linux环境下dlib对人脸坐标的类型要求更严格,Windows会自动兼容浮点型坐标,但Linux必须使用整数类型的坐标元组。以下是具体解决步骤:
1. 强制转换人脸坐标为整数类型
检查你的face_locations列表中的每个坐标元组(格式为(top, right, bottom, left)),确保所有值都是整数。如果是浮点型(比如通过其他人脸检测工具得到的坐标),直接强制转换:
# 假设face_locs是你得到的人脸坐标列表 face_locs = [(int(top), int(right), int(bottom), int(left)) for (top, right, bottom, left) in face_locs] # 再传入face_encodings或compute_face_descriptor encodings = face_recognition.face_encodings(img, known_face_locations=face_locs)
2. 重新编译安装dlib
Linux下dlib是源码编译安装的,如果编译过程中依赖缺失或环境不匹配,可能导致类型校验异常。按以下步骤重新安装:
- 卸载现有dlib:
pip uninstall -y dlib - 安装编译依赖(Ubuntu/Raspbian通用):
sudo apt-get update && sudo apt-get install build-essential cmake libopenblas-dev liblapack-dev libx11-dev libgtk-3-dev python3-dev -y - 重新安装指定版本的dlib:
pip install dlib==19.24.1
3. 确保face_locations参数格式正确
确认传入的是坐标元组的列表,而非单个元组。如果仅检测到一张人脸,也要包成列表形式:
# 错误示例:直接传单个元组 face_recognition.face_encodings(img, known_face_locations=(top, right, bottom, left)) # 正确示例:包成列表 face_recognition.face_encodings(img, known_face_locations=[(top, right, bottom, left)])
内容的提问来源于stack exchange,提问作者Sahil Bandar
相关产品推荐
相关产品推荐

