查看: 2089|回复: 0

[评测分享] 【树莓派5 测评】 + 11.USB-Camera功能测试(zmj)

[复制链接]
  • TA的每日心情
    奋斗
    前天 14:03
  • 签到天数: 249 天

    连续签到: 2 天

    [LV.8]以坛为家I

    发表于 2024-4-30 16:22:05 | 显示全部楼层 |阅读模式
    分享到:
    【树莓派5 测评】 + 11.USB-Camera功能测试(zmj)


    本篇主要介绍在树莓派5开发板测试USB-Camera(UVC)的流程。
    //------UVC / USB-Camera
    UVC通俗的讲就是USB摄像头,全称为USB Video Class,即:USB视频类,是一种为USB视频捕获设备定义的协议标准。
    ---------------------------以下为正文---------------------------
    1.        摄像头接口和设备
    摄像头设备位于” /dev/video*“(此处仅连接了USB摄像头)。
    1. //------查看摄像头设备节点
    2. //---测试指令
    3. a.通过设备列表查看
    4. ls -la /dev/video*
    5. b.通过v4l2-ctl工具查看
    6. v4l2-ctl --list-devices
    7. c.通过media0检查有效摄像头节点
    8. media-ctl -d /dev/media3 -p
    9. d.无效指令:libcamera-hello无法检测UVC,貌似支持CSI接口摄像头
    10. libcamera-hello --list-cameras
    11. //---检查结果
    12. USB 2.0 Camera: USB Camera (usb-xhci-hcd.0-2.1):
    13.         /dev/video0
    14.         /dev/video1
    15.         /dev/media3
    16.         
    17. //------示例Log信息
    18. zhaomeijing@raspberrypi5:~/workspace/09_uvc$ lsusb
    19. Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
    20. Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    21. Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
    22. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    23. zhaomeijing@raspberrypi5:~/workspace/09_uvc$ ls -la /dev/video*
    24. crw-rw----+ 1 root video 81, 18 Apr 26 18:15 /dev/video19
    25. crw-rw----+ 1 root video 81,  0 Apr 26 18:15 /dev/video20
    26. crw-rw----+ 1 root video 81,  1 Apr 26 18:15 /dev/video21
    27. crw-rw----+ 1 root video 81,  2 Apr 26 18:15 /dev/video22
    28. crw-rw----+ 1 root video 81,  3 Apr 26 18:15 /dev/video23
    29. crw-rw----+ 1 root video 81,  4 Apr 26 18:15 /dev/video24
    30. crw-rw----+ 1 root video 81,  5 Apr 26 18:15 /dev/video25
    31. crw-rw----+ 1 root video 81,  6 Apr 26 18:15 /dev/video26
    32. crw-rw----+ 1 root video 81,  7 Apr 26 18:15 /dev/video27
    33. crw-rw----+ 1 root video 81,  8 Apr 26 18:15 /dev/video28
    34. crw-rw----+ 1 root video 81,  9 Apr 26 18:15 /dev/video29
    35. crw-rw----+ 1 root video 81, 10 Apr 26 18:15 /dev/video30
    36. crw-rw----+ 1 root video 81, 11 Apr 26 18:15 /dev/video31
    37. crw-rw----+ 1 root video 81, 12 Apr 26 18:15 /dev/video32
    38. crw-rw----+ 1 root video 81, 13 Apr 26 18:15 /dev/video33
    39. crw-rw----+ 1 root video 81, 14 Apr 26 18:15 /dev/video34
    40. crw-rw----+ 1 root video 81, 15 Apr 26 18:15 /dev/video35
    41. crw-rw----+ 1 root video 81, 16 Apr 26 18:15 /dev/video36
    42. crw-rw----+ 1 root video 81, 17 Apr 26 18:15 /dev/video37
    43. zhaomeijing@raspberrypi5:~/workspace/09_uvc$ lsusb
    44. Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
    45. Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    46. Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
    47. Bus 001 Device 004: ID 1b3f:2008 Generalplus Technology Inc. Usb Audio Device
    48. Bus 001 Device 003: ID 0c45:6362 Microdia USB 2.0 Camera
    49. Bus 001 Device 002: ID 1a40:0801 Terminus Technology Inc. USB 2.0 Hub
    50. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    51. zhaomeijing@raspberrypi5:~/workspace/09_uvc$ dmesg | tail -23
    52. [148218.886321] usb 1-2: new high-speed USB device number 2 using xhci-hcd
    53. [148219.034490] usb 1-2: New USB device found, idVendor=1a40, idProduct=0801, bcdDevice= 1.00
    54. [148219.034496] usb 1-2: New USB device strings: Mfr=0, Product=1, SerialNumber=0
    55. [148219.034499] usb 1-2: Product: USB 2.0 Hub
    56. [148219.082777] hub 1-2:1.0: USB hub found
    57. [148219.082819] hub 1-2:1.0: 4 ports detected
    58. [148219.370336] usb 1-2.1: new high-speed USB device number 3 using xhci-hcd
    59. [148219.548936] usb 1-2.1: New USB device found, idVendor=0c45, idProduct=6362, bcdDevice= 0.00
    60. [148219.548944] usb 1-2.1: New USB device strings: Mfr=2, Product=1, SerialNumber=0
    61. [148219.548947] usb 1-2.1: Product: USB 2.0 Camera
    62. [148219.548949] usb 1-2.1: Manufacturer: Sonix Technology Co., Ltd.
    63. [148219.694326] usb 1-2.3: new full-speed USB device number 4 using xhci-hcd
    64. [148219.815720] usb 1-2.3: config 1 has an invalid interface number: 3 but max is 2
    65. [148219.815725] usb 1-2.3: config 1 has no interface number 1
    66. [148219.816405] usb 1-2.3: New USB device found, idVendor=1b3f, idProduct=2008, bcdDevice= 1.00
    67. [148219.816409] usb 1-2.3: New USB device strings: Mfr=1, Product=2, SerialNumber=0
    68. [148219.816412] usb 1-2.3: Product: Usb Audio Device
    69. [148219.816415] usb 1-2.3: Manufacturer: Generalplus
    70. [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
    71. [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
    72. [148220.026566] usb 1-2.1: Found UVC 1.00 device USB 2.0 Camera (0c45:6362)
    73. [148220.054790] usbcore: registered new interface driver uvcvideo
    74. [148220.079573] usbcore: registered new interface driver snd-usb-audio
    75. zhaomeijing@raspberrypi5:~/workspace/09_uvc$ ls -la /dev/video*
    76. crw-rw----+ 1 root video 81, 19 Apr 28 11:25 /dev/video0
    77. crw-rw----+ 1 root video 81, 20 Apr 28 11:25 /dev/video1
    78. crw-rw----+ 1 root video 81, 18 Apr 26 18:15 /dev/video19
    79. crw-rw----+ 1 root video 81,  0 Apr 26 18:15 /dev/video20
    80. crw-rw----+ 1 root video 81,  1 Apr 26 18:15 /dev/video21
    81. crw-rw----+ 1 root video 81,  2 Apr 26 18:15 /dev/video22
    82. crw-rw----+ 1 root video 81,  3 Apr 26 18:15 /dev/video23
    83. crw-rw----+ 1 root video 81,  4 Apr 26 18:15 /dev/video24
    84. crw-rw----+ 1 root video 81,  5 Apr 26 18:15 /dev/video25
    85. crw-rw----+ 1 root video 81,  6 Apr 26 18:15 /dev/video26
    86. crw-rw----+ 1 root video 81,  7 Apr 26 18:15 /dev/video27
    87. crw-rw----+ 1 root video 81,  8 Apr 26 18:15 /dev/video28
    88. crw-rw----+ 1 root video 81,  9 Apr 26 18:15 /dev/video29
    89. crw-rw----+ 1 root video 81, 10 Apr 26 18:15 /dev/video30
    90. crw-rw----+ 1 root video 81, 11 Apr 26 18:15 /dev/video31
    91. crw-rw----+ 1 root video 81, 12 Apr 26 18:15 /dev/video32
    92. crw-rw----+ 1 root video 81, 13 Apr 26 18:15 /dev/video33
    93. crw-rw----+ 1 root video 81, 14 Apr 26 18:15 /dev/video34
    94. crw-rw----+ 1 root video 81, 15 Apr 26 18:15 /dev/video35
    95. crw-rw----+ 1 root video 81, 16 Apr 26 18:15 /dev/video36
    96. crw-rw----+ 1 root video 81, 17 Apr 26 18:15 /dev/video37
    97. zhaomeijing@raspberrypi5:~/workspace/09_uvc$ libcamera-hello --list-cameras
    98. No cameras available!
    99. zhaomeijing@raspberrypi5:~/workspace/09_uvc$ v4l2-ctl --list-devices
    100. pispbe (platform:1000880000.pisp_be):
    101.         /dev/video20
    102.         /dev/video21
    103.         /dev/video22
    104.         /dev/video23
    105.         /dev/video24
    106.         /dev/video25
    107.         /dev/video26
    108.         /dev/video27
    109.         /dev/video28
    110.         /dev/video29
    111.         /dev/video30
    112.         /dev/video31
    113.         /dev/video32
    114.         /dev/video33
    115.         /dev/video34
    116.         /dev/video35
    117.         /dev/video36
    118.         /dev/video37
    119.         /dev/media0
    120.         /dev/media1

    121. rpivid (platform:rpivid):
    122.         /dev/video19
    123.         /dev/media2

    124. USB 2.0 Camera: USB Camera (usb-xhci-hcd.0-2.1):
    125.         /dev/video0
    126.         /dev/video1
    127.         /dev/media3

    128. zhaomeijing@raspberrypi5:~/workspace/09_uvc$ media-ctl -d /dev/media3 -p
    129. Media controller API version 6.6.20

    130. Media device information
    131. ------------------------
    132. driver          uvcvideo
    133. model           USB 2.0 Camera: USB Camera
    134. serial
    135. bus info        usb-xhci-hcd.0-2.1
    136. hw revision     0x0
    137. driver version  6.6.20

    138. Device topology
    139. - entity 1: USB 2.0 Camera: USB Camera (1 pad, 1 link)
    140.             type Node subtype V4L flags 1
    141.             device node name /dev/video0
    142.         pad0: Sink
    143.                 <- "Extension 3":1 [ENABLED,IMMUTABLE]

    144. - entity 4: USB 2.0 Camera: USB Camera (0 pad, 0 link)
    145.             type Node subtype V4L flags 0
    146.             device node name /dev/video1

    147. - entity 8: Extension 3 (2 pads, 2 links)
    148.             type V4L2 subdev subtype Unknown flags 0
    149.         pad0: Sink
    150.                 <- "Processing 2":1 [ENABLED,IMMUTABLE]
    151.         pad1: Source
    152.                 -> "USB 2.0 Camera: USB Camera":0 [ENABLED,IMMUTABLE]

    153. - entity 11: Processing 2 (2 pads, 2 links)
    154.              type V4L2 subdev subtype Unknown flags 0
    155.         pad0: Sink
    156.                 <- "Camera 1":0 [ENABLED,IMMUTABLE]
    157.         pad1: Source
    158.                 -> "Extension 3":0 [ENABLED,IMMUTABLE]

    159. - entity 14: Camera 1 (1 pad, 1 link)
    160.              type V4L2 subdev subtype Sensor flags 0
    161.         pad0: Source
    162.                 -> "Processing 2":0 [ENABLED,IMMUTABLE]

    163. zhaomeijing@raspberrypi5:~/workspace/09_uvc$
    复制代码
    //------USB摄像头状态
    image-20240428153531433.png
    2.        USB摄像头测试
    将USB摄像头插入开发板,将自动安装 uvc 添加设备节点。  
    USB 2.0 Camera: USB Camera (usb-xhci-hcd.0-2.1):
    /dev/video0/dev/video1/dev/media3
    USB摄像头测试内容包括设备节点检测、视频、拍照,测试指令如下:
    1. //------USB-Camera(某宝几十块买的USB摄像头,支持1080P)
    2. a.确认USB摄像头的设备节点,此处为/dev/video0。
    3. v4l2-ctl --list-devices
    4. b.查看摄像头支持的格式与分辨率
    5. v4l2-ctl --list-formats-ext -d /dev/video0
    6. c.摄像头预览(视频640 x 480)
    7. gst-launch-1.0 v4l2src device=/dev/video0 ! \
    8. "video/x-raw, width=640, Height=480, framerate=(fraction)30/1" ! \
    9. queue ! glimagesink
    10. d.摄像头预览(视频1920 x 1080)
    11. gst-launch-1.0 v4l2src device=/dev/video0 ! \
    12. video/x-raw,format=YUY2,width=1920,height=1080 ! \
    13. queue ! glimagesink
    14. e.摄像头预览(拍照)
    15. gst-launch-1.0 v4l2src device=/dev/video0 num-buffers=3 ! \
    16. jpegenc ! filesink location=~/pic_of_video0.jpg
    复制代码
    //------测试场景
    image-20240428164213135.png
    //------USB摄像头测试正常(此处展示的是视频捕捉的测试场景;需要注意,分辨率越高,需要的响应时间越长)
    image-20240428164232585.png

    3.        边沿检测
    USB摄像头的视频流主要通过python调用OpenCV库完成对每一帧图像的边沿检测,参数调节通过滑动条实时调节以获取更好结果,按下ESC键退出测试程序(不得不说python是真的强大):
    1. //------基于OPENCV的摄像头视频边沿检测
    2. //---指令(python2或者python3均可以):
    3. python3 ./edge.py 0
    4. 或者
    5. python ./edge.py 0
    6. //---指令中的参数”0“说明:
    7. 使用USB-Camera,它位于/dev/video0
    8. //------依赖(根据错误提示安装对应的依赖库及以来组件):
    9. sudo apt install opencv*
    10. sudo apt install libopencv*
    11. sudo apt install gststream*
    12. sudo apt install python3-opencv*
    13. //---Log信息:
    14. zhaomeijing@raspberrypi5:~/workspace/09_uvc$ python3 ./edge.py 0

    15. Video capture sample.

    16. Sample shows how VideoCapture class can be used to acquire video
    17. frames from a camera of a movie file. Also the sample provides
    18. an example of procedural video generation by an object, mimicking
    19. the VideoCapture interface (see Chess class).

    20. 'create_capture' is a convenience function for capture creation,
    21. falling back to procedural video in case of error.

    22. Usage:
    23.     video.py [--shotdir <shot path>] [source0] [source1] ...'

    24.     sourceN is an
    25.      - integer number for camera capture
    26.      - name of video file
    27.      - synth:<params> for procedural video

    28. Synth examples:
    29.     synth:bg=lena.jpg:noise=0.1
    30.     synth:class=chess:bg=lena.jpg:noise=0.1:size=640x480

    31. Keys:
    32.     ESC    - exit
    33.     SPACE  - save current frame to <shot path> directory


    34. [ 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
    35. Done
    36. zhaomeijing@raspberrypi5:~/workspace/09_uvc$
    复制代码

    3.1        代码解析
    代码功能说明:
    1. //------代码功能说明:
    2. a.导入必要的库:cv2、numpy、video 和 sys。
    3. b.创建两个滑动条:cv.namedWindow('edge') 用来显示边缘检测的结果窗口,cv.createTrackbar('thrs1', 'edge', 2000, 5000, nothing) 和 cv.createTrackbar('thrs2', 'edge', 4000, 5000, nothing) 用来设置边缘检测的参数。
    4. c.创建一个视频捕获对象:cap = video.create_capture(fn),其中 fn 是指定的视频文件名。
    5. d.在一个无限循环中,从视频捕获对象中读取帧数据:while True:。
    6. e.将读取的帧数据转换为灰度图像:gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)。
    7. f.对灰度图像进行边缘检测:edge = cv.Canny(gray, thrs1, thrs2, apertureSize=5)。
    8. g.将原始图像和边缘检测结果合并:vis = img.copy(),然后将结果图像中非零像素的部分设为绿色:vis = np.uint8(vis/2.),最后将结果图像显示出来:cv.imshow('edge', vis)。
    9. h.监听窗口的关闭事件:ch = cv.waitKey(5),如果按下了ESC键,则退出循环:if ch == 27:。
    10. i.最后打印程序的文档字符串:print(__doc__),并关闭所有窗口:cv.destroyAllWindows()。
    复制代码
    //------完整代码
    1. #!/usr/bin/env python

    2. from __future__ import print_function

    3. import cv2 as cv
    4. import numpy as np

    5. # relative module
    6. import video

    7. # built-in module
    8. import sys

    9. def main():
    10.     try:
    11.         fn = sys.argv[1]
    12.     except:
    13.         fn = 0

    14.     def nothing(*arg):
    15.         pass

    16.     cv.namedWindow('edge')
    17.     cv.createTrackbar('thrs1', 'edge', 2000, 5000, nothing)
    18.     cv.createTrackbar('thrs2', 'edge', 4000, 5000, nothing)

    19.     cap = video.create_capture(fn)
    20.     while True:
    21.         _flag, img = cap.read()
    22.         gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    23.         thrs1 = cv.getTrackbarPos('thrs1', 'edge')
    24.         thrs2 = cv.getTrackbarPos('thrs2', 'edge')
    25.         edge = cv.Canny(gray, thrs1, thrs2, apertureSize=5)
    26.         vis = img.copy()
    27.         vis = np.uint8(vis/2.)
    28.         vis[edge != 0] = (0, 255, 0)
    29.         cv.imshow('edge', vis)
    30.         ch = cv.waitKey(5)
    31.         if ch == 27:
    32.             break

    33.     print('Done')


    34. if __name__ == '__main__':
    35.     print(__doc__)
    36.     main()
    37.     cv.destroyAllWindows()
    复制代码
    3.2        测试效果
    基于USB-Camera的边沿检测测试结果如图所示:
    image-20240428201913702.png

    //------end

    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-11-15 19:29 , Processed in 0.124067 second(s), 19 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.