回答

收藏

[评测分享] 【树莓派5 测评】 + 12.UVC扩展功能测试(zmj)

#板卡评测 #板卡评测 2900 人阅读 | 0 人回复 | 2024-04-30

【树莓派5 测评】 + 12.UVC扩展功能测试(zmj)
UVC的有很多好玩的地方,继续推一篇关于UVC的功能测试。
1.        借助lsusb检测UVC更多信息
以前没有着重留意“lsusb”的扩展功能,这次发现它的功能十分强大。
  1. //------指令lsusb
  2. //---基础功能:lsusb
  3. Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
  4. Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
  5. Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
  6. Bus 001 Device 004: ID 1b3f:2008 Generalplus Technology Inc. Usb Audio Device
  7. Bus 001 Device 003: ID 0c45:6362 Microdia USB 2.0 Camera
  8. Bus 001 Device 002: ID 1a40:0801 Terminus Technology Inc. USB 2.0 Hub
  9. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
  10. //---检查接口设备速率:lsusb -t
  11. /:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
  12. /:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/2p, 480M
  13. /:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
  14. /:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/2p, 480M
  15.     |__ Port 2: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
  16.         |__ Port 1: Dev 3, If 0, Class=Video, Driver=uvcvideo, 480M
  17.         |__ Port 1: Dev 3, If 1, Class=Video, Driver=uvcvideo, 480M
  18.         |__ Port 3: Dev 4, If 2, Class=Audio, Driver=snd-usb-audio, 12M
  19.         |__ Port 3: Dev 4, If 0, Class=Audio, Driver=snd-usb-audio, 12M
  20.         |__ Port 3: Dev 4, If 3, Class=Human Interface Device, Driver=usbhid, 12M
  21. //---查看更详细信息:lsusb -v -s 001:003
  22. Bus 001 Device 003: ID 0c45:6362 Microdia USB 2.0 Camera
  23. Couldn't open device, some information will be missing
  24. Device Descriptor:
  25.   bLength                18
  26.   bDescriptorType         1
  27.   bcdUSB               2.00
  28.   bDeviceClass          239 Miscellaneous Device
  29.   bDeviceSubClass         2
  30.   bDeviceProtocol         1 Interface Association
  31.   bMaxPacketSize0        64
  32.   idVendor           0x0c45 Microdia
  33.   idProduct          0x6362
  34.   bcdDevice            0.00
  35.   iManufacturer           2 Sonix Technology Co., Ltd.
  36.   iProduct                1 USB 2.0 Camera
  37.   iSerial                 0
  38.   bNumConfigurations      1
  39.   ...
  40. //------Log信息
  41. zhaomeijing@raspberrypi5:~/workspace/09_uvc$ lsusb
  42. Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
  43. Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
  44. Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
  45. Bus 001 Device 004: ID 1b3f:2008 Generalplus Technology Inc. Usb Audio Device
  46. Bus 001 Device 003: ID 0c45:6362 Microdia USB 2.0 Camera
  47. Bus 001 Device 002: ID 1a40:0801 Terminus Technology Inc. USB 2.0 Hub
  48. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
  49. zhaomeijing@raspberrypi5:~/workspace/09_uvc$ lsusb -h
  50. Usage: lsusb [options]...
  51. List USB devices
  52.   -v, --verbose
  53.       Increase verbosity (show descriptors)
  54.   -s [[bus]:][devnum]
  55.       Show only devices with specified device and/or
  56.       bus numbers (in decimal)
  57.   -d vendor:[product]
  58.       Show only devices with the specified vendor and
  59.       product ID numbers (in hexadecimal)
  60.   -D device
  61.       Selects which device lsusb will examine
  62.   -t, --tree
  63.       Dump the physical USB device hierarchy as a tree
  64.   -V, --version
  65.       Show version of program
  66.   -h, --help
  67.       Show usage and help
  68. zhaomeijing@raspberrypi5:~/workspace/09_uvc$ lsusb -t
  69. /:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
  70. /:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/2p, 480M
  71. /:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
  72. /:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/2p, 480M
  73.     |__ Port 2: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
  74.         |__ Port 1: Dev 3, If 0, Class=Video, Driver=uvcvideo, 480M
  75.         |__ Port 1: Dev 3, If 1, Class=Video, Driver=uvcvideo, 480M
  76.         |__ Port 3: Dev 4, If 2, Class=Audio, Driver=snd-usb-audio, 12M
  77.         |__ Port 3: Dev 4, If 0, Class=Audio, Driver=snd-usb-audio, 12M
  78.         |__ Port 3: Dev 4, If 3, Class=Human Interface Device, Driver=usbhid, 12M
  79. zhaomeijing@raspberrypi5:~/workspace/09_uvc$ lsusb -v -s 001:003

  80. Bus 001 Device 003: ID 0c45:6362 Microdia USB 2.0 Camera
  81. Couldn't open device, some information will be missing
  82. Device Descriptor:
  83.   bLength                18
  84.   bDescriptorType         1
  85.   bcdUSB               2.00
  86.   bDeviceClass          239 Miscellaneous Device
  87.   bDeviceSubClass         2
  88.   bDeviceProtocol         1 Interface Association
  89.   bMaxPacketSize0        64
  90.   idVendor           0x0c45 Microdia
  91.   idProduct          0x6362
  92.   bcdDevice            0.00
  93.   iManufacturer           2 Sonix Technology Co., Ltd.
  94.   iProduct                1 USB 2.0 Camera
  95.   iSerial                 0
  96.   bNumConfigurations      1
  97.     Interface Descriptor:
  98.       bLength                 9
  99.       bDescriptorType         4
  100.       bInterfaceNumber        1
  101.       bAlternateSetting       6
  102.       bNumEndpoints           1
  103.       bInterfaceClass        14 Video
  104.       bInterfaceSubClass      2 Video Streaming
  105.       bInterfaceProtocol      0
  106.       iInterface              0
  107.       Endpoint Descriptor:
  108.         bLength                 7
  109.         bDescriptorType         5
  110.         bEndpointAddress     0x81  EP 1 IN
  111.         bmAttributes            5
  112.           Transfer Type            Isochronous
  113.           Synch Type               Asynchronous
  114.           Usage Type               Data
  115.         wMaxPacketSize     0x1400  3x 1024 bytes
  116.         bInterval               1
  117. zhaomeijing@raspberrypi5:~/workspace/09_uvc$
复制代码
2.        借助OpenCV实现便捷的RGB与灰度转换
下面的代码可捕捉UVC生成的视频流,用BGR颜色和灰色模式展示。
  1. //------测试指令
  2. python3 ./uvc_gray.py
  3. 注:测试界面按“Q”退出,或者指令界面“Ctrl + C”退出。
  4. //------Log信息
  5. zhaomeijing@raspberrypi5:~/workspace/09_uvc$ python3 ./uvc_gray.py
  6. [ WARN:0@1.217] global ./modules/videoio/src/cap_gstreamer.cpp (1405) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
  7. zhaomeijing@raspberrypi5:~/workspace/09_uvc$
  8. //------源码:OpenCV实现UVC灰度转换
  9. import numpy as np
  10. import cv2

  11. cap = cv2.VideoCapture(0)
  12. cap.set(3,640) # set Width
  13. cap.set(4,480) # set Height
  14.   
  15. while(True):
  16.     ret, frame = cap.read()
  17.     frame = cv2.flip(frame, -1) # Flip camera vertically
  18.     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  19.      
  20.     cv2.imshow('frame', frame)
  21.     cv2.imshow('gray', gray)
  22.      
  23.     k = cv2.waitKey(30) & 0xff
  24.     if k == 27: # press 'ESC' to quit
  25.         break

  26. cap.release()
  27. cv2.destroyAllWindows()
复制代码
//------测试结果:
3.        人脸检测
人脸检测有多种方式实现,此处通过python实现。
  1. //------测试指令
  2. python3 ./face_detect.py
  3. 注:测试界面按“Q”退出,或者指令界面“Ctrl + C”退出。
  4. //------源码
  5. import cv2

  6. # 导入人脸级联分类器,'.xml'文件里包含训练出来的人脸特征
  7. #face_engine = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
  8. face_engine = cv2.CascadeClassifier('/usr/share/opencv4/haarcascades/haarcascade_frontalface_alt.xml')
  9. #/usr/share/opencv4/haarcascades/haarcascade_frontalface_alt.xml

  10. # 导入人眼级联分类器,'.xml'文件里包含训练出来的人眼特征
  11. eye_cascade = cv2.CascadeClassifier('/usr/share/opencv4/haarcascades/haarcascade_eye.xml')
  12. #/usr/share/opencv4/haarcascades/haarcascade_eye.xml

  13. # 调用摄像头(读取/ect中的驱动);这里索引是0的原因是添加过驱动了;
  14. cap = cv2.VideoCapture(0)
  15. while (True):
  16.     # 获取摄像头拍摄到的画面
  17.     # 会得到两个参数,一个是否捕捉到图像(True/False),另一个为存放每帧的图像
  18.     ret, frame = cap.read()  # 读取一帧图像
  19.     # 每帧图像放大1.1倍,重复检测10次
  20.     faces = face_engine.detectMultiScale(frame, 1.1, 10)  # 得到人脸,可能不止一个
  21.     img = frame  # 复制
  22.     print(img)
  23.     for (x, y, w, h) in faces:
  24.         # 画出人脸框,蓝色,画笔宽度为2
  25.         img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
  26.         # 框选出人脸区域,在人脸区域而不是全图中进行人眼检测,节省计算资源
  27.         face_area = img[y:y + h, x:x + w]  # 人脸区域
  28.         eyes = eye_cascade.detectMultiScale(face_area, 1.1, 5)
  29.         # 用人眼级联分类器在人脸区域进行人眼识别,返回的eyes为眼睛坐标列表
  30.         for (ex, ey, ew, eh) in eyes:
  31.             # 画出人眼框,绿色,画笔宽度为1
  32.             cv2.rectangle(face_area, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 1)

  33.     # 实时展示效果画面
  34.     #cv2.imshow(''frame'', img)
  35.     cv2.imshow('frame', img)
  36.     # 每5毫秒监听一次键盘动作
  37.     if cv2.waitKey(5) & 0xFF == ord('q'):  # 当按下“q”键时退出人脸检测
  38.         break

  39. # 最后,关闭所有窗口
  40. cap.release()
  41. cv2.destroyAllWindows()  # 释放资源
  42. //------Log信息
  43. zhaomeijing@raspberrypi5:~/workspace/09_uvc$ python3 ./face_detect.py
  44. Xlib:  extension "DRI2" missing on display "localhost:11.0".
  45. Xlib:  extension "DRI2" missing on display "localhost:11.0".
  46. [ WARN:0@3.464] global ./modules/videoio/src/cap_gstreamer.cpp (1405) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
  47. [[[253 253 252]
  48.   [253 253 252]
  49.   [253 253 252]
  50.   ...
  51.   [253 253 252]
  52.   [253 253 252]
  53.   [253 253 252]]
  54.   ...

  55. [[[253 253 252]
  56.   [253 253 252]
  57.   [253 253 252]
  58.   ...
  59.   [249 255 231]
  60.   [248 255 220]
  61.   [205 225 174]]
  62.   
  63. [[ 76  90  70]
  64.   [ 80  90  73]
  65.   [ 86  93  80]
  66.   ...
  67.   [ 46  50  45]
  68.   [ 46  48  43]
  69.   [ 44  46  41]]

  70. [[ 80  93  75]
  71.   [ 78  90  75]
  72.   [ 83  92  79]
  73.   ...
  74.   [ 46  50  45]
  75.   [ 44  49  47]
  76.   [ 42  47  46]]

  77. [[ 82  92  75]
  78.   [ 82  91  77]
  79.   [ 78  88  80]
  80.   ...
  81.   [ 44  51  44]
  82.   [ 42  47  45]
  83.   [ 42  47  46]]]
  84.   
  85. zhaomeijing@raspberrypi5:~/workspace/09_uvc$
复制代码
//------测试结果:
//------end

关注下面的标签,发现更多相似文章
分享到:
回复

使用道具 举报

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

本版积分规则

关闭

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