I am trying to capture and display frames from USB camera with OpenCV in WSL2. Here is the code:
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()
When I try to run this code, I can see that camera has opened. But, nothing happens. Then, if I press ctrl + c I see following output:
(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
When I change cap = cv2.VideoCapture(‘/dev/video0’) with cap = cv2.VideoCapture(0) and try to run the code, I can see the camera has opened and I get the following output:
[ 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.
Notes
- Before these trials I have mentioned, I have done following:
USBIPD
I have installed usbipd-win from here. Then, binded and attached the usb camera with wsl. I see following when enter usbipd list command:
Connected:
BUSID VID:PID DEVICE STATE
1-3 0b05:19b6 USB Input Device Not shared
1-8 322e:2122 USB2.0 HD UVC WebCam Attached
1-14 8087:0033 Intel(R) Wireless Bluetooth(R) Not shared
2- Enabled USB Cameras From WSL Kernel Configuration
I have followed step specified here. When I enter uname -r -v in WSL 2, I see the following output:
5.15.90.1-microsoft-standard-WSL2+ #7 SMP Wed Mar 13 15:17:47 +03 2024
- I set permissions for /dev/video0 and /dev/video1 with sudo chmod 777 /dev/video*. When I enter ls -l /dev on WSL 2 I see the following output:
crwxrwxrwx 1 root root 81, 0 Jun 13 12:54 video0
crwxrwxrwx 1 root root 81, 1 Jun 13 12:54 video1
- When I try to run the same code (version wth VideoCapture(0)) on Windows, I can see frames from usb camera displayed on the screen.