kuroの覚え書き

96の個人的覚え書き

Raspberry Pi でUSBカメラを使う(整理編)

まずは何はなくともV4L2

$ v4l2-ctl

General/Common options:
  --all              display all information available
  -C, --get-ctrl=<ctrl>[,<ctrl>...]
                     get the value of the controls [VIDIOC_G_EXT_CTRLS]
  -c, --set-ctrl=<ctrl>=<val>[,<ctrl>=<val>...]
                     set the value of the controls [VIDIOC_S_EXT_CTRLS]
  -D, --info         show driver info [VIDIOC_QUERYCAP]
  -d, --device=<dev> use device <dev> instead of /dev/video0
                     if <dev> starts with a digit, then /dev/video<dev> is used 
#複数のデバイスを接続していたりするときは明示的に選択する必要がある。1台のときは省略可。
  -e, --out-device=<dev> use device <dev> for output streams instead of the
                     default device as set with --device
                     if <dev> starts with a digit, then /dev/video<dev> is used
  -h, --help         display this help message
  --help-all         all options
  --help-io          input/output options
  --help-misc        miscellaneous options
  --help-overlay     overlay format options
  --help-sdr         SDR format options
  --help-selection   crop/selection options
  --help-stds        standards and other video timings options
  --help-streaming   streaming options
  --help-tuner       tuner/modulator options
  --help-vbi         VBI format options
  --help-vidcap      video capture format options
  --help-vidout      vidout output format options
  --help-edid        edid handling options
  -k, --concise      be more concise if possible.
  -l, --list-ctrls   display all controls and their values [VIDIOC_QUERYCTRL]
  -L, --list-ctrls-menus
		     display all controls and their menus [VIDIOC_QUERYMENU]
  -r, --subset=<ctrl>[,<offset>,<size>]+
                     the subset of the N-dimensional array to get/set for control <ctrl>,
                     for every dimension an (<offset>, <size>) tuple is given.
  -w, --wrapper      use the libv4l2 wrapper library.
  --list-devices     list all v4l devices
  --log-status       log the board status in the kernel log [VIDIOC_LOG_STATUS]
  --get-priority     query the current access priority [VIDIOC_G_PRIORITY]
  --set-priority=<prio>
                     set the new access priority [VIDIOC_S_PRIORITY]
                     <prio> is 1 (background), 2 (interactive) or 3 (record)
  --silent           only set the result code, do not print any messages
  --sleep=<secs>     sleep <secs>, call QUERYCAP and close the file handle
  --verbose          turn on verbose ioctl status reporting

接続しているカメラの情報取得1

$ v4l2-ctl --info
Driver Info (not using libv4l2):
	Driver name   : uvcvideo
	Card type     : Venus USB2.0 Camera: Venus USB2
	Bus info      : usb-3f980000.usb-1.1.2
	Driver version: 4.19.66
	Capabilities  : 0x84A00001
		Video Capture
		Streaming
		Extended Pix Format
		Device Capabilities
	Device Caps   : 0x04200001
		Video Capture
		Streaming
		Extended Pix Format


接続しているカメラの情報取得2
(--info)にプラス現在の解像度、FPSの設定値、その他のパラメータの設定可能範囲と現在の設定値が表示される。

$ v4l2-ctl --all
Driver Info (not using libv4l2):
	Driver name   : uvcvideo
	Card type     : Venus USB2.0 Camera: Venus USB2
	Bus info      : usb-3f980000.usb-1.1.2
	Driver version: 4.19.66
	Capabilities  : 0x84A00001
		Video Capture
		Streaming
		Extended Pix Format
		Device Capabilities
	Device Caps   : 0x04200001
		Video Capture
		Streaming
		Extended Pix Format
Priority: 2
Video input : 0 (Camera 1: ok)
Format Video Capture:
	Width/Height      : 800/600
	Pixel Format      : 'MJPG'
	Field             : None
	Bytes per Line    : 0
	Size Image        : 960000
	Colorspace        : sRGB
	Transfer Function : Default
	YCbCr/HSV Encoding: Default
	Quantization      : Default
	Flags             : 
Crop Capability Video Capture:
	Bounds      : Left 0, Top 0, Width 800, Height 600
	Default     : Left 0, Top 0, Width 800, Height 600
	Pixel Aspect: 1/1
Selection: crop_default, Left 0, Top 0, Width 800, Height 600
Selection: crop_bounds, Left 0, Top 0, Width 800, Height 600
Streaming Parameters Video Capture:
	Capabilities     : timeperframe
	Frames per second: 15.000 (15/1)
	Read buffers     : 0
                     brightness (int)    : min=-10 max=10 step=1 default=0 value=-10
                       contrast (int)    : min=0 max=20 step=1 default=10 value=10
                     saturation (int)    : min=0 max=10 step=1 default=4 value=4
                            hue (int)    : min=-5 max=5 step=1 default=0 value=0
 white_balance_temperature_auto (bool)   : default=1 value=1
                          gamma (int)    : min=100 max=200 step=1 default=150 value=120
                           gain (int)    : min=32 max=48 step=1 default=32 value=32
           power_line_frequency (menu)   : min=0 max=2 default=1 value=1
      white_balance_temperature (int)    : min=2800 max=6500 step=1 default=6500 value=6500 flags=inactive
                      sharpness (int)    : min=0 max=10 step=1 default=4 value=5
         backlight_compensation (int)    : min=0 max=2 step=1 default=0 value=0
                  exposure_auto (menu)   : min=0 max=3 default=3 value=3
              exposure_absolute (int)    : min=8 max=16384 step=1 default=512 value=512 flags=inactive

接続しているカメラの情報取得3
(--all)で見られるパラメータの設定可能範囲と現在の設定値だけが表示される。

$ v4l2-ctl --list-ctrls
                     brightness (int)    : min=-10 max=10 step=1 default=0 value=-10
                       contrast (int)    : min=0 max=20 step=1 default=10 value=10
                     saturation (int)    : min=0 max=10 step=1 default=4 value=4
                            hue (int)    : min=-5 max=5 step=1 default=0 value=0
 white_balance_temperature_auto (bool)   : default=1 value=1
                          gamma (int)    : min=100 max=200 step=1 default=150 value=120
                           gain (int)    : min=32 max=48 step=1 default=32 value=32
           power_line_frequency (menu)   : min=0 max=2 default=1 value=1
      white_balance_temperature (int)    : min=2800 max=6500 step=1 default=6500 value=6500 flags=inactive
                      sharpness (int)    : min=0 max=10 step=1 default=4 value=5
         backlight_compensation (int)    : min=0 max=2 step=1 default=0 value=0
                  exposure_auto (menu)   : min=0 max=3 default=3 value=3
              exposure_absolute (int)    : min=8 max=16384 step=1 default=512 value=512 flags=inactive

接続しているカメラの情報取得4
(--list-ctrls)で見られるパラメータの設定可能範囲と現在の設定値で(menu)の項目の選択肢も含めて表示される。

$ v4l2-ctl --list-ctrls-menu
                     brightness (int)    : min=-10 max=10 step=1 default=0 value=-10
                       contrast (int)    : min=0 max=20 step=1 default=10 value=10
                     saturation (int)    : min=0 max=10 step=1 default=4 value=4
                            hue (int)    : min=-5 max=5 step=1 default=0 value=0
 white_balance_temperature_auto (bool)   : default=1 value=1
                          gamma (int)    : min=100 max=200 step=1 default=150 value=120
                           gain (int)    : min=32 max=48 step=1 default=32 value=32
           power_line_frequency (menu)   : min=0 max=2 default=1 value=1
				0: Disabled
				1: 50 Hz
				2: 60 Hz
      white_balance_temperature (int)    : min=2800 max=6500 step=1 default=6500 value=6500 flags=inactive
                      sharpness (int)    : min=0 max=10 step=1 default=4 value=5
         backlight_compensation (int)    : min=0 max=2 step=1 default=0 value=0
                  exposure_auto (menu)   : min=0 max=3 default=3 value=3
				1: Manual Mode
				3: Aperture Priority Mode
              exposure_absolute (int)    : min=8 max=16384 step=1 default=512 value=512 flags=inactive

接続しているカメラの情報取得5
カメラの対応しているビデオフォーマットが確認できる。

$ v4l2-ctl --list-formats
ioctl: VIDIOC_ENUM_FMT
	Index       : 0
	Type        : Video Capture
	Pixel Format: 'YUYV'
	Name        : YUYV 4:2:2

	Index       : 1
	Type        : Video Capture
	Pixel Format: 'MJPG' (compressed)
	Name        : Motion-JPEG

接続しているカメラの情報取得6
利用できる解像度をリストアップ

$ v4l2-ctl --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
	Index       : 0
	Type        : Video Capture
	Pixel Format: 'YUYV'
	Name        : YUYV 4:2:2
		Size: Discrete 640x480
			Interval: Discrete 0.033s (30.000 fps)
			Interval: Discrete 0.067s (15.000 fps)
		Size: Discrete 352x288
			Interval: Discrete 0.033s (30.000 fps)
			Interval: Discrete 0.067s (15.000 fps)
		Size: Discrete 320x240
			Interval: Discrete 0.033s (30.000 fps)
			Interval: Discrete 0.067s (15.000 fps)
		Size: Discrete 176x144
			Interval: Discrete 0.033s (30.000 fps)
			Interval: Discrete 0.067s (15.000 fps)
		Size: Discrete 160x120
			Interval: Discrete 0.033s (30.000 fps)
			Interval: Discrete 0.067s (15.000 fps)
		Size: Discrete 1280x960
			Interval: Discrete 0.133s (7.500 fps)
			Interval: Discrete 0.200s (5.000 fps)
		Size: Discrete 1280x1024
			Interval: Discrete 0.133s (7.500 fps)
			Interval: Discrete 0.200s (5.000 fps)
		Size: Discrete 1600x1200
			Interval: Discrete 0.200s (5.000 fps)
			Interval: Discrete 0.400s (2.500 fps)
		Size: Discrete 800x600
			Interval: Discrete 0.050s (20.000 fps)
			Interval: Discrete 0.067s (15.000 fps)
		Size: Discrete 1280x720
			Interval: Discrete 0.100s (10.000 fps)
			Interval: Discrete 0.200s (5.000 fps)

	Index       : 1
	Type        : Video Capture
	Pixel Format: 'MJPG' (compressed)
	Name        : Motion-JPEG
		Size: Discrete 640x480
			Interval: Discrete 0.033s (30.000 fps)
			Interval: Discrete 0.067s (15.000 fps)
		Size: Discrete 352x288
			Interval: Discrete 0.033s (30.000 fps)
			Interval: Discrete 0.067s (15.000 fps)
		Size: Discrete 320x240
			Interval: Discrete 0.033s (30.000 fps)
			Interval: Discrete 0.067s (15.000 fps)
		Size: Discrete 160x120
			Interval: Discrete 0.033s (30.000 fps)
			Interval: Discrete 0.067s (15.000 fps)
		Size: Discrete 1280x960
			Interval: Discrete 0.067s (15.000 fps)
			Interval: Discrete 0.100s (10.000 fps)
		Size: Discrete 1280x1024
			Interval: Discrete 0.067s (15.000 fps)
			Interval: Discrete 0.100s (10.000 fps)
		Size: Discrete 1600x1200
			Interval: Discrete 0.067s (15.000 fps)
			Interval: Discrete 0.133s (7.500 fps)
		Size: Discrete 800x600
			Interval: Discrete 0.067s (15.000 fps)
			Interval: Discrete 0.100s (10.000 fps)
		Size: Discrete 1280x720
			Interval: Discrete 0.067s (15.000 fps)
			Interval: Discrete 0.100s (10.000 fps)

パラメータ設定値を個別に確認

$ v4l2-ctl --get-ctrl brightness
brightness: -10

以下のような書き方も同じ結果を返す

$ v4l2-ctl -C brightness
brightness: -10
$ v4l2-ctl --get-ctrl=brightness
brightness: -10

パラメータを設定する

$ v4l2-ctl --set-ctrl brightness=0

$ v4l2-ctl -C brightness
brightness: 0

以下のような書き方も同じ結果を返す

$ v4l2-ctl -c brightness=0

2つ以上のパラメータを同時に設定したり、確認したりもできる

$ v4l2-ctl -c brightness=10,contrast=5

$ v4l2-ctl -C brightness,contrast
brightness: 10
contrast: 5

以上のように確認、設定した上で

$ fswebcam -r 800x600 --no-banner -F 1 -S 100 ./image.jpg

のようにfswebcamで写真を撮る。
f:id:k-kuro:20210509180048p:plain
デフォルト設定で撮影するとこんな感じで

$ v4l2-ctl -l
                     brightness (int)    : min=-10 max=10 step=1 default=0 value=6
                       contrast (int)    : min=0 max=20 step=1 default=10 value=10
                     saturation (int)    : min=0 max=10 step=1 default=4 value=4
                            hue (int)    : min=-5 max=5 step=1 default=0 value=-3
 white_balance_temperature_auto (bool)   : default=1 value=1
                          gamma (int)    : min=100 max=200 step=1 default=150 value=177
                           gain (int)    : min=32 max=48 step=1 default=32 value=32
           power_line_frequency (menu)   : min=0 max=2 default=1 value=1
      white_balance_temperature (int)    : min=2800 max=6500 step=1 default=6500 value=6500 flags=inactive
                      sharpness (int)    : min=0 max=10 step=1 default=4 value=9
         backlight_compensation (int)    : min=0 max=2 step=1 default=0 value=0
                  exposure_auto (menu)   : min=0 max=3 default=3 value=3
              exposure_absolute (int)    : min=8 max=16384 step=1 default=512 value=512 flags=inactive

という設定だと
f:id:k-kuro:20210509180129p:plain
こうなる。
明るさなどはカメラ側がある程度自動で合わせようとするので、あんまり思ったとおりにはならないようだ。

解像度とFPSの設定は

$ v4l2-ctl --set-fmt-video=width=640,height=480 --set-parm=15

というふうにできるがfswebcamを使うならfswebcamのほうで設定できるので、v4l2で無理に設定しておく必要はないだろう。