WSL2 connection calls USB device

Disclaimer: The tutorial in this article comes from the WSL tutorial on Microsoft's official website, the link address: Connect USB devices

        I recently learned OpenCV and found that Microsoft's WSL is a good thing. Combined with the VS Code editor, whether it is C++ or Python, it is a perfect match. Python is best installed with Anaconda, and you will learn machine learning and neural networks later. Even paired with Cuda, perfect! It can be said that for students who only have one computer with Win11 installed, WSL2+Anaconda+VS Code+OpenCV+Cuda is the fastest and best experience environment construction solution. ps: GUI (graphical interface) is easy to solve, just install an application with a graphical interface with sudo apt install gedit , and wsl will automatically configure the GUI.

        Let's get to the point. As we all know, WSL does not support USB devices by default, how can you learn OpenCV without a camera! I searched all over the Internet and found that the tutorials on Google basically teach us to download the code from the Github library of Microsoft's WSL, and then compile it locally to enable the USB function. This method has been verified many times to be feasible, but it is too cumbersome and consumes a lot of campus network traffic. In the end, I kept changing the search keywords, and finally visited Microsoft's official tutorial. Not much nonsense, the specific tutorials are introduced below. Those who have English reading ability should try to check the official tutorials. The official tutorials are more detailed and updated.

Let me explain in advance, if you want to use wsl to implement OpenCV to call the camera to process video, there are the following methods:

1. Using the recorded video directly can fully satisfy the experiment and test;

2. Use a network camera, or install an IP Camera on a mobile phone, and access the IP address in the LAN: If the port can be used normally, then OpenCV can also be used directly. This is the best method, and it is feasible in actual measurement. There are many related apps, and if IP isolation is enabled on the campus network, then just turn on a hotspot on the laptop.

3. Honestly recompile the WSL kernel and enable the USB camera driver.

System configuration requirements:

  • Win11 (version number 22000 and above, please refer to the official tutorial for Win10: Win10 Official Tutorial for Connecting USB Devices
  • Does not support Arm64 devices, only supports x64/x86 architecture
  • The system has successfully installed the WSL2 environment
  • The linux kernel version number of wsl2 is 5.10.60.1 and above, check method: enter uname -a in the linux command line interface

step:

Install usbipd-win (in Windows environment)

  1. Go to Github to download the release file of usbipd-win
  2. Select the .msi file to download, if the browser reports an error, choose to believe the file, double-click to run the installation
  3. For other installation methods and interpretations, please visit the official tutorial

 Install the USBIP tool (in the Linux environment)

        After the usbip tool is installed in the Windows environment, it needs to be configured in the Linux environment. The following describes the configuration in the Ubuntu system. For other systems, please refer to the official tutorial.

        In Ubuntu's bash (command line), run the following command:

sudo apt install 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

Connect a USB device

After the tool is configured, the USB device is not automatically connected to WSL, and we need to connect it manually.

Note : Before connecting the USB device, you need to confirm that there is a WSL command line interface open. We need the VM environment of WSL2 to be activated.

  1. Start by running a PowerShell command-line interface as an administrator, then enter the following command:
    usbipd wsl list

  2. Select the BUSID value of the USB device you want to connect, and enter the following command in PowerShell:

    usbipd wsl attach --busid <busid>   #注意,<busid>是一个整体,直接输入busid号就行,不要带<>

    It should be noted that after entering the command, Ubuntu's bash command line may remind you to enter the password, because this operation requires sudo authority.

  3. Then you can check whether the USB device is successfully connected, enter the following command in Ubuntu's bash:

    lsusb
     When the USB is not connected: After the connection is successful:  You can see that the Camera has been connected to the WSL. PowerShell also shows that Camera is connected to Ubuntu.
  4. After using the device in WSL, you can directly eject the USB device from Windows or unplug it directly, that is, disconnect the USB device from the physical level, or run this command from PowerShell in administrator mode:
    usbipd wsl detach --busid <busid>  #记得修改<busid>具体号码

 Precautions:

  1. Every time you close wsl or disconnect from the usb device, you need to reconnect the device. There are two ways to reconnect the usb device:
    1. As mentioned above, run the administrator mode PowerShell, connect with the usbipd command;
    2. Ubuntu's bash interface connection, enter the following command:
      usbip list -r $HOSTNAME.local     #查看已连接过的设备信息,如记得busid,该命令非必须执行命令
      sudo usbip attach -r $HOSTNAME.local -b <busid>  #连接USB设备,注意替换<busid>

  2.  How to update the latest version of linux-tools-*-generic:
    1. Create a script file:
      #!/usr/bin/env bash
      
      rel="$(uname -r)"
      rel="${rel%%-*}"
      rel=(${rel//./ })
      
      function latest_linux_tools {
          apt-cache search linux-tools |
          awk -v cur_ver="${rel[*]}" '
          /^linux-tools([-\.][0-9]+)+-generic\>/ {
              ltg_package=$1
              gsub(/[^0-9]+/," ",$1);
              gsub(/^\s*/,"",$1);
              split($1,ltg_ver,/\s*/);
              split(cur_ver,cmp_ver,/\s*/)
              if (ltg_ver[1]<=cmp_ver[1] && ltg_ver[2]<=cmp_ver[2] && ltg_ver[3]<=cmp_ver[3]) {
                  print ltg_package;
              }
          }' | sort -nr | head -n 1
      }
      
      # optional ...
      apt-get install "$@" "$(latest_linux_tools)"
    2.  Execute with the following command:
      sudo nano /usr/local/bin/install_linux_tools_generic
      # 将上面的脚本内容复制到这个install_linux_tools_generic文件中
      # 修改文件权限
      sudo chmod u+x /usr/local/bin/install_linux_tools_generic
      
      # 先预览一下要最新版本的linux-tools-*-generic版本
      sudo install_linux_tools_generic -s
      
      # 如果有新版本,可以安装,执行下面指令
      sudo install_linux_tools_generic -y

    3.  Reference source: Connecting USB devices to WSL - Windows Command Line (microsoft.com)
  3. Because usbipd-win does not provide camera support, or there is no usb camera driver in wsl, so the usb camera cannot be successfully used only by using usbipd-win. We need to add the usb camera driver to wsl. The only successful method I know so far is to download the wsl code from github, then compile it locally, enable the usb function, and then package the driver of the usb camera you use. Built my own kernel, but can't get it to display as a video device Issue #257 dorssel/usbipd-win (github.com) OpenCV cannot find camera device in wsl2 Issue #6211 microsoft/WSL (github. com)
     

おすすめ

転載: blog.csdn.net/leiconghe/article/details/123877944