主要内容
- 如题所示
Update wsl kernel
wsl --update
Install usbipd In win10
winget install usbipd
Install usbpi in wsl2
sudo apt install linux-tools-generic hwdata
sudo update-alternatives --install /usr/local/bin/usbip usbip /usr/lib/linux-tools/*-generic/usbip 20
Check usb camera in win10
Open powershell using admin(管理员权限打开)
# list device
usbipd wsl list
#attach to wsl
usbipd wsl attach -b 2-2
- In wsl terminal
lsusb
- 默认Linux in wsl2 kernel 没有打开相关的驱动,需要重新编译,安装新的驱动
# install depend
sudo apt update && sudo apt install build-essential flex bison libssl-dev libelf-dev
# clone
cd /usr/src
TAGVERNUM=5.15.90.1 \
&& TAGVER=linux-msft-wsl-${TAGVERNUM}
sudo git clone --depth 1 -b ${TAGVER} \
https://github.com/microsoft/WSL2-Linux-Kernel.git \
${TAGVERNUM}-microsoft-standard \
&& cd ${TAGVERNUM}-microsoft-standard
sudo make KCONFIG_CONFIG=Microsoft/config-wsl menuconfig
# Select Device Drivers -> Enable Multimedia support
# Select Multimedia support -> Media Drivers: Enable Media USB Adapters
# Select Media USB Adapters: Enable USB Video Class (UVC)
# Save the config to "Microsoft/config-wsl.uvc" (or whatever you want to call it)
# Exit menuconfig
sudo make KCONFIG_CONFIG=Microsoft/config-wsl.uvc -j$(nproc)
# Install the kernel modules and headers:
sudo make modules_install headers_install
# Copy the kernel image to the Windows file system:
# inpalce username to accutual user name
sudo cp arch/x86/boot/bzImage /mnt/c/Users/{username}
#Create or edit the file %USERPROFILE%\Users/{username}\.wslconfig with the following content: in winndows
[wsl2]
kernel=C:\\Users\\11487\\bzImage
# Open a PowerShell terminal window as Administrator
# Stop the WSL instance
wsl --shutdown
# open new terimal to open new wsl
Create usb device ip connection.
Check device
lsusb
#Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
#Bus 001 Device 002: ID 046d:0825 Logitech, Inc. Webcam C270
#Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
ls /dev/video*
sudo chmod 777 /dev/video0
#using opencv-python
python3 test.py
- 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()
- If use new ubuntu distribution, please follow https://github.com/PINTO0309/wsl2_linux_kernel_usbcam_enable_conf/blob/main/README.md
Ref
- https://learn.microsoft.com/en-us/windows/wsl/connect-usb
- https://github.com/dorssel/usbipd-win
- https://learn.microsoft.com/en-us/community/content/wsl-user-msft-kernel-v6#1—building-the-microsoft-linux-kernel-v61x
- https://stackoverflow.com/questions/72255353/wsl-webcam-usb-can-not-open-camera-by-index
- https://github.com/PINTO0309/wsl2_linux_kernel_usbcam_enable_conf/blob/main/README.md