使用UVC模块对USB摄像头操作时的mode select失败问题

Viewed 43

重现步骤

1,连接Logicool HD Pro Webcam C920外接USB摄像头
2,执行以下UVC API手册的6. 最佳实践的代码
https://developer.canaan-creative.com/k230_canmv/zh/main/zh/api/mpp/media_uvc.html

期待结果和实际结果

期待能看到USB摄像头的拍摄影像,但执行中,
mode select时会发生以下错误:
select mode success: False, mode: {"width":640, "height":480, "format":mjpeg, "fps":30.00}

软硬件版本信息

开发板:01Studio CanMV K230
使用镜像:CanMV-K230_01Studio_micropython_v1.2.2-0-g4b8cae1_nncase_v2.9.0.img

错误日志

CanMV v1.2.2(based on Micropython e00a144) on 2025-04-23; k230_canmv_01studio with K230
vb common pool count 3
detect USB Camera #HD Pro Webcam C920
模式0: 640x480 0@30.00003fps
模式1: 640x480 0@24.00004fps
模式2: 640x480 0@20.0fps
模式3: 640x480 0@15.00002fps
模式4: 640x480 0@10.0fps
模式5: 640x480
模式6: 640x480
模式7: 160x90 0@30.00003fps
模式8: 160x90 0@24.00004fps
模式9: 160x90 0@20.0fps
模式10: 160x90 0@15.00002fps
模式11: 160x90 0@10.0fps
模式12: 160x90
模式13: 160x90
模式14: 160x120 0@30.00003fps
模式15: 160x120 0@24.00004fps
模式16: 160x120 0@20.0fps
模式17: 160x120 0@15.00002fps
模式18: 160x120 0@10.0fps
模式19: 160x120
模式20: 160x120
模式21: 176x144 0@30.00003fps
模式22: 176x144 0@24.00004fps
模式23: 176x144 0@20.0fps
模式24: 176x144 0@15.00002fps
模式25: 176x144 0@10.0fps
模式26: 176x144
模式27: 176x144
模式28: 320x180 0@30.00003fps
模式29: 320x180 0@24.00004fps
模式30: 320x180 0@20.0fps
模式31: 320x180 0@15.00002fps
模式32: 320x180 0@10.0fps
模式33: 320x180
模式34: 320x180
模式35: 320x240 0@30.00003fps
模式36: 320x240 0@24.00004fps
模式37: 320x240 0@20.0fps
模式38: 320x240 0@15.00002fps
模式39: 320x240 0@10.0fps
模式40: 320x240
模式41: 320x240
模式42: 352x288 0@30.00003fps
模式43: 352x288 0@24.00004fps
模式44: 352x288 0@20.0fps
模式45: 352x288 0@15.00002fps
模式46: 352x288 0@10.0fps
模式47: 352x288
模式48: 352x288
模式49: 432x240 0@30.00003fps
模式50: 432x240 0@24.00004fps
模式51: 432x240 0@20.0fps
模式52: 432x240 0@15.00002fps
模式53: 432x240 0@10.0fps
模式54: 432x240
模式55: 432x240
模式56: 640x360 0@30.00003fps
模式57: 640x360 0@24.00004fps
模式58: 640x360 0@20.0fps
模式59: 640x360 0@15.00002fps
模式60: 640x360 0@10.0fps
模式61: 640x360
模式62: 640x360
模式63: 800x448 0@30.00003fps
模式64: 800x448 0@24.00004fps
模式65: 800x448 0@20.0fps
模式66: 800x448 0@15.00002fps
模式67: 800x448 0@10.0fps
模式68: 800x448
模式69: 800x448
模式70: 800x600 0@24.00004fps
模式71: 800x600 0@20.0fps
模式72: 800x600 0@15.00002fps
模式73: 800x600 0@10.0fps
模式74: 800x600
模式75: 800x600
模式76: 864x480 0@24.00004fps
模式77: 864x480 0@20.0fps
模式78: 864x480 0@15.00002fps
模式79: 864x480 0@10.0fps
模式80: 864x480
模式81: 864x480
模式82: 960x720 0@15.00002fps
模式83: 960x720 0@10.0fps
模式84: 960x720
模式85: 960x720
模式86: 1024x576 0@15.00002fps
模式87: 1024x576 0@10.0fps
模式88: 1024x576
模式89: 1024x576
模式90: 1280x720 0@10.0fps
模式91: 1280x720
模式92: 1280x720
模式93: 1600x896
模式94: 1600x896
模式95: 1920x1080
模式96: 2304x1296
模式97: 2304x1536
select mode success: False, mode: {"width":640, "height":480, "format":mjpeg, "fps":30.00}

尝试解决过程

在UVC API 手册的网页有部分提问和回复内容
https://developer.canaan-creative.com/k230_canmv/zh/main/zh/api/mpp/media_uvc.html

补充材料

测试代码如下:

import time, os, urandom, sys

from media.display import *
from media.media import *
from media.uvc import *

DISPLAY_WIDTH = ALIGN_UP(640, 16)
DISPLAY_HEIGHT = 480

# use lcd as display output
Display.init(Display.ST7701, width = DISPLAY_WIDTH, height = DISPLAY_HEIGHT, to_ide = True)
# init media manager
MediaManager.init()

while True:
    plugin, dev = UVC.probe()
    if plugin:
        print(f"detect USB Camera {dev}")
        break


for i, mode in enumerate(UVC.list_video_mode()):
    print(f"模式{i}: {mode.width}x{mode.height} {mode.format}@{mode.fps}fps")

mode = UVC.video_mode(640, 480, UVC.FORMAT_MJPEG, 30)

#mode = UVC.video_mode(256, 192,UVC.FORMAT_MJPEG, 25)
#mode = UVC.video_mode()

succ, mode = UVC.select_video_mode(mode)
print(f"select mode success: {succ}, mode: {mode}")

#UVC.start()
success = UVC.start(delay_ms=1000, cvt=False)

while True:
    img = UVC.snapshot()
    if img is not None:
        img = img.to_rgb565()
        Display.show_image(img)

# deinit display
Display.deinit()
UVC.stop()
time.sleep_ms(100)
# release media buffer
MediaManager.deinit()

3 Answers

用串口看一下摄像头插上后得日志输出。

[E/USB] ++Disconnect Detected Interrupt++ (Host) a_host
[E/USB] ++Disconnect Detected Interrupt++ (Host) a_host
[W/usbh_hub] Failed to enable port 1
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x00
        bAlternateSetting: 0x00
        bNumEndpoints: 0x01
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x01
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x01
        bAlternateSetting: 0x00
        bNumEndpoints: 0x00
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x01
        bAlternateSetting: 0x01
        bNumEndpoints: 0x01
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x01
        bAlternateSetting: 0x02
        bNumEndpoints: 0x01
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x01
        bAlternateSetting: 0x03
        bNumEndpoints: 0x01
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x01
        bAlternateSetting: 0x04
        bNumEndpoints: 0x01
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x01
        bAlternateSetting: 0x05
        bNumEndpoints: 0x01
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x01
        bAlternateSetting: 0x06
        bNumEndpoints: 0x01
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x01
        bAlternateSetting: 0x07
        bNumEndpoints: 0x01
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x01
        bAlternateSetting: 0x08
        bNumEndpoints: 0x01
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x01
        bAlternateSetting: 0x09
        bNumEndpoints: 0x01
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x01
        bAlternateSetting: 0x0a
        bNumEndpoints: 0x01
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x01
        bAlternateSetting: 0x0b
        bNumEndpoints: 0x01
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x02
        bAlternateSetting: 0x00
        bNumEndpoints: 0x00
        bInterfaceClass: 0x01
        bInterfaceSubClass: 0x01
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x03
        bAlternateSetting: 0x00
        bNumEndpoints: 0x00
        bInterfaceClass: 0x01
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x03
        bAlternateSetting: 0x01
        bNumEndpoints: 0x01
        bInterfaceClass: 0x01
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x03
        bAlternateSetting: 0x02
        bNumEndpoints: 0x01
        bInterfaceClass: 0x01
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x03
        bAlternateSetting: 0x03
        bNumEndpoints: 0x01
        bInterfaceClass: 0x01
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
bcdVDC:0100
Num of altsettings:12
Ingore altsetting 0
Altsetting:1, Ep=81 Attr=05 Mps=192 Interval=01 Mult=00
Altsetting:2, Ep=81 Attr=05 Mps=384 Interval=01 Mult=00
Altsetting:3, Ep=81 Attr=05 Mps=512 Interval=01 Mult=00
Altsetting:4, Ep=81 Attr=05 Mps=640 Interval=01 Mult=00
Altsetting:5, Ep=81 Attr=05 Mps=800 Interval=01 Mult=00
Altsetting:6, Ep=81 Attr=05 Mps=944 Interval=01 Mult=00
Altsetting:7, Ep=81 Attr=05 Mps=640 Interval=01 Mult=01
Altsetting:8, Ep=81 Attr=05 Mps=800 Interval=01 Mult=01
Altsetting:9, Ep=81 Attr=05 Mps=992 Interval=01 Mult=01
Altsetting:10, Ep=81 Attr=05 Mps=896 Interval=01 Mult=02
Altsetting:11, Ep=81 Attr=05 Mps=1020 Interval=01 Mult=02
bNumFormats:3
  FormatIndex:1
  FormatType:uncompressed
  bNumFrames:19
  Resolution:
      FrameIndex:1
      wWidth:  640, wHeight:  480, dwDefaultFrameInterval: 333333
      FrameIndex:2
      wWidth:  160, wHeight:   90, dwDefaultFrameInterval: 333333
      FrameIndex:3
      wWidth:  160, wHeight:  120, dwDefaultFrameInterval: 333333
      FrameIndex:4
      wWidth:  176, wHeight:  144, dwDefaultFrameInterval: 333333
      FrameIndex:5
      wWidth:  320, wHeight:  180, dwDefaultFrameInterval: 333333
      FrameIndex:6
      wWidth:  320, wHeight:  240, dwDefaultFrameInterval: 333333
      FrameIndex:7
      wWidth:  352, wHeight:  288, dwDefaultFrameInterval: 333333
      FrameIndex:8
      wWidth:  432, wHeight:  240, dwDefaultFrameInterval: 333333
      FrameIndex:9
      wWidth:  640, wHeight:  360, dwDefaultFrameInterval: 333333
      FrameIndex:10
      wWidth:  800, wHeight:  448, dwDefaultFrameInterval: 333333
      FrameIndex:11
      wWidth:  800, wHeight:  600, dwDefaultFrameInterval: 416666
      FrameIndex:12
      wWidth:  864, wHeight:  480, dwDefaultFrameInterval: 416666
      FrameIndex:13
      wWidth:  960, wHeight:  720, dwDefaultFrameInterval: 666666
      FrameIndex:14
      wWidth: 1024, wHeight:  576, dwDefaultFrameInterval: 666666
      FrameIndex:15
      wWidth: 1280, wHeight:  720, dwDefaultFrameInterval: 1000000
      FrameIndex:16
      wWidth: 1600, wHeight:  896, dwDefaultFrameInterval: 1333333
      FrameIndex:17
      wWidth: 1920, wHeight: 1080, dwDefaultFrameInterval: 2000000
      FrameIndex:18
      wWidth: 2304, wHeight: 1296, dwDefaultFrameInterval: 4999998
      FrameIndex:19
      wWidth: 2304, wHeight: 1536, dwDefaultFrameInterval: 4999998
  FormatIndex:2
  FormatType:uncompressed
  bNumFrames:0
  Resolution:
  FormatIndex:3
  FormatType:mjpeg
  bNumFrames:17
  Resolution:
      FrameIndex:1
      wWidth:  640, wHeight:  480, dwDefaultFrameInterval: 333333
      FrameIndex:2
      wWidth:  160, wHeight:   90, dwDefaultFrameInterval: 333333
      FrameIndex:3
      wWidth:  160, wHeight:  120, dwDefaultFrameInterval: 333333
      FrameIndex:4
      wWidth:  176, wHeight:  144, dwDefaultFrameInterval: 333333
      FrameIndex:5
      wWidth:  320, wHeight:  180, dwDefaultFrameInterval: 333333
      FrameIndex:6
      wWidth:  320, wHeight:  240, dwDefaultFrameInterval: 333333
      FrameIndex:7
      wWidth:  352, wHeight:  288, dwDefaultFrameInterval: 333333
      FrameIndex:8
      wWidth:  432, wHeight:  240, dwDefaultFrameInterval: 333333
      FrameIndex:9
      wWidth:  640, wHeight:  360, dwDefaultFrameInterval: 333333
      FrameIndex:10
      wWidth:  800, wHeight:  448, dwDefaultFrameInterval: 333333
      FrameIndex:11
      wWidth:  800, wHeight:  600, dwDefaultFrameInterval: 333333
      FrameIndex:12
      wWidth:  864, wHeight:  480, dwDefaultFrameInterval: 333333
      FrameIndex:13
      wWidth:  960, wHeight:  720, dwDefaultFrameInterval: 333333
      FrameIndex:14
      wWidth: 1024, wHeight:  576, dwDefaultFrameInterval: 333333
      FrameIndex:15
      wWidth: 1280, wHeight:  720, dwDefaultFrameInterval: 333333
      FrameIndex:16
      wWidth: 1600, wHeight:  896, dwDefaultFrameInterval: 333333
      FrameIndex:17
      wWidth: 1920, wHeight: 1080, dwDefaultFrameInterval: 333333
[E/usbh_core] do not support, Interface 2, Class:0x01,Subclass:0x01,Protocl:0x00

刚才也试了连接另外一台热成像USB摄像头,似乎不支持mjpeg的模式。
串口的输出如下:

[W/usbh_hub] Failed to enable port 1
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x00
        bAlternateSetting: 0x00
        bNumEndpoints: 0x01
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x01
        bInterfaceProtocol: 0x00
        iInterface: 0x02
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x01
        bAlternateSetting: 0x00
        bNumEndpoints: 0x00
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
Interface Descriptor:
        bLength: 0x09
        bDescriptorType: 0x04
        bInterfaceNumber: 0x01
        bAlternateSetting: 0x01
        bNumEndpoints: 0x01
        bInterfaceClass: 0x0e
        bInterfaceSubClass: 0x02
        bInterfaceProtocol: 0x00
        iInterface: 0x00
bcdVDC:0110
Num of altsettings:2
Ingore altsetting 0
Altsetting:1, Ep=81 Attr=05 Mps=1024 Interval=01 Mult=00
bNumFormats:1
  FormatIndex:1
  FormatType:uncompressed
  bNumFrames:1
  Resolution:
      FrameIndex:1
      wWidth:  256, wHeight:  192, dwDefaultFrameInterval: 200000