跳到主要内容

3.4.2 参考示例(C++)

本章节介绍多媒体库开发的多种功能示例,包括摄像头图像采集、视频编解码、视频显示、算法推理等功能。

摄像头图像采集和显示

本示例vio2display示例实现了MIPI摄像头图像采集功能,并通过HDMI接口输出,用户可通过显示器预览画面。

示例流程框图:(待更新)

  • 环境准备:

    • 开发板断电状态下,将MIPI摄像头接入开发板
    • 通过 HDMI 线缆连接开发板和显示器
    • 开发板上电,并通过命令行登录
  • 运行方式: 示例代码以源码形式提供,需要使用make命令进行编译后运行,步骤如下:

    sunrise@ubuntu:~$ cd /app/cdev_demo/vio2display
    sunrise@ubuntu:/app/cdev_demo/vio2display$ make
    sunrise@ubuntu:/app/cdev_demo/vio2display$ ./vio2display -w 1920 -h 1080

    参数说明:

    • -w: sensor 输出宽度
    • -h: sensor 输出高度
  • 预期效果: 程序正确运行后,开发板会通过显示器输出MIPI摄像头采集的实时画面。运行 log 如下:

      sunrise@ubuntu:/tmp/nfs/sp_cdev/cdev_demo/vio2display$ ./vio2display -w 1920 -h 1080
    disp_w=1920, disp_h=1080
    2023/03/28 02:08:03.359 !INFO [x3_cam_init_param][0099]Enable mipi host0 mclk
    2023/03/28 02:08:03.359 !INFO [x3_cam_init_param][0099]Enable mipi host1 mclk
    Camera: gpio_num=114, active=low, i2c_bus=3, mipi_host=0
    Camera: gpio_num=114, active=low, i2c_bus=1, mipi_host=1
    Camera: gpio_num=114, active=low, i2c_bus=0, mipi_host=2
    Camera 0:
    enable: 1
    i2c_bus: 3
    mipi_host: 0
    Camera 1:
    enable: 1
    i2c_bus: 1
    mipi_host: 1
    Camera 2:
    enable: 1
    i2c_bus: 0
    mipi_host: 2
    cmd=i2ctransfer -y -f 3 w2@0x10 0x0 0x0 r1 2>&1, result=0x02

    Found sensor:imx219 on i2c bus 3, use mipi host 0
    Setting VPS channel-2: src_w:1920, src_h:1080; dst_w:1920, dst_h:1080;
    Setting VPS channel-1: src_w:1920, src_h:1080; dst_w:1920, dst_h:1080;
    sp_open_camera success!
    libiar: hb_disp_set_timing done!

    Press 'q' to Exit !

摄像头图像采集并编码

本示例vio2encoder示例实现了 MIPI 摄像头图像采集功能,并编码后在本地保存,用户可通过显示器预览画面。

示例流程框图:(待更新)

  • 环境准备:

    • 开发板断电状态下,将MIPI摄像头接入开发板
    • 通过 HDMI 线缆连接开发板和显示器
    • 开发板上电,并通过命令行登录
  • 运行方式: 按照以下命令执行程序 示例代码以源码形式提供,需要使用make命令进行编译后运行,步骤如下:

    sunrise@ubuntu:~$ cd /app/cdev_demo/vio2encoder
    sunrise@ubuntu:/app/cdev_demo/vio2encoder$ sudo make
    sunrise@ubuntu:/app/cdev_demo/vio2encoder$ sudo ./vio2encoder -w 1920 -h 1080 --iwidth 1920 --iheight 1080 -o stream.h264

    参数说明:

    • -w: 编码视频宽度
    • -h: 编码视频高度
    • --iwidth: sensor 输出宽度
    • --iheight: sensor 输出高度
    • -o: 编码输出路径
  • 预期效果: 程序正确运行后,在当前目录下会生成名为stream.h264的视频文件。运行 log 如下:

    sunrise@ubuntu:/tmp/nfs/sp_cdev/cdev_demo/vio2encoder$ sudo ./vio2encoder -w 1920 -h 1080 --iwidth 1920 --iheight 1080 -o stream.h264
    2023/03/28 02:27:32.560 !INFO [x3_cam_init_param][0099]Enable mipi host0 mclk
    2023/03/28 02:27:32.561 !INFO [x3_cam_init_param][0099]Enable mipi host1 mclk
    Camera: gpio_num=114, active=low, i2c_bus=3, mipi_host=0
    Camera: gpio_num=114, active=low, i2c_bus=1, mipi_host=1
    Camera: gpio_num=114, active=low, i2c_bus=0, mipi_host=2
    Camera 0:
    enable: 1
    i2c_bus: 3
    mipi_host: 0
    Camera 1:
    enable: 1
    i2c_bus: 1
    mipi_host: 1
    Camera 2:
    enable: 1
    i2c_bus: 0
    mipi_host: 2
    cmd=i2ctransfer -y -f 3 w2@0x10 0x0 0x0 r1 2>&1, result=0x02

    Found sensor:imx219 on i2c bus 3, use mipi host 0
    Setting VPS channel-2: src_w:1920, src_h:1080; dst_w:1920, dst_h:1080;
    Setting VPS channel-1: src_w:1920, src_h:1080; dst_w:1920, dst_h:1080;
    sp_open_camera success!
    sp_start_encode success!
    sp_module_bind(vio -> encoder) success!

VPS 缩放示例

本示例实现了基于视频处理模块VPS的视频缩放功能,用户可通过显示器预览画面。

  • 环境准备:

    • 通过 HDMI 线缆连接开发板和显示器
    • 开发板上电,并通过命令行登录
    • 准备图像(NV12)、视频文件(H264)作为输入
  • 运行方式: 示例代码以源码形式提供,需要使用make命令进行编译后运行,步骤如下:

    sunrise@ubuntu:~$ cd /app/cdev_demo/vps
    sunrise@ubuntu:/app/cdev_demo/vps$ sudo make
    sunrise@ubuntu:/app/cdev_demo/vps$ sudo ./vps -m 1 -i stream.h264 -o output.yuv --iheight 1080 --iwidth 1920 --oheight 720 --owidth 1280

    参数配置:

    • -i: 待操作的文件路径
    • --iheight: 输入高度
    • --iwidth: 输入宽度
    • -m: 输入模式,1:视频流;2:NV12 图片
    • -o: 输出路径
    • --oheight: 输出高度
    • --owidth: 输出宽度
    • --skip:(可选)对于视频流输入,调过开头的帧数
  • 预期效果: 程序正确运行后,当前目录会保存处理后的图像文件outpu.yuv。运行 log 如下:

    sunrise@ubuntu:/app/cdev_demo/vps$ sudo ./vps -m 1 -i stream.h264 -o output.yuv --iheight 1080 --iwidth 1920 --oheight 720 --owidth 1280

    ... 省略 ...
    2000/01/01 18:57:31.330 !INFO [CamInitVseParam][0231]Setting VSE channel-0: input_width:1920, input_height:1080, dst_w:1280, dst_h:720
    ... 省略 ...

目标检测算法—fcos

本示例基于fcos模型,实现了本地视频流的目标检测算法功能,用户可通过显示器预览检测结果。

  • 环境准备:

    • 通过 HDMI 线缆连接开发板和显示器
    • 开发板上电,并通过命令行登录
    • 准备视频文件(H264)作为输入
  • 运行方式: 示例代码以源码形式提供,需要使用make命令进行编译后运行,步骤如下:

    sunrise@ubuntu:~$ cd /app/cdev_demo/bpu/src
    sunrise@ubuntu:/app/cdev_demo/bpu/src$ sudo make
    sunrise@ubuntu:/app/cdev_demo/bpu/src$ cd bin
    sunrise@ubuntu:/app/cdev_demo/bpu/src/bin$ sudo ./sample -f /app/model/basic/fcos_512x512_nv12.bin -m 1 -i 1080p_.h264 -w 1920 -h 1080

    参数配置:

    • -f: 模型文件路径
    • -h: 输入视频的高度
    • -w: 输入视频的宽度
    • -i: 输入视频的路径
    • -m: 模型类型,默认为 1
  • 预期效果: 程序正确运行后,会通过HDMI接口输出视频和算法检测渲染后的画面,用户可通过显示器预览。运行 log 如下:

    sunrise@ubuntu:/app/cdev_demo/bpu/src/bin$ sudo ./sample -f /app/model/basic/fcos_512x512_nv12.bin -m 1 -i 1080p_.h264 -w 1920 -h 1080
    [BPU_PLAT]BPU Platform Version(1.3.1)!
    [HBRT] set log level as 0. version = 3.14.5
    [DNN] Runtime version = 1.9.7_(3.14.5 HBRT)
    Model info:
    model_name: fcos_512x512_nv12Input count: 1input[0]: tensorLayout: 2 tensorType: 1 validShape:(1, 3, 512, 512, ), alignedShape:(1, 3, 512, 512, )
    Output count: 15Output[0]: tensorLayout: 0 tensorType: 13 validShape:(1, 64, 64, 80, ), alignedShape:(1, 64, 64, 80, )
    Output[1]: tensorLayout: 0 tensorType: 13 validShape:(1, 32, 32, 80, ), alignedShape:(1, 32, 32, 80, )
    Output[2]: tensorLayout: 0 tensorType: 13 validShape:(1, 16, 16, 80, ), alignedShape:(1, 16, 16, 80, )
    Output[3]: tensorLayout: 0 tensorType: 13 validShape:(1, 8, 8, 80, ), alignedShape:(1, 8, 8, 80, )
    Output[4]: tensorLayout: 0 tensorType: 13 validShape:(1, 4, 4, 80, ), alignedShape:(1, 4, 4, 80, )
    Output[5]: tensorLayout: 0 tensorType: 13 validShape:(1, 64, 64, 4, ), alignedShape:(1, 64, 64, 4, )
    Output[6]: tensorLayout: 0 tensorType: 13 validShape:(1, 32, 32, 4, ), alignedShape:(1, 32, 32, 4, )
    Output[7]: tensorLayout: 0 tensorType: 13 validShape:(1, 16, 16, 4, ), alignedShape:(1, 16, 16, 4, )
    Output[8]: tensorLayout: 0 tensorType: 13 validShape:(1, 8, 8, 4, ), alignedShape:(1, 8, 8, 4, )
    Output[9]: tensorLayout: 0 tensorType: 13 validShape:(1, 4, 4, 4, ), alignedShape:(1, 4, 4, 4, )
    Output[10]: tensorLayout: 0 tensorType: 13 validShape:(1, 64, 64, 1, ), alignedShape:(1, 64, 64, 1, )
    Output[11]: tensorLayout: 0 tensorType: 13 validShape:(1, 32, 32, 1, ), alignedShape:(1, 32, 32, 1, )
    Output[12]: tensorLayout: 0 tensorType: 13 validShape:(1, 16, 16, 1, ), alignedShape:(1, 16, 16, 1, )
    Output[13]: tensorLayout: 0 tensorType: 13 validShape:(1, 8, 8, 1, ), alignedShape:(1, 8, 8, 1, )
    Output[14]: tensorLayout: 0 tensorType: 13 validShape:(1, 4, 4, 1, ), alignedShape:(1, 4, 4, 1, )
    libiar: hb_disp_set_timing done!
    dispaly init ret = 0
    vps open ret = 0
    module bind vps & display ret = 0
    display start ret = 0
    [x3_av_open_stream]:[380]:probesize: 5000000
    decode start ret = 0
    module bind decoder & vps ret = 0
    [ERROR]["vdec"][video/src/vdec_group.c:348] [8870.450264]vdec_channel_bump_thread[348]: VDEC_MODULE module try again

    [draw_rect]:[137]:========point is 0,return========
    fps:55.555556,processing time:18

目标检测算法—YOLOv5s

本示例基于YOLOv5模型,实现了摄像头目标检测算法功能,用户可通过显示器预览检测结果。

  • 环境准备:

    • 开发板断电状态下,将MIPI摄像头接入开发板
    • 通过 HDMI 线缆连接开发板和显示器
    • 开发板上电,并通过命令行登录
  • 运行方式: 示例代码以源码形式提供,需要使用make命令进行编译后运行,步骤如下:

    sunrise@ubuntu:~$ cd /app/cdev_demo/bpu/src
    sunrise@ubuntu:/app/cdev_demo/bpu/src$ sudo make
    sunrise@ubuntu:/app/cdev_demo/bpu/src$ cd bin
    sunrise@ubuntu:/app/cdev_demo/bpu/src/bin$ sudo ./sample -f /app/model/basic/yolov5s_672x672_nv12.bin -m 0

    参数配置:

    • -f: 模型的路径
    • -m: 模型类型,默认为 0
  • 预期效果: 程序正确运行后,会通过HDMI接口输出视频和算法检测渲染后的画面,用户可通过显示器预览。运行 log 如下:

    sunrise@ubuntu:/app/cdev_demo/bpu/src/bin$ sudo ./sample -f /app/model/basic/yolov5s_672x672_nv12.bin -m 0
    [BPU_PLAT]BPU Platform Version(1.3.6)!
    [HBRT] set log level as 0. version = 3.15.49.0
    [DNN] Runtime version = 1.23.8_(3.15.49 HBRT)
    [A][DNN][packed_model.cpp:247][Model](2000-01-01,19:06:39.821.214) [HorizonRT] The model builder version = 1.23.5
    [W][DNN]bpu_model_info.cpp:491][Version](2000-01-01,19:06:39.876.293) Model: yolov5s_v2_672x672_bayese_nv12. Inconsistency between the hbrt library version 3.15.49.0 and the model build version 3.15.47.0 detected, in order to ensure correct model results, it is recommended to use compilation tools and the BPU SDK from the same OpenExplorer package.
    Model info:
    model_name: yolov5s_v2_672x672_bayese_nv12Input count: 1input[0]: tensorLayout: 2 tensorType: 1 validShape:(1, 3, 672, 672, ), alignedShape:(1, 3, 672, 672, )
    Output count: 3Output[0]: tensorLayout: 0 tensorType: 13 validShape:(1, 84, 84, 255, ), alignedShape:(1, 84, 84, 255, )
    Output[1]: tensorLayout: 0 tensorType: 13 validShape:(1, 42, 42, 255, ), alignedShape:(1, 42, 42, 255, )
    Output[2]: tensorLayout: 0 tensorType: 13 validShape:(1, 21, 21, 255, ), alignedShape:(1, 21, 21, 255, )
    [INFO] board_id is 301, not need skip sci1.
    Searching camera sensor on device: /proc/device-tree/soc/cam/vcon@0 i2c bus: 6 mipi rx phy: 0
    INFO: Found sensor name:ov5647 on mipi rx csi 0, i2c addr 0x36, config_file:linear_1920x1080_raw10_30fps_2lane.c
    2000/01/01 19:06:40.012 !INFO [CamInitParam][0139]Setting VSE channel-0: input_width:1920, input_height:1080, dst_w:672, dst_h:672
    2000/01/01 19:06:40.013 !INFO [CamInitParam][0139]Setting VSE channel-1: input_width:1920, input_height:1080, dst_w:1920, dst_h:1080
    ... 省略 ...