2

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 list
BUSID  VID:PID    DEVICE                                                        STATE
1-1    0c45:6725  Integrated Webcam                                             Not attached
1-2    046d:c534  USB Input Device                                              Not attached
1-3    0cf3:e007  Qualcomm QCA61x4A Bluetooth                                   Not attached
1-4    27c6:639c  Goodix Moc Fingerprint                                        Not attached
2-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:

> lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 0c45:6725 Microdia Integrated_Webcam_HD
Bus 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 cv2
import sys

source = 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: doc

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!

Terrarium
  • 293
  • 1
  • 3
  • 9
  • Do you see any video devices in /dev (`ls /dev/video?`) ? or anything like `/sys/class/video4lan`? – steeldriver May 01 '22 at 21:49
  • @steeldriver Nope, I ran `ls -R /dev | grep video` and `ls -R /sys | grep video`. Nothing there, but I see `001` and `003` under `ls /dev/bus/usb/001`. `003` is the device id of the integrated webcam – Terrarium May 01 '22 at 22:09
  • 1
    I can reproduce this, but I don't have a solution yet. The WSL2 kernel does not have any media device drivers configured, so at the least, you'll need to [build your own kernel](https://github.com/microsoft/WSL2-Linux-Kernel). I've made progress by building with the UVC drivers (which also appear to support the Microdia). But I'm still missing something - I can get the camera to turn on when I run `source = cv2.VideoCapture(0)`, but no further. `ffmpeg` also starts recording, but doesn't actually capture anything. I'll try to get a write-up of my progress in a "partial answer", at least. – NotTheDr01ds May 03 '22 at 22:09
  • @NotTheDr01ds did you manage to find a solution to this? – Xavier Hubbard Anderson Jun 16 '22 at 19:03
  • @XavierHubbardAnderson Unfortunately, no. And I gave it another try just a few days ago when I saw [this Ask Ubuntu question](https://askubuntu.com/q/1413377/1165986). However, I did come across [this](https://github.com/PINTO0309/wsl2_linux_kernel_usbcam_enable_conf) which seems to indicate that it is working for someone. I wasn't able to replicate it, but I didn't try dropping back to the 5.10.60.1 kernel referenced there, either. – NotTheDr01ds Jun 16 '22 at 19:18
  • @XavierHubbardAnderson Ah, also a write-up of what I tried in [this Stack Overflow answer](https://stackoverflow.com/a/72259612/11810933). – NotTheDr01ds Jun 16 '22 at 19:21

0 Answers0