问题描述

树莓派5调用摄像头显示图形窗口报错FATAL:exception not rethrown_OpenCV

YOLOv5  2025-1-13 Python-3.8.20 torch-2.4.1 CPU

Fusing layers... 
Model summary: 224 layers, 7266973 parameters, 0 gradients
WARNING  Environment does not support cv2.imshow() or PIL Image.show()
OpenCV(4.10.0) /io/opencv/modules/highgui/src/window.cpp:1301: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'

[ WARN:0@35.004] global cap_ffmpeg_impl.hpp:453 _opencv_ffmpeg_interrupt_callback Stream timeout triggered after 30014.057493 ms
1/1: tcp://127.0.0.1:8888...  Success (inf frames 640x480 at 30.00 FPS)

Traceback (most recent call last):
  File "detect.py", line 438, in <module>
    main(opt)
  File "detect.py", line 433, in main
    run(**vars(opt))
  File "/home/Pi/miniconda3/envs/yolov5/lib/python3.8/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
  File "detect.py", line 185, in run
    for path, im, im0s, vid_cap, s in dataset:
  File "/home/Pi/Desktop/yolov5/yolov5-master/utils/dataloaders.py", line 510, in __next__
    if not all(x.is_alive() for x in self.threads) or cv2.waitKey(1) == ord("q"):  # q to quit
cv2.error: OpenCV(4.10.0) /io/opencv/modules/highgui/src/window.cpp:1367: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvWaitKey'  


解决方案:

1. 安装 GTK 支持库

如果你的环境没有安装 GTK,OpenCV 可能无法显示图像。你可以尝试安装 GTK 库:

sudo apt-get update
sudo apt-get install libgtk2.0-dev pkg-config

然后重新编译 OpenCV。如果你使用的是 opencv-python,你可能需要重新安装,确保 OpenCV 能够支持图形显示::

pip uninstall opencv-python
pip install opencv-python


2. 设置图形显示环境

如果你在树莓派的图形界面中运行程序,但仍然遇到问题,尝试设置环境变量以确保正确的图形环境:

export QT_QPA_PLATFORM=xcb

然后重启树莓派,应该就可以啦