视频捕获并不能使用所有受支持的上限

时间:2019-04-18 18:49:10

标签: python linux opencv gstreamer v4l2

我正在NVIDIA Jetson TX2的Ubuntu 18.04上使用Python 3.6。所有软件都是最新的。 Linux内核为 4.9.140-tegra 。 GStreamer是版本 1.14.1

我有一个具有UVC功能的网络摄像头,该摄像头显示以下功能,并且我需要以Y16(GREY16_LE)格式进行流传输:

root@nvidia:/tmp# v4l2-ctl --list-formats-ext -d /dev/video1
ioctl: VIDIOC_ENUM_FMT
        Index       : 0
        Type        : Video Capture
        Pixel Format: 'UYVY'
        Name        : UYVY 4:2:2
                Size: Discrete 160x120
                        Interval: Discrete 0.111s (9.000 fps)

        Index       : 1
        Type        : Video Capture
        Pixel Format: 'Y16 '
        Name        : 16-bit Greyscale
                Size: Discrete 160x120
                        Interval: Discrete 0.111s (9.000 fps)
                Size: Discrete 160x122
                        Interval: Discrete 0.111s (9.000 fps)

        Index       : 2
        Type        : Video Capture
        Pixel Format: 'GREY'
        Name        : 8-bit Greyscale
                Size: Discrete 160x120
                        Interval: Discrete 0.111s (9.000 fps)

        Index       : 3
        Type        : Video Capture
        Pixel Format: 'RGBP'
        Name        : 16-bit RGB 5-6-5
                Size: Discrete 160x120
                        Interval: Discrete 0.111s (9.000 fps)

        Index       : 4
        Type        : Video Capture
        Pixel Format: 'BGR3'
        Name        : 24-bit BGR 8-8-8
                Size: Discrete 160x120
                        Interval: Discrete 0.111s (9.000 fps)

通过命令行中的GStreamer,我可以通过我想要的格式的gst-launch成功使设备进入 PLAYING 状态。

root@nvidia:/tmp# gst-launch-1.0 v4l2src device=/dev/video1 ! video/x-raw,width=160,height=122,format='GRAY16_LE',framerate=9/1 ! fakesink
Setting pipeline to PAUSED ...
Pipeline is live and does not need PREROLL ...
Setting pipeline to PLAYING ...
New clock: GstSystemClock

我正在尝试使用Python OpenCV打开GRY16_LE格式的设备:

import cv2
cap = cv2.VideoCapture("v4l2src device=/dev/video1 ! video/x-raw, format=(string)GRAY16_LE, width=(int)160, height=(int)120, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction)9/1 ! appsink")
ret, frame = cap.read()

在增强的GStreamer调试期间,将探查并打印以下功能:

0:00:00.260383826 15768      0x49a9850 INFO                    v4l2 gstv4l2object.c:4136:gst_v4l2_object_probe_caps:<v4l2src0:src> probed caps: video/x-raw, format=(string)UYVY, width=(int)160, height=(int)120, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction)9/1; video/x-raw, format=(string)BGR, width=(int)160, height=(int)120, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction)9/1; video/x-raw, format=(string)RGB16, width=(int)160, height=(int)120, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction)9/1; video/x-raw, format=(string)GRAY8, width=(int)160, height=(int)120, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction)9/1; video/x-raw, format=(string)GRAY16_LE, width=(int)160, height=(int)122, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction)9/1; video/x-raw, format=(string)GRAY16_LE, width=(int)160, height=(int)120, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction)9/1

启用GStreamer调试后,错误如下所示:

0:00:00.260385266 15768      0x4744c00 INFO              GST_STATES gstelement.c:2579:_priv_gst_element_state_changed:<v4l2src0> notifying about state-changed PAUSED to PLAYING (VOID_PENDING pending)
0:00:00.260529393 15768      0x49a9850 WARN                 basesrc gstbasesrc.c:3055:gst_base_src_loop:<v4l2src0> error: Internal data stream error.
0:00:00.260531697 15768      0x4744c00 INFO              GST_STATES gstbin.c:2952:gst_bin_change_state_func:<pipeline0> child 'v4l2src0' changed state to 4(PLAYING) successfully
0:00:00.260589009 15768      0x49a9850 WARN                 basesrc gstbasesrc.c:3055:gst_base_src_loop:<v4l2src0> error: streaming stopped, reason not-negotiated (-4)
0:00:00.260660881 15768      0x4744c00 INFO              GST_STATES gstbin.c:2087:gst_bin_get_state_func:<pipeline0> getting state
0:00:00.260748913 15768      0x49a9850 INFO        GST_ERROR_SYSTEM gstelement.c:2145:gst_element_message_full_with_details:<v4l2src0> posting message: Internal data stream error.
0:00:00.260762833 15768      0x4744c00 INFO              GST_STATES gstelement.c:2392:gst_element_get_state_func:<pipeline0> waiting for element to commit state
0:00:00.260909520 15768      0x49a9850 INFO        GST_ERROR_SYSTEM gstelement.c:2172:gst_element_message_full_with_details:<v4l2src0> posted error message: Internal data stream error.
0:00:00.261064816 15768      0x4744c00 INFO              GST_STATES gstbin.c:2504:gst_bin_element_set_state:<appsink0> current PAUSED pending VOID_PENDING, desired next READY
0:00:00.261189072 15768      0x49a9850 INFO                    task gsttask.c:316:gst_task_func:<v4l2src0:src> Task going to paused

我使用的是探针返回的相同格式,但错误指示“格式协商错误”。如何解决此问题,使我的框架出现在Python中?谢谢。

2 个答案:

答案 0 :(得分:0)

我不得不修补OpenCV 4源文件中的cap_gstreamer.cpp文件,然后再次构建并安装OpenCV。

到那时,我可以用appsink格式的GRAY16_LE捕获帧。

答案 1 :(得分:0)

我的目标与您相似,并且我可以使用OpenCV捕获Y16原始帧而无需修补.cpp

capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"Y16 "))
capture.set(cv2.CAP_PROP_CONVERT_RGB, False)

我得到的帧直接为uint16格式:)