在 python 中将 gstreamer 管道转换为 opencv
convert gstreamer pipeline to opencv in python
我使用以下 gstreamer 命令创建了一个网络流:
发件人:
gst-launch-1.0 -v videotestsrc ! video/x-raw,framerate=20/1 ! videoscale ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay ! udpsink host=X.X.X.X port=5000
接收者:
gst-launch-1.0 -v udpsrc port=5000 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! autovideosink
这很好用。我现在想在 python 脚本中包含接收方的流。在脚本中我想用opencv做一些视频处理。
有谁知道如何转换所描述的管道,以便它可以与 opencv 一起使用?
谢谢!
编辑1:
发现这应该有效:
cap = cv2.VideoCapture("udpsrc port=5000 ! application/x- rtp,media=video,payload=26,clock-rate=90000,encoding-name=H264, payload=96 ! rtph264depay ! decodebin ! videoconvert ! appsink", cv2.CAP_GSTREAMER)
我收到以下错误:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/nvidia/build-opencv/opencv/modules/highgui/src/window.cpp, line 331 Traceback (most recent call last): File "launchstream_ip.py", line 13, in <module> cv2.imshow('frame', frame) cv2.error: /home/nvidia/build-opencv/opencv/modules/highgui/src/window.cpp:331: error: (-215) size.width>0 && size.height>0 in function imshow
import numpy as np
import cv2
from multiprocessing import Process
def send():
cap_send = cv2.VideoCapture('videotestsrc ! video/x-raw,framerate=20/1 ! videoscale ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
out_send = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000',cv2.CAP_GSTREAMER,0, 20, (320,240), True)
if not cap_send.isOpened() or not out_send.isOpened():
print('VideoCapture or VideoWriter not opened')
exit(0)
while True:
ret,frame = cap_send.read()
if not ret:
print('empty frame')
break
out_send.write(frame)
cv2.imshow('send', frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
cap_send.release()
out_send.release()
def receive():
cap_receive = cv2.VideoCapture('udpsrc port=5000 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)
if not cap_receive.isOpened():
print('VideoCapture not opened')
exit(0)
while True:
ret,frame = cap_receive.read()
if not ret:
print('empty frame')
break
cv2.imshow('receive', frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
cap_receive.release()
if __name__ == '__main__':
s = Process(target=send)
r = Process(target=receive)
s.start()
r.start()
s.join()
r.join()
cv2.destroyAllWindows()
我使用以下 gstreamer 命令创建了一个网络流:
发件人:
gst-launch-1.0 -v videotestsrc ! video/x-raw,framerate=20/1 ! videoscale ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay ! udpsink host=X.X.X.X port=5000
接收者:
gst-launch-1.0 -v udpsrc port=5000 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! autovideosink
这很好用。我现在想在 python 脚本中包含接收方的流。在脚本中我想用opencv做一些视频处理。
有谁知道如何转换所描述的管道,以便它可以与 opencv 一起使用?
谢谢!
编辑1:
发现这应该有效:
cap = cv2.VideoCapture("udpsrc port=5000 ! application/x- rtp,media=video,payload=26,clock-rate=90000,encoding-name=H264, payload=96 ! rtph264depay ! decodebin ! videoconvert ! appsink", cv2.CAP_GSTREAMER)
我收到以下错误:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/nvidia/build-opencv/opencv/modules/highgui/src/window.cpp, line 331 Traceback (most recent call last): File "launchstream_ip.py", line 13, in <module> cv2.imshow('frame', frame) cv2.error: /home/nvidia/build-opencv/opencv/modules/highgui/src/window.cpp:331: error: (-215) size.width>0 && size.height>0 in function imshow
import numpy as np
import cv2
from multiprocessing import Process
def send():
cap_send = cv2.VideoCapture('videotestsrc ! video/x-raw,framerate=20/1 ! videoscale ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
out_send = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000',cv2.CAP_GSTREAMER,0, 20, (320,240), True)
if not cap_send.isOpened() or not out_send.isOpened():
print('VideoCapture or VideoWriter not opened')
exit(0)
while True:
ret,frame = cap_send.read()
if not ret:
print('empty frame')
break
out_send.write(frame)
cv2.imshow('send', frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
cap_send.release()
out_send.release()
def receive():
cap_receive = cv2.VideoCapture('udpsrc port=5000 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)
if not cap_receive.isOpened():
print('VideoCapture not opened')
exit(0)
while True:
ret,frame = cap_receive.read()
if not ret:
print('empty frame')
break
cv2.imshow('receive', frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
cap_receive.release()
if __name__ == '__main__':
s = Process(target=send)
r = Process(target=receive)
s.start()
r.start()
s.join()
r.join()
cv2.destroyAllWindows()