【树莓派5 测评】 + 11.USB-Camera功能测试(zmj)
本篇主要介绍在树莓派5开发板测试USB-Camera(UVC)的流程。 //------UVC / USB-Camera
UVC通俗的讲就是USB摄像头,全称为USB Video Class,即:USB视频类,是一种为USB视频捕获设备定义的协议标准。---------------------------以下为正文--------------------------- 1. 摄像头接口和设备摄像头设备位于” /dev/video*“(此处仅连接了USB摄像头)。 - //------查看摄像头设备节点
- //---测试指令
- a.通过设备列表查看
- ls -la /dev/video*
- b.通过v4l2-ctl工具查看
- v4l2-ctl --list-devices
- c.通过media0检查有效摄像头节点
- media-ctl -d /dev/media3 -p
- d.无效指令:libcamera-hello无法检测UVC,貌似支持CSI接口摄像头
- libcamera-hello --list-cameras
- //---检查结果
- USB 2.0 Camera: USB Camera (usb-xhci-hcd.0-2.1):
- /dev/video0
- /dev/video1
- /dev/media3
-
- //------示例Log信息
- zhaomeijing@raspberrypi5:~/workspace/09_uvc$ lsusb
- Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
- Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
- Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
- Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
- zhaomeijing@raspberrypi5:~/workspace/09_uvc$ ls -la /dev/video*
- crw-rw----+ 1 root video 81, 18 Apr 26 18:15 /dev/video19
- crw-rw----+ 1 root video 81, 0 Apr 26 18:15 /dev/video20
- crw-rw----+ 1 root video 81, 1 Apr 26 18:15 /dev/video21
- crw-rw----+ 1 root video 81, 2 Apr 26 18:15 /dev/video22
- crw-rw----+ 1 root video 81, 3 Apr 26 18:15 /dev/video23
- crw-rw----+ 1 root video 81, 4 Apr 26 18:15 /dev/video24
- crw-rw----+ 1 root video 81, 5 Apr 26 18:15 /dev/video25
- crw-rw----+ 1 root video 81, 6 Apr 26 18:15 /dev/video26
- crw-rw----+ 1 root video 81, 7 Apr 26 18:15 /dev/video27
- crw-rw----+ 1 root video 81, 8 Apr 26 18:15 /dev/video28
- crw-rw----+ 1 root video 81, 9 Apr 26 18:15 /dev/video29
- crw-rw----+ 1 root video 81, 10 Apr 26 18:15 /dev/video30
- crw-rw----+ 1 root video 81, 11 Apr 26 18:15 /dev/video31
- crw-rw----+ 1 root video 81, 12 Apr 26 18:15 /dev/video32
- crw-rw----+ 1 root video 81, 13 Apr 26 18:15 /dev/video33
- crw-rw----+ 1 root video 81, 14 Apr 26 18:15 /dev/video34
- crw-rw----+ 1 root video 81, 15 Apr 26 18:15 /dev/video35
- crw-rw----+ 1 root video 81, 16 Apr 26 18:15 /dev/video36
- crw-rw----+ 1 root video 81, 17 Apr 26 18:15 /dev/video37
- zhaomeijing@raspberrypi5:~/workspace/09_uvc$ lsusb
- Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
- Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
- Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
- Bus 001 Device 004: ID 1b3f:2008 Generalplus Technology Inc. Usb Audio Device
- Bus 001 Device 003: ID 0c45:6362 Microdia USB 2.0 Camera
- Bus 001 Device 002: ID 1a40:0801 Terminus Technology Inc. USB 2.0 Hub
- Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
- zhaomeijing@raspberrypi5:~/workspace/09_uvc$ dmesg | tail -23
- [148218.886321] usb 1-2: new high-speed USB device number 2 using xhci-hcd
- [148219.034490] usb 1-2: New USB device found, idVendor=1a40, idProduct=0801, bcdDevice= 1.00
- [148219.034496] usb 1-2: New USB device strings: Mfr=0, Product=1, SerialNumber=0
- [148219.034499] usb 1-2: Product: USB 2.0 Hub
- [148219.082777] hub 1-2:1.0: USB hub found
- [148219.082819] hub 1-2:1.0: 4 ports detected
- [148219.370336] usb 1-2.1: new high-speed USB device number 3 using xhci-hcd
- [148219.548936] usb 1-2.1: New USB device found, idVendor=0c45, idProduct=6362, bcdDevice= 0.00
- [148219.548944] usb 1-2.1: New USB device strings: Mfr=2, Product=1, SerialNumber=0
- [148219.548947] usb 1-2.1: Product: USB 2.0 Camera
- [148219.548949] usb 1-2.1: Manufacturer: Sonix Technology Co., Ltd.
- [148219.694326] usb 1-2.3: new full-speed USB device number 4 using xhci-hcd
- [148219.815720] usb 1-2.3: config 1 has an invalid interface number: 3 but max is 2
- [148219.815725] usb 1-2.3: config 1 has no interface number 1
- [148219.816405] usb 1-2.3: New USB device found, idVendor=1b3f, idProduct=2008, bcdDevice= 1.00
- [148219.816409] usb 1-2.3: New USB device strings: Mfr=1, Product=2, SerialNumber=0
- [148219.816412] usb 1-2.3: Product: Usb Audio Device
- [148219.816415] usb 1-2.3: Manufacturer: Generalplus
- [148219.851468] input: Generalplus Usb Audio Device as /devices/platform/axi/1000120000.pcie/1f00200000.usb/xhci-hcd.0/usb1/1-2/1-2.3/1-2.3:1.3/0003:1B3F:2008.0001/input/input5
- [148219.910418] hid-generic 0003:1B3F:2008.0001: input,hidraw0: USB HID v2.01 Device [Generalplus Usb Audio Device] on usb-xhci-hcd.0-2.3/input3
- [148220.026566] usb 1-2.1: Found UVC 1.00 device USB 2.0 Camera (0c45:6362)
- [148220.054790] usbcore: registered new interface driver uvcvideo
- [148220.079573] usbcore: registered new interface driver snd-usb-audio
- zhaomeijing@raspberrypi5:~/workspace/09_uvc$ ls -la /dev/video*
- crw-rw----+ 1 root video 81, 19 Apr 28 11:25 /dev/video0
- crw-rw----+ 1 root video 81, 20 Apr 28 11:25 /dev/video1
- crw-rw----+ 1 root video 81, 18 Apr 26 18:15 /dev/video19
- crw-rw----+ 1 root video 81, 0 Apr 26 18:15 /dev/video20
- crw-rw----+ 1 root video 81, 1 Apr 26 18:15 /dev/video21
- crw-rw----+ 1 root video 81, 2 Apr 26 18:15 /dev/video22
- crw-rw----+ 1 root video 81, 3 Apr 26 18:15 /dev/video23
- crw-rw----+ 1 root video 81, 4 Apr 26 18:15 /dev/video24
- crw-rw----+ 1 root video 81, 5 Apr 26 18:15 /dev/video25
- crw-rw----+ 1 root video 81, 6 Apr 26 18:15 /dev/video26
- crw-rw----+ 1 root video 81, 7 Apr 26 18:15 /dev/video27
- crw-rw----+ 1 root video 81, 8 Apr 26 18:15 /dev/video28
- crw-rw----+ 1 root video 81, 9 Apr 26 18:15 /dev/video29
- crw-rw----+ 1 root video 81, 10 Apr 26 18:15 /dev/video30
- crw-rw----+ 1 root video 81, 11 Apr 26 18:15 /dev/video31
- crw-rw----+ 1 root video 81, 12 Apr 26 18:15 /dev/video32
- crw-rw----+ 1 root video 81, 13 Apr 26 18:15 /dev/video33
- crw-rw----+ 1 root video 81, 14 Apr 26 18:15 /dev/video34
- crw-rw----+ 1 root video 81, 15 Apr 26 18:15 /dev/video35
- crw-rw----+ 1 root video 81, 16 Apr 26 18:15 /dev/video36
- crw-rw----+ 1 root video 81, 17 Apr 26 18:15 /dev/video37
- zhaomeijing@raspberrypi5:~/workspace/09_uvc$ libcamera-hello --list-cameras
- No cameras available!
- zhaomeijing@raspberrypi5:~/workspace/09_uvc$ v4l2-ctl --list-devices
- pispbe (platform:1000880000.pisp_be):
- /dev/video20
- /dev/video21
- /dev/video22
- /dev/video23
- /dev/video24
- /dev/video25
- /dev/video26
- /dev/video27
- /dev/video28
- /dev/video29
- /dev/video30
- /dev/video31
- /dev/video32
- /dev/video33
- /dev/video34
- /dev/video35
- /dev/video36
- /dev/video37
- /dev/media0
- /dev/media1
- rpivid (platform:rpivid):
- /dev/video19
- /dev/media2
- USB 2.0 Camera: USB Camera (usb-xhci-hcd.0-2.1):
- /dev/video0
- /dev/video1
- /dev/media3
- zhaomeijing@raspberrypi5:~/workspace/09_uvc$ media-ctl -d /dev/media3 -p
- Media controller API version 6.6.20
- Media device information
- ------------------------
- driver uvcvideo
- model USB 2.0 Camera: USB Camera
- serial
- bus info usb-xhci-hcd.0-2.1
- hw revision 0x0
- driver version 6.6.20
- Device topology
- - entity 1: USB 2.0 Camera: USB Camera (1 pad, 1 link)
- type Node subtype V4L flags 1
- device node name /dev/video0
- pad0: Sink
- <- "Extension 3":1 [ENABLED,IMMUTABLE]
- - entity 4: USB 2.0 Camera: USB Camera (0 pad, 0 link)
- type Node subtype V4L flags 0
- device node name /dev/video1
- - entity 8: Extension 3 (2 pads, 2 links)
- type V4L2 subdev subtype Unknown flags 0
- pad0: Sink
- <- "Processing 2":1 [ENABLED,IMMUTABLE]
- pad1: Source
- -> "USB 2.0 Camera: USB Camera":0 [ENABLED,IMMUTABLE]
- - entity 11: Processing 2 (2 pads, 2 links)
- type V4L2 subdev subtype Unknown flags 0
- pad0: Sink
- <- "Camera 1":0 [ENABLED,IMMUTABLE]
- pad1: Source
- -> "Extension 3":0 [ENABLED,IMMUTABLE]
- - entity 14: Camera 1 (1 pad, 1 link)
- type V4L2 subdev subtype Sensor flags 0
- pad0: Source
- -> "Processing 2":0 [ENABLED,IMMUTABLE]
- zhaomeijing@raspberrypi5:~/workspace/09_uvc$
复制代码//------USB摄像头状态 2. USB摄像头测试将USB摄像头插入开发板,将自动安装 uvc 添加设备节点。 USB 2.0 Camera: USB Camera (usb-xhci-hcd.0-2.1): /dev/video0/dev/video1/dev/media3
USB摄像头测试内容包括设备节点检测、视频、拍照,测试指令如下: - //------USB-Camera(某宝几十块买的USB摄像头,支持1080P)
- a.确认USB摄像头的设备节点,此处为/dev/video0。
- v4l2-ctl --list-devices
- b.查看摄像头支持的格式与分辨率
- v4l2-ctl --list-formats-ext -d /dev/video0
- c.摄像头预览(视频640 x 480)
- gst-launch-1.0 v4l2src device=/dev/video0 ! \
- "video/x-raw, width=640, Height=480, framerate=(fraction)30/1" ! \
- queue ! glimagesink
- d.摄像头预览(视频1920 x 1080)
- gst-launch-1.0 v4l2src device=/dev/video0 ! \
- video/x-raw,format=YUY2,width=1920,height=1080 ! \
- queue ! glimagesink
- e.摄像头预览(拍照)
- gst-launch-1.0 v4l2src device=/dev/video0 num-buffers=3 ! \
- jpegenc ! filesink location=~/pic_of_video0.jpg
复制代码//------测试场景 //------USB摄像头测试正常(此处展示的是视频捕捉的测试场景;需要注意,分辨率越高,需要的响应时间越长)
3. 边沿检测USB摄像头的视频流主要通过python调用OpenCV库完成对每一帧图像的边沿检测,参数调节通过滑动条实时调节以获取更好结果,按下ESC键退出测试程序(不得不说python是真的强大): - //------基于OPENCV的摄像头视频边沿检测
- //---指令(python2或者python3均可以):
- python3 ./edge.py 0
- 或者
- python ./edge.py 0
- //---指令中的参数”0“说明:
- 使用USB-Camera,它位于/dev/video0
- //------依赖(根据错误提示安装对应的依赖库及以来组件):
- sudo apt install opencv*
- sudo apt install libopencv*
- sudo apt install gststream*
- sudo apt install python3-opencv*
- //---Log信息:
- zhaomeijing@raspberrypi5:~/workspace/09_uvc$ python3 ./edge.py 0
- Video capture sample.
- Sample shows how VideoCapture class can be used to acquire video
- frames from a camera of a movie file. Also the sample provides
- an example of procedural video generation by an object, mimicking
- the VideoCapture interface (see Chess class).
- 'create_capture' is a convenience function for capture creation,
- falling back to procedural video in case of error.
- Usage:
- video.py [--shotdir <shot path>] [source0] [source1] ...'
- sourceN is an
- - integer number for camera capture
- - name of video file
- - synth:<params> for procedural video
- Synth examples:
- synth:bg=lena.jpg:noise=0.1
- synth:class=chess:bg=lena.jpg:noise=0.1:size=640x480
- Keys:
- ESC - exit
- SPACE - save current frame to <shot path> directory
- [ WARN:0@1.380] global ./modules/videoio/src/cap_gstreamer.cpp (1405) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
- Done
- zhaomeijing@raspberrypi5:~/workspace/09_uvc$
复制代码
3.1 代码解析代码功能说明: - //------代码功能说明:
- a.导入必要的库:cv2、numpy、video 和 sys。
- b.创建两个滑动条:cv.namedWindow('edge') 用来显示边缘检测的结果窗口,cv.createTrackbar('thrs1', 'edge', 2000, 5000, nothing) 和 cv.createTrackbar('thrs2', 'edge', 4000, 5000, nothing) 用来设置边缘检测的参数。
- c.创建一个视频捕获对象:cap = video.create_capture(fn),其中 fn 是指定的视频文件名。
- d.在一个无限循环中,从视频捕获对象中读取帧数据:while True:。
- e.将读取的帧数据转换为灰度图像:gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)。
- f.对灰度图像进行边缘检测:edge = cv.Canny(gray, thrs1, thrs2, apertureSize=5)。
- g.将原始图像和边缘检测结果合并:vis = img.copy(),然后将结果图像中非零像素的部分设为绿色:vis = np.uint8(vis/2.),最后将结果图像显示出来:cv.imshow('edge', vis)。
- h.监听窗口的关闭事件:ch = cv.waitKey(5),如果按下了ESC键,则退出循环:if ch == 27:。
- i.最后打印程序的文档字符串:print(__doc__),并关闭所有窗口:cv.destroyAllWindows()。
复制代码//------完整代码 - #!/usr/bin/env python
- from __future__ import print_function
- import cv2 as cv
- import numpy as np
- # relative module
- import video
- # built-in module
- import sys
- def main():
- try:
- fn = sys.argv[1]
- except:
- fn = 0
- def nothing(*arg):
- pass
- cv.namedWindow('edge')
- cv.createTrackbar('thrs1', 'edge', 2000, 5000, nothing)
- cv.createTrackbar('thrs2', 'edge', 4000, 5000, nothing)
- cap = video.create_capture(fn)
- while True:
- _flag, img = cap.read()
- gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
- thrs1 = cv.getTrackbarPos('thrs1', 'edge')
- thrs2 = cv.getTrackbarPos('thrs2', 'edge')
- edge = cv.Canny(gray, thrs1, thrs2, apertureSize=5)
- vis = img.copy()
- vis = np.uint8(vis/2.)
- vis[edge != 0] = (0, 255, 0)
- cv.imshow('edge', vis)
- ch = cv.waitKey(5)
- if ch == 27:
- break
- print('Done')
- if __name__ == '__main__':
- print(__doc__)
- main()
- cv.destroyAllWindows()
复制代码 3.2 测试效果基于USB-Camera的边沿检测测试结果如图所示:
//------end
|