4
2

More than 1 year has passed since last update.

[備忘録] wslからusbカメラにアクセス

Last updated at Posted at 2022-02-26

環境構築

wslカーネルのビルド

実行(Powershell ⇒ wsl)

PowerShell(管理者)

  • 下記のコマンドで使用したいカメラの番号を取得(例:1-4)
$ usbipd wsl list
  • 有効化
$ usbipd wsl attach --busid 1-4
  • 無効化
$ usbipd wsl detach --busid 1-4

wsl

  • usbカメラのデバイスファイルのパーミッションを変更
$ sudo chmod 777 /dev/video* 
  • fswebcamによる動作確認
$ fswebcam -d /dev/video0 -r 640×480 test.jpg

test.jpg

$ v4l2-ctl --list-devices
  • デバイスがサポートするフォーマットと フレームサイズとフレームレートの組み合わせを表示する
$ v4l2-ctl -d /dev/video0 --list-formats-ext

openCVで画像取得

import cv2
capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc('M','J','P','G'))

if capture.isOpened() is False:
  raise IOError

while(True):
  try:
    ret, frame = capture.read()
    if ret is False:
      raise IOError
    cv2.imshow('frame',frame)
    cv2.waitKey(1)
  except KeyboardInterrupt:
    # 終わるときは CTRL + C を押す
    break

capture.release()
cv2.destroyAllWindows()
  • キャプチャーボードがあればゲーム画面も取得できます
    image.png

使用した機器

参考文献

4
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2