树莓派安装指定TensorFlow依赖遇无头模式Qt插件错误求助
树莓派AI项目依赖安装与无头运行问题解决
问题描述
- 为客户AI项目在树莓派上安装指定版本TensorFlow及相关依赖,纠结两种实现方案:
- 在树莓派上编译所有内容
- 将脚本和模型转换为TF Lite
- 项目依赖列表:
absl-py==1.4.0 appdirs==1.4.4 astunparse==1.6.3 audioread==3.0.0 cachetools==5.3.1 certifi==2023.5.7 cffi==1.15.1 charset-normalizer==3.1.0 colorama==0.4.6 contourpy==1.1.0 cycler==0.11.0 Cython==0.29.35 decorator==5.1.1 filelock==3.12.2 flatbuffers==23.5.26 fonttools==4.40.0 gast==0.4.0 google-auth==2.21.0 google-auth-oauthlib==1.0.0 google-pasta==0.2.0 grpcio==1.56.0 h5py==3.9.0 idna==3.4 importlib-metadata==6.7.0 importlib-resources==5.12.0 Jinja2==3.1.2 joblib==1.3.1 keras==2.13.1 kiwisolver==1.4.4 lapx==0.5.2.post1 lazy_loader==0.3 libclang==16.0.0 librosa==0.10.0.post2 llvmlite==0.40.1 lxml==4.9.3 Markdown==3.4.3 MarkupSafe==2.1.3 matplotlib==3.7.1 mpmath==1.3.0 msgpack==1.0.5 networkx==3.1 numba==0.57.1 numpy==1.24.3 oauthlib==3.2.2 opencv-contrib-python==4.8.0.74 opt-einsum==3.3.0 packaging==23.1 pandas==2.0.3 Pillow==10.0.0 pooch==1.6.0 protobuf==4.23.4 psutil==5.9.5 pyasn1==0.5.0 pyasn1-modules==0.3.0 PyAudio==0.2.13 pycparser==2.21 pyparsing==3.1.0 python-dateutil==2.8.2 python-docx==0.8.11 pytz==2023.3 PyYAML==6.0 requests==2.31.0 requests-oauthlib==1.3.1 resampy==0.4.2 rsa==4.9 scikit-learn==1.3.0 scipy==1.11.1 seaborn==0.12.2 six==1.16.0 sounddevice==0.4.6 soundfile==0.12.1 soxr==0.3.5 SpeechRecognition==3.10.0 supervision==0.11.1 sympy==1.12 tensorboard==2.13.0 tensorboard-data-server==0.7.1 tensorflow==2.13.0 tensorflow-estimator==2.13.0 tensorflow-hub==0.13.0 tensorflow-intel==2.13.0 tensorflow-io-gcs-filesystem==0.31.0 termcolor==2.3.0 threadpoolctl==3.1.0 torch==2.0.1 torchvision==0.15.2 tqdm==4.65.0 typing_extensions==4.5.0 tzdata==2023.3 ultralytics==8.0.125 urllib3==1.26.16 Werkzeug==2.3.6 wrapt==1.15.0 zipp==3.15.0
- 依赖安装完成后运行脚本,出现如下错误:
Got keys from plugin meta data ("xcb") QFactoryLoader::QFactoryLoader() checking directory path "/usr/bin/platforms" ... loaded library "/home/n3lson/.local/lib/python3.9/site-packages/cv2/qt/plugins/platforms/libqxcb.so" qt.qpa.xcb: could not connect to display qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/n3lson/.local/lib/python3.9/site-packages/cv2/qt/plugins" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: xcb.
- 树莓派为无头运行模式,需解决上述错误并明确方案选择。
解决方案
一、解决Qt平台插件初始化错误(无头模式)
树莓派无头运行无图形界面,OpenCV尝试加载Qt的xcb插件失败,可通过以下两种方式解决:
- 强制OpenCV使用无屏幕后端
在脚本开头添加环境变量配置:
或启动脚本时通过命令行指定:import os os.environ['QT_QPA_PLATFORM'] = 'offscreen'QT_QPA_PLATFORM=offscreen python your_script.py - 安装缺失的xcb系统依赖
若需保留图形相关功能,执行以下命令补全依赖:sudo apt-get update sudo apt-get install libxcb-xinerama0 libxcb-randr0 libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-render-util0 libxcb-shape0 libxcb-xkb1
二、TensorFlow安装方案选择
- 不推荐在树莓派上编译所有内容
树莓派ARM架构性能有限,编译TensorFlow及大量依赖耗时极长,且易出现编译错误,成功率低,后续维护成本高。 - 优先选择转换为TF Lite
TF Lite专为嵌入式设备优化,体积更小、运行效率更高,适配树莓派这类低功耗设备。可直接使用预编译的TF Lite包,无需复杂编译。将原标准TensorFlow模型通过官方转换工具转为.tflite格式后,修改脚本适配TF Lite的API即可。 - 若必须使用标准TensorFlow
不要手动逐个安装依赖,直接使用树莓派适配的预编译TensorFlow whl包,能有效避免依赖版本冲突问题。
内容的提问来源于stack exchange,提问作者Aionly
相关产品推荐
相关产品推荐

