问题描述:

树莓派调用摄像头显示图形窗口报错:xcb  Could not load the Qt_Qt

(yolov5) Pi@raspberrypi:~/Desktop/yolov5/yolov5-master $ python detect.py --source 0 --weights yolov5n.pt
detect: weights=['yolov5n.pt'], source=0, data=data/coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_format=0, save_csv=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False, vid_stride=1
YOLOv5  2025-1-13 Python-3.8.20 torch-2.4.1 CPU

Fusing layers... 
YOLOv5n summary: 213 layers, 1867405 parameters, 0 gradients, 4.5 GFLOPs
qt.qpa.plugin: Could not load the Qt platform plugin "wayland" in "/home/Pi/miniconda3/envs/yolov5/lib/python3.8/site-packages/cv2/qt/plugins" even though it was found.
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/Pi/miniconda3/envs/yolov5/lib/python3.8/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: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, xcb.

已放弃


解决方案:

首先,可以先尝试安装pyside6和pyqt5库

pip install pyside6
pip install pyqt5

如果不行,继续执行下方的猛操作

1. 安装缺失的 Qt 插件

尝试安装缺失的 Qt 插件,尤其是 xcb 插件:

sudo apt-get install libxcb-xinerama0

2. 设置环境变量

尝试设置 QT_QPA_PLATFORM 环境变量为 offscreen,这可能会解决没有显示输出设备时的错误:

export QT_QPA_PLATFORM=offscreen

你可以将这个命令添加到你的 .bashrc.profile 文件中,确保每次启动时都自动设置。

3. 使用 cv2 的 Headless 模式

如果你不需要显示图形界面,可以尝试安装 opencv-python-headless,它是一个不包含 GUI 模块的 OpenCV 版本:

pip install opencv-python-headless

4. 重新安装 OpenCV 和 Qt

如果上述方法无效,可以尝试重新安装 OpenCV 和 Qt:

pip uninstall opencv-python opencv-python-headless
pip install opencv-python

然后重启开发板,尝试应该可以了。


补充:

解决完上方的报错,估计会有新的报错,可以查看这篇文章:https://blog.51cto.com/wzqgzs/13104709

树莓派调用摄像头显示图形窗口报错:xcb  Could not load the Qt_Qt_02