🤪
WSL2 USBカメラ+他のUSB機器 2022年09月06日版
- OS:Windows11 21H2
- OSビルド:22000.918
- WSL2 5.10.102.1 + Ubuntu20.04
- WSL2 Linux Kernel 5.10.60.1 からKernelモジュールにUSBIP対応が標準的に組み込まれたらしいが、Microsoft公式が提供しているKernelや手順ををそのまま使用すると動作しない
- 2022年09月06日時点の最新カーネルは
5.15.62.1
だが、wsl --update
で展開されるバージョンが5.10.102.1
だったため5.10.102.1
を使用する - 以下すべての手順の
Windows Terminal
を使用する箇所は管理者権限
で実行 - 以下、
[WT]
は Windows Terminal、[Ubuntu]
は Ubuntu側のbashを表す - WSLのカーネルアップデートと
usbipd-win
のインストール - [WT] Windows Terminalで実行
> wsl --update
> wsl --status
カーネル バージョン: 5.10.102.1
> winget install --interactive --exact dorssel.usbipd-win
見つかりました usbipd-win [dorssel.usbipd-win] バージョン 2.3.0
このアプリケーションは所有者からライセンス供与されます。
Microsoft はサードパーティのパッケージに対して責任を負わず、ライセンスも付与しません。
Downloading https://github.com/dorssel/usbipd-win/releases/download/v2.3.0/usbipd-win_2.3.0.msi
██████████████████████████████ 10.4 MB / 10.4 MB
インストーラーハッシュが正常に検証されました
パッケージのインストールを開始しています...
インストールが完了しました
- [WT] Ubuntu 20.04 のインストール
> wsl --install --distribution Ubuntu-20.04
- [WT] WSLのディストリビューションを起動(WSL2起動用アイコンをマウスでクリックして起動してもよい)
> wsl --list
Linux 用 Windows サブシステム ディストリビューション:
Ubuntu-20.04 (既定)
> wsl -d Ubuntu-20.04
- [Ubuntu] 追加パッケージのインストール
sudo apt update
sudo apt install -y linux-tools-5.4.0-77-generic hwdata
sudo update-alternatives --install /usr/local/bin/usbip usbip /usr/lib/linux-tools/5.4.0-77-generic/usbip 20
- [WT] WSLの再起動
> wsl --shutdown
> wsl -d Ubuntu-20.04
- PCにUSBカメラを接続する
- [WT] USBカメラが
usbipd
に認識されることを確認する (この記事では 2-7)
> usbipd wsl list
BUSID VID:PID DEVICE STATE
2-2 056e:00d9 USB 入力デバイス Not attached
2-3 1c4f:0027 USB 入力デバイス Not attached
2-7 1bcf:2284 Full HD webcam, USB microphone Not attached
2-11 0495:3011 ESS USB DAC, USB 入力デバイス Not attached
2-14 8087:0029 インテル(R) ワイヤレス Bluetooth(R) Not attached
- [WT] USBカメラをUbuntu側にアタッチする(アタッチに成功した場合は何も表示されない)
> usbipd wsl attach --busid 2-7
>
- [WT] USBカメラが正常にアタッチされていることを確認する(
Attached
と表示されていれば成功)
> usbipd wsl list
BUSID VID:PID DEVICE STATE
2-2 056e:00d9 USB 入力デバイス Not attached
2-3 1c4f:0027 USB 入力デバイス Not attached
2-7 1bcf:2284 Full HD webcam, USB microphone Attached - Ubuntu-20.04
2-11 0495:3011 ESS USB DAC, USB 入力デバイス Not attached
2-14 8087:0029 インテル(R) ワイヤレス Bluetooth(R) Not attached
- [Ubuntu] Ubuntuのbashにログオンした既定のユーザを
video
グループに所属させる。なお、WSLを起動した時点で既に追加されているメッセージが表示される。
$ sudo adduser $USER video
- [Ubuntu] WSL2上の
Ubuntu20.04
の中からUSBカメラが認識されていることを確認する。lsusb
コマンドを経由すると正常にUSBカメラが認識されているが、/dev/video*
にはUSBカメラが列挙されない
$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 1bcf:2284 Sunplus Innovation Technology Inc. Full HD webcam
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
$ ls -l /dev/video*
ls: cannot access '/dev/video*': No such file or directory
- [Ubuntu] USB CameraがWSL内で認識されるようにLinuxカーネルをカスタムビルドする。下記リポジトリの手順通りに実施すると、WSL Linuxカーネルがカスタムビルドされたものに入れ替わる。注意点は、
<windows username>
の部分だけは各自の環境のWindowsユーザー名に手で書き換える必要が有ること。なお、.wslconfig
は 絶対に windows 側で編集してはならない。絶対に。
- [WT] WSLをシャットダウンし、その後起動する
> wsl --shutdown
> wsl -d Ubuntu-20.04
- [WT] USBカメラが
usbipd
に認識されることを確認する (この記事では 2-7)
> usbipd wsl list
BUSID VID:PID DEVICE STATE
2-2 056e:00d9 USB 入力デバイス Not attached
2-3 1c4f:0027 USB 入力デバイス Not attached
2-7 1bcf:2284 Full HD webcam, USB microphone Not attached
2-11 0495:3011 ESS USB DAC, USB 入力デバイス Not attached
2-14 8087:0029 インテル(R) ワイヤレス Bluetooth(R) Not attached
- [WT] USBカメラをUbuntu側にアタッチする(アタッチに成功した場合は何も表示されない)
> usbipd wsl attach --busid 2-7
>
- [WT] USBカメラが正常にアタッチされていることを確認する(
Attached
と表示されていれば成功)
> usbipd wsl list
BUSID VID:PID DEVICE STATE
2-2 056e:00d9 USB 入力デバイス Not attached
2-3 1c4f:0027 USB 入力デバイス Not attached
2-7 1bcf:2284 Full HD webcam, USB microphone Attached - Ubuntu-20.04
2-11 0495:3011 ESS USB DAC, USB 入力デバイス Not attached
2-14 8087:0029 インテル(R) ワイヤレス Bluetooth(R) Not attached
- [Ubuntu] WSL2上の
Ubuntu20.04
の中からUSBカメラが認識されていることを確認する
$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 1bcf:2284 Sunplus Innovation Technology Inc. Full HD webcam
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
$ ls -l /dev/video*
crw------- 1 root root 81, 0 Sep 6 17:29 /dev/video0
crw------- 1 root root 81, 1 Sep 6 17:29 /dev/video1
- [Ubuntu] USBカメラがWSL2の中から認識されることを確認するテストコードを作成する
$ pip install opencv-contrib-python
$ cat << 'EOT' > ${HOME}/usbcam_test.py
import cv2
W=640
H=480
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M','J','P','G'))
#cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('Y','U','Y','V'))
cap.set(cv2.CAP_PROP_FRAME_WIDTH, W)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, H)
cap.set(cv2.CAP_PROP_FPS, 30)
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()
EOT
- [Ubuntu] USBカメラ認識テスト実行(表示がカクつくとかブロックノイズが入るなどの現象はこの手順固有の問題ではなく、カメラとUSBインタフェースのデータ転送レートの問題と考えられるため、表示がうまくいかないときは、テストコード中の
MJPEG
をYUYV
側に切り替えてみたり、入力解像度を下げてみたり、USBカメラを複数台取り替えてみること。少なくとも USB3.0ポートに接続されたハイスピードな RealSense D435 は正常に動作する。)
$ sudo chmod 777 /dev/video* && python ${HOME}/usbcam_test.py
- PA-PA452
Discussion
Hi, I follow the steps shown on this blog and I have successfully compiled the kernel like below:
![](https://storage.googleapis.com/zenn-user-upload/50e4c1296bc7-20221217.png)
![](https://storage.googleapis.com/zenn-user-upload/a5a801fdd863-20221217.png)
But when I run usbcam_test.py to test if it works,it will throw the messages like this:
I searched it on google and it seems like I need to install the Xserver to display the gui,But the lastest wsl2 kernel should already integrated wslg,so I wonder if it is due to the kernel version which you used is 5.10.102.1 not the lastest 5.15.62.1,and if it really the reson,what should I do to solve it,update to the lastest kernel or just install Xserver in my currently distro,hope you can give me some advice
I have not written instructions for using Anaconda or Miniconda.
Thanks for your sincere reply,and I have solved this problem using X410 to disply the window:
![](https://storage.googleapis.com/zenn-user-upload/be97958465ca-20221217.png)
![](https://storage.googleapis.com/zenn-user-upload/193a050a65dc-20221217.png)
Actually I want to use wslg to display the window at first but I don't know why I can't update my kernel:
and obviously this kernel won't support wslg so the window isn't shown as expected like the comment I have posted above,and the question I want to ask you is that whether it is because I use your recompiled kernel so I can't update it?
I guess it is natural since you have described the config file so that Windows will refer to the Linux Kernel that you have custom built on your own.
![](https://storage.googleapis.com/zenn-user-upload/0ccf472eb245-20221218.png)
oh,I did remember that I have set the kernel in my .wslconfig file before,so is that mean I can't use wslg in this custom built kernel and if it really does,how should I do to install wslg in this custom built kernel,I would appreciate it if you can give me some instructions😃
I don't know anything about the individual's environment needs. Change your own kernel settings.
Sorry, I am preparing for the final exam recently, so the reply may not be timely, thank you very much for your patient reply,but at present I use X410 to run the gui and everything is fine, so I am going to reinstall a new Ubuntu20.04 distro to verify whether it is truly because I'm using a recompiled kernel,However,I'll also try to follow your advice and change the kernel to the lastest version to see if it works