32位树莓派4使用face_recognition遇Pillow导入错误求助
问题:32位树莓派4B导入face_recognition触发ImportError
设备与环境
- 硬件:Raspberry Pi 4 Model B Rev 1.4(32位系统)
- Python版本:3.9.0
- 已安装依赖:face_recognition 1.3.0、dlib 19.24.2、Pillow 10.2.0等
报错详情
执行import face_recognition时触发ImportError:
pi@raspberrypi:~ $ python3 Python 3.9.0 (default, Feb 13 2024, 22:18:52) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import face_recognition Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/pi/.local/lib/python3.9/site-packages/face_recognition/__init__.py", line 7, in <module> from .api import load_image_file, face_locations, batch_face_locations, face_landmarks, face_encodings, compare_faces, face_distance File "/home/pi/.local/lib/python3.9/site-packages/face_recognition/api.py", line 3, in <module> import PIL.Image File "/home/pi/.local/lib/python3.9/site-packages/PIL/Image.py", line 84, in <module> from . import _imaging as core ImportError: /home/pi/.local/lib/python3.9/site-packages/PIL/_imaging.cpython-39-arm-linux-gnueabihf.so: undefined symbol: opj_encoder_set_extra_options
已尝试操作
卸载并重新安装Pillow 10.2.0,问题未解决:
pi@raspberrypi:~ $ pip3 uninstall Pillow Found existing installation: pillow 10.2.0 Uninstalling pillow-10.2.0: Would remove: /home/pi/.local/lib/python3.9/site-packages/PIL/* /home/pi/.local/lib/python3.9/site-packages/pillow-10.2.0.dist-info/* Proceed (Y/n)? Y Successfully uninstalled pillow-10.2.0 pi@raspberrypi:~ $ pip3 install Pillow Defaulting to user installation because normal site-packages is not writeable Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Collecting Pillow Using cached https://www.piwheels.org/simple/pillow/pillow-10.2.0-cp39-cp39-linux_armv7l.whl (1.2 MB) Installing collected packages: Pillow Successfully installed Pillow-10.2.0
解决方法
1. 降级Pillow到兼容版本
Pillow 10.x在32位树莓派环境下存在OpenJPEG依赖兼容性问题,降级到9.x稳定版本:
pip3 uninstall -y Pillow pip3 install Pillow==9.5.0
2. 手动编译安装Pillow
如果降级无效,说明系统依赖库与预编译包不匹配,手动编译适配本地环境:
- 安装编译依赖:
sudo apt update && sudo apt install -y libopenjp2-7-dev libtiff5-dev libjpeg62-turbo-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python3-dev
- 卸载现有Pillow:
pip3 uninstall -y Pillow
- 源码编译安装:
pip3 install --no-binary :all: Pillow==9.5.0
3. 修复系统OpenJPEG库
报错中的opj_encoder_set_extra_options属于OpenJPEG库,尝试重新安装系统库:
sudo apt update && sudo apt install --reinstall libopenjp2-7 libopenjp2-7-dev
内容的提问来源于stack exchange,提问作者QG_office
相关产品推荐
相关产品推荐

