I am trying to access my webcam through OpenCV in WSL2 (Ubuntu). I found this blog post where it explains how to connect USB devices to WSL2. Running usbipd wsl list
in windows command prompt lists the following devices:
> usbipd wsl listBUSID VID:PID DEVICE STATE1-1 0c45:6725 Integrated Webcam Not attached1-2 046d:c534 USB Input Device Not attached1-3 0cf3:e007 Qualcomm QCA61x4A Bluetooth Not attached1-4 27c6:639c Goodix Moc Fingerprint Not attached2-2 8564:7000 USB Mass Storage Device Not attached
Here my machine's integrated webcam is a USB device with BUSID 1-1
. I ran usbipd wsl attach --busid 1-1
to connect my integrated webcam to WSL.
Now from WSL, typing lsusb
lists the devices as follows:
> lsusbBus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hubBus 001 Device 003: ID 0c45:6725 Microdia Integrated_Webcam_HDBus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
It looks like the integrated webcam has been successfully connected to WSL (listed as Microdia Integrated_Webcam_HD
).
This is the code used to access the webcam:
import cv2import syssource = cv2.VideoCapture(0)win_name = 'Camera Preview'cv2.namedWindow(win_name, cv2.WINDOW_NORMAL)while cv2.waitKey(1) != 27: # Escape has_frame, frame = source.read() if not has_frame: break cv2.imshow(win_name, frame)source.release()cv2.destroyWindow(win_name)
Here cv2.VideoCapture()
takes an index as argument. The OpenCV documentation mentions:
If I run the code above with 0
as the argument, I get the following output:
[ WARN:0@0.216] global /io/opencv/modules/videoio/src/cap_v4l.cpp (889) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
I have connected USB devices to WSL, but still I am not able to access the integrated webcam. Any ideas how to get around this problem would be much appreciated!