使用gstreamer和python opencv捕获实时流?

时间:2018-10-18 17:28:42

标签: python opencv gstreamer

首先,我拥有带有Gstreamer库的python 3。

print(cv2.getBuildInformation())

它显示Gstreamer,旁边是YES。

这是在rasperryPi 3中使用gstreamer的发射机代码。

gst-launch-1.0 v4l2src device="/dev/video0" ! video/x-raw,width=320,height=240 ! videoconvert ! x264enc tune=zerolatency ! rtph264pay ! udpsink host='my ip address' port=10000

并且我将使用python代码确定形状,识别对象等。

这是我的python代码:

import numpy as np
import cv2




def receive():
cap = cv2.VideoCapture("udpsrc port=10000 ! application/x-rtp,encoding-name=H264 ! rtpjitterbuffer ! rtph264depay ! avdec_h264 ! videoconvert ! ximagesin ", cv2.CAP_GSTREAMER)

while True:

    ret,frame = cap.read()

    if not ret:
        print('empty frame')
        continue 


    cv2.imshow('receive', frame)
    if cv2.waitKey(1)&0xFF == ord('q'):
        break


cap.release()



receive();

但它始终显示空白帧。

当我在终端上尝试此命令时:

gst-launch-1.0 udpsrc port=10000 ! application/x-rtp,encoding-name=H264 ! rtpjitterbuffer ! rtph264depay ! avdec_h264 ! videoconvert ! ximagesink

它工作正常,所以问题出在我的python端。

那么您有什么建议?

2 个答案:

答案 0 :(得分:1)

cap = cv2.VideoCapture('udpsrc port=7000 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! appsink', cv2.CAP_GSTREAMER)

使用此管道,它将正常工作。 将端口更改为您正在使用的端口。

答案 1 :(得分:0)

我使用不同的发送-接收脚本,这些命令从命令行运行时可以给我带来更好的视频延迟(在更高的分辨率下),但是我不知道如何在Ubuntu端将结果流导入OpenCV。

(我的OpenCV也在ffmpeg和gstreamer开启的情况下安装。)

我在Raspberry pi上的GStreamer发送器代码:

offline_access

在Ubuntu 16.04结束时我的Gstreamer Receiver代码:

gst-launch-1.0 -v v4l2src ! video/x-raw,width=320,height=240 ! videoconvert ! jpegenc ! rtpjpegpay ! udpsink host=192.168.1.101 port=5200 

我希望这将帮助您改善流质量和延迟。 我希望有人可以为我提供OpenCV管道导入代码的示例:)

问候, 彼得·伦克