How should I open remote camera in Windows 10 from WSL2?

Viewed 1739

How should I open remote camera in Windows 10 from WSL2?

I have a Microsoft Surface Book 2 (SB2) for which I can open the camera in Windows 10, but I can't open it in Ubuntu/WSL.

I known that the Windows VScode remote extension can access WSL to open program and edit or compile.

So is it possible to access SB2's camera in by any method?

Why? Because I have a program which recognizes objects and calculates distances using the SB2's camera. (YouTube video demonstration)

But I want to complete it in WSL because development is usually more convenient in Linux.

3 Answers

A late answer since the question got bumped today with a "negative" answer. And there's no problem with a negative answer, since I thought this was the case for a long time. However, I recently came across a possible solution. Note that I haven't tried this myself.

This does assume that the camera is connected via USB, even if internally. I am not sure if that's the case with the Surface Book, but it is likely.

This comes from a post in the Home Assistant forums, where folks have been running Home Assistant (a home automation platform) on WSL.

Since most (all?) PC Z-wave interfaces are via USB dongles, one of the key blockers has been how to utilize a USB Z-wave dongle natively from WSL. I'm not going to repeat the whole comment from those forums, but to summarize (and adapt the instructions for "camera"):

  • Share the USB camera under Windows as a USB/IP device using USB/IP for Windows.
  • uname -a to check your WSL2 kernel version. Microsoft has added support for USB/IP in 5.10.60.1.
  • If you aren't using the latest, download it and set that kernel as the one for WSL2 using .wslconfig. At some point, a wsl --update will probably move you to the latest automatically.
  • sudo modprobe any needed modules for your camera.
  • usbip list -r $HOSTNAME.local to (hopefully) see the list of your Windows USB devices and their associated bus id.
  • usbip attach -r $HOSTNAME.local -b <bus_id> with the bus is of your camera.

With that in place, the WSL/Linux system should see your camera as a native USB device, allowing you access from Python or any other language just as if you were on a native Linux system with a native USB device directly attached.

As a work around you could streams the camera video as MJPEG data over HTTP and receive it in WSL:

1. Install tool to stream webcam on host (e.g. cam2web)

https://github.com/cvsandbox/cam2web

Access the Windows build directly:

http://www.cvsandbox.com/projects/cam2web/v1.1.0.html

2. Receive Stream in WSL

For WSL/WSL1:

2.1 Use (e.g.) ffmpeg to receive stream

Type in the WSL terminal: ffmpeg -i "http://127.0.0.1:8000/camera/mjpeg" -an output.mp4

For WSL2:

2.1 Find IP Adress of host Windows computer

Unfortunately it is not possible to use the localhost address, hence type in the WSL2 terminal:

cat /etc/resolv.conf

The IP address listed as nameserver is the host IP address.

2.2 Use (e.g.) ffmpeg to receive stream

Type in the WSL2 terminal:

ffmpeg -i "http://<host ip address>:8000/camera/mjpeg"-an output.mp4

Exchange <host ip address> with the host IP address from 2.1.

Why the difference between WSL1 and WSL2?

Only WSL1 has direct access to the interfaces of the host computer. For WSL2 the host computer is like another computer in the same network.


Tested:

  • WSL1/2: Ubuntu 20.04
  • Host: Windows Home 10.0.22000
Related