Tensorflow WSL2 — Webcam

Nelson Punch
Software-Dev-Explore
6 min readApr 19, 2024
Photo by Ethan Hoover on Unsplash

Introduction

I wrote an article about setting up Tensorflow with GPU on WSL2 here. Now I am encounter a problem which I am not able to use my webcam. However after a days of search and experiments, I finally got it working.

Here I will write down how I do it step by step.

Attach webcam to WSL2

In order to expose our webcam to WSL2, we need a solution. According to documentation here usbipd-win can help us to overcome this problem.

usbipd-win is a windows software for sharing locally connected USB devices to other machines, including Hyper-V guests and WSL 2.

Following commands are on windows

You can download latest version either from here or use winget on windows.

winget install usbipd

To list connected usb device.

usbipd list

First we need to bind device. Once a device is bind, you don’t need to bind it again. For example I would like to use Integrated Webcam.

Make sure you run your terminal as administrator otherwise it will not allow you to do binding.

usbipd bind -b 1-5

Then you can see its STATE is now Shared.

Next is to attach the device to WSL2.

usbipd attach -w -b 1–5

You need to make sure the distribution in WSL2 is running in order to attach the device to it. The way to do it is to start distribution in another terminal.

Following commands are on WSL2

To check if webcam is attached.

lsusb

Yes it is attached to WSL2.

Build WSL2 Linux Kernel from scratch

In order to grab video from the webcam by attaching a webcam to WSL2 is not enough. At this point of time I am writing the article, the WSL2 kernel version is 5.15.150.1 which doesn’t included proper driver for webcam.

The only way I found to solve this problem is to build a new kernel and include the proper drivers.

Preparation

Following commands are on windows

First update WSL2.

wsl --update

Following commands are on WSL2

Next update and upgrade apt then install necessary dependencies.

sudo apt update && sudo apt upgrade -y && sudo apt install -y build-essential flex bison dwarves libssl-dev l
ibelf-dev libncurses-dev pkg-config

Check your version of WSL2.

uname -r

For me it is 5.15.150.1

Clone WSL2 Linux Kernel repo from here and select the branch that match your version.

# install git if you haven't
sudo apt install -y git

# clone the repo
git clone https://github.com/microsoft/WSL2-Linux-Kernel.git

Configuration for building kernel

Following commands are on WSL2

After it the repo finish downloaded, we need to prepare configuration file to build the kernel.

# go to the folder
cd WSL2-Linux-Kernel/

# copy current system configuration zip file to the folder
sudo cp /proc/config.gz config.gz

# unzip file
sudo gunzip config.gz

# rename unzipped config file to .config
# .config file will be used as default configuration
# file for building kernel
sudo mv config .config

Then we start to configure what drivers we need to be included.

sudo make menuconfig

It will prompt a configuration window.

Make sure you select following drivers.

When you are doing selecting, make sure you select it with * not M.


# Build WSL2 kernel with usb camera support
# menuconfig -> Device Drivers -> Multimedia support -> Filter media drivers
# -> Device Drivers -> Multimedia support -> Media device types -> Cameras and video grabbers
# -> Device Drivers -> Multimedia support -> Video4Linux options -> V4L2 sub-device userspace API
# -> Device Drivers -> Multimedia support -> Media drivers -> Media USB Adapters -> USB Video Class (UVC)
# -> Device Drivers -> Multimedia support -> Media drivers -> Media USB Adapters -> UVC input events device support
# -> Device Drivers -> Multimedia support -> Media drivers -> Media USB Adapters -> GSPCA based webcams

Build kernel

Following commands are on WSL2

After you finish configuration then we can start to build kernel.

# build kernel 
# $(nproc) return number of processors
sudo make -j $(nproc)
sudo make modules_install -j $(nproc)
sudo make install -j$(nproc)

If you encounter bc: not found error like image below, you need to install bc by sudo apt install -y bc then rebuild kernel again.

A little tip here. After build process either completed or fail, there are some files will be generated. In order to make a clean rebuild we can run these commands to clean it up. Then you start from where you copy configuration file step.

sudo make clean
sudo make mrproper
sudo make distclean

Install kernel

Once it is finished, you can see a file name vmlinux in the directory. That is your kernel. We need to copy kernel to windows system then tell WSL2 to find the kernel.

Copy kernel to windows system.

# replace /d/WSLDistri/Kernel/dist/kernel-5.15.150.1
# with your desired directory path on windows
# kernel-5.15.150.1 is file name
sudo cp vmlinux /mnt/d/WSLDistri/Kernel/dist/kernel-5.15.150.1

Tell WSL2 where to find the kernel

first we need to create a file .wslconfig under your username’s folder on windows so that WSL2 will use it for configuration.

# replace [username] with your username
# on window
vim /mnt/c/Users/[username]/.wslconfig

This will enter text editor(vim). Next press i on keyboard to start editing and add following.

[wsl2]
# replace D:\\WSLDistri\\Kernel\\dist\\kernel-5.15.150.1
# with your kernel location on windows
# double backward slash for escaping is must
kernel=D:\\WSLDistri\\Kernel\\dist\\kernel-5.15.150.1

When finish press ESC on keyboard to exit editing mode then shift + : for enter command. Enter wq to save and quit and press Enter.

Finally exit WSL2 and shut it down. You can exit WSL2 by typing exit or Ctrl + D on keyboard.

Following commands are on windows

wsl --shutdown

And then we start WSL2 with webcam attached to it.

usbipd attach -w -b 1-5

Test

Let’s see if webcam is working.

Check if webcam is attached to WSL2

lsusb

This will install and start webcam application.

sudo apt install v4l-utils guvcview
sudo guvcview

Webcam is working. However the quality is low. For me low quality issue can be solved by switching to different webcam or lower resolution.

Webcam doesn’t work

Not all resolution, FPS and output type can works without problem. You might have this problem. And frame just freeze there.

This could be the FPS and resolution or output type doesn’t support. You can see the format by.

v4l2-ctl --list-formats-ext

and use.

sudo guvcview

To find out which FPS, resolution and output type works for you.

Conclusion

I went through using usbipd-win to attach webcam to WSL2 and building kernel, which included drivers we want from scratch. And finally I test it.

This is a long process but it is not as complicated as I thought. In general, the webcam works without issue.

--

--