You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WSL2中使用OpenCV显示USB摄像头的帧?

WSL2下OpenCV无法捕获USB摄像头帧的问题

测试代码

import cv2

cap = cv2.VideoCapture('/dev/video0')

while True:
    ret, frame = cap.read()
    if not ret:
        continue
    cv2.imshow('usb cam test', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

报错情况

情况1:使用/dev/video0作为设备路径

运行代码后摄像头已打开但无画面,按下Ctrl+C后出现以下报错:

[ WARN:0] global /home/pc/opencv_build/opencv/modules/videoio/src/cap_gstreamer.cpp (1758) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module source reported: Could not read from resource.
[ WARN:0] global /home/pc/opencv_build/opencv/modules/videoio/src/cap_gstreamer.cpp (888) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global /home/pc/opencv_build/opencv/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ WARN:0] global /home/pc/opencv_build/opencv/modules/videoio/src/cap_v4l.cpp (998) tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.

情况2:使用索引0初始化VideoCapture

修改代码为cap = cv2.VideoCapture(0)后运行,摄像头已打开,但出现以下报错:

(python3:736): GStreamer-CRITICAL **: 12:20:01.674:
Trying to dispose element pipeline0, but it is in PAUSED instead of the NULL state.
You need to explicitly set elements to the NULL state before
dropping the final reference, to allow them to clean up.
This problem may also be caused by a refcounting bug in the
application or some element.

[ WARN:0] global /home/pc/opencv_build/opencv/modules/videoio/src/cap_gstreamer.cpp (888) open OpenCV | GStreamer warning: unable to start pipeline

(python3:736): GStreamer-CRITICAL **: 12:20:01.675:
Trying to dispose element videoconvert0, but it is in PLAYING instead of the NULL state.
You need to explicitly set elements to the NULL state before
dropping the final reference, to allow them to clean up.
This problem may also be caused by a refcounting bug in the
application or some element.

[ WARN:0] global /home/pc/opencv_build/opencv/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

(python3:736): GStreamer-CRITICAL **: 12:20:01.675:
Trying to dispose element appsink0, but it is in READY instead of the NULL state.
You need to explicitly set elements to the NULL state before
dropping the final reference, to allow them to clean up.
This problem may also be caused by a refcounting bug in the
application or some element.

[ WARN:0] global /home/pc/opencv_build/opencv/modules/videoio/src/cap_v4l.cpp (887) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
Traceback (most recent call last):
  File "usbcam_test.py", line 5, in <module>
    cap = cv2.VideoCapture(0)
KeyboardInterrupt

已执行的排查操作

  • USBIPD配置:安装usbipd-win,绑定并将USB摄像头附加到WSL,usbipd list显示摄像头处于Attached状态。
  • 开启WSL内核摄像头支持:按步骤配置WSL内核,当前内核版本为5.15.90.1-microsoft-standard-WSL2+ #7 SMP Wed Mar 13 15:17:47 +03 2024。
  • 设置/dev/video权限:执行sudo chmod 777 /dev/video*,ls -l /dev显示video0和video1权限为crwxrwxrwx。
  • Windows环境验证:在Windows上运行相同代码(使用VideoCapture(0))可正常显示摄像头帧。

寻求解决WSL2中无法显示USB摄像头帧的方法。


内容的提问来源于stack exchange,提问作者mericgeren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 13:27:08