wsl2 use usb camera

主要内容

  • 如题所示

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
  • 21
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值