使用opencv和python将gstreamer视频流式传输到不同的地址



在我的最后一个问题中,我很难打开gstreamer管道来流式传输网络摄像头视频源,但在帮助下,我成功地在同一台计算机上构建了视频发送器和视频接收器。当试图在另一台计算机上托管该视频流时,出现了一个新问题。两台计算机都通过以太网连接,IP相同(当然最后一个数字不同(。我的发件人代码:

发件人.py

camset='v4l2src device=/dev/video0 ! video/x-raw,width=640,height=360,framerate=52/1 ! 
nvvidconv flip-method=0 ! video/x-raw(memory:NVMM), format=I420, width=640, height=360 ! 
nvvidconv ! video/x-raw, format=BGRx ! videoconvert ! video/x-raw, format=BGR ! queue ! appsink drop=1'

gst_str_rtp = "appsrc ! video/x-raw,format=BGR ! queue ! videoconvert ! video/x-raw,format=BGRx ! nvvidconv !
video/x-raw(memory:NVMM),format=NV12,width=640,height=360,framerate=52/1 ! nvv4l2h264enc insert-sps-pps=1 
insert-vui=1 idrinterval=30 ! h264parse ! rtph264pay ! udpsink host=169.254.84.12 port=5004 auto-multicast=0"

out = cv2.VideoWriter(gst_str_rtp, cv2.CAP_GSTREAMER, 0, float(52), (frame_width, frame_height), True)
cap = cv2.VideoCapture(camset,cv2.CAP_GSTREAMER)
if not cap.isOpened():
print("Cannot capture from camera. Exiting.")
quit()
# Check writer
if not out:
print("Cannot write. Exiting.")
quit()
# Go
while True:
ret, frame = cap.read()
if ret == False:
break
out.write(frame)
cv2.imshow("sender", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

在gstrongtr_rtp上,我添加了主机,它是我想将流发送到的计算机的IP。

和我的接收器:

服务器.py

def main():
global video_frame
camSet='udpsrc address=169.254.84.12 port=5004 auto-multicast=0 ! application/x-rtp,media=video,encoding-name=H264 ! 
rtpjitterbuffer latency=0 
! rtph264depay ! decodebin ! nvvidconv ! video/x-raw,format=BGRx ! 
videoconvert ! video/x-raw,format=BGR ! appsink drop=1'
print("before capture")
try:
try:
cap = cv2.VideoCapture(camSet)
except:
print("capture fail")
while (cap.isOpened()):
print("in loop")
ret, frame = cap.read()
try:
cv2.imshow('stream',frame) 
except:
print("fail")
#outvid.write(frame)
if cv2.waitKey(1)==ord('q'):
break
cap.release()
except:
print("bad capture")

cv2.destroyAllWindows()
os._exit(0)
exit()
if __name__ == '__main__':
main()

我试过有地址和没有地址,都不起作用。如果我在同一台计算机上同时启动server.py和sender.py(ubuntu 18.04(jetson NX(将始终是发送方(,并将主机和地址更改为本地主机,则流可以很好地工作,但当尝试使用不同的IP地址通过网络进行流传输时,我在服务器端的视频捕获中总是得到None。

我所有的防火墙都关闭了**WireShark显示发送方工作于

source           destination   protocol length        info
169.254.84.2    169.254.84.12   UDP      1442         57170 → 5004 Len=1400
169.254.84.2    169.254.84.12   UDP      1442         57170 → 5004 Len=1400
169.254.84.2    169.254.84.12   UDP      1442         57170 → 5004 Len=1400

不太确定我还能尝试什么——也许是因为某种原因,配置不起作用。如有任何帮助,将不胜感激

可能不是您案例的解决方案,但以下内容可能有助于找到问题:

  1. 您将首先从终端检查:
gst-launch-1.0 -v udpsrc address=169.254.84.12 port=5004 auto-multicast=0 ! application/x-rtp,media=video,encoding-name=H264 ! rtpjitterbuffer latency=0 ! rtph264depay ! decodebin ! autovideosink
# For Windows add .exe
gst-launch-1.0.exe -v udpsrc address=169.254.84.12 port=5004 auto-multicast=0 ! application/x-rtp,media=video,encoding-name=H264 ! rtpjitterbuffer latency=0 ! rtph264depay ! decodebin ! autovideosink

如果这不起作用,您将使用命令发布输出。

如果它有效,您已经安装了gstreamer,并且能够通过udp接收和解码rtph264流,所以下一步将从opencv尝试它。

  1. 检查您的opencv-lib构建是否支持gstreamer。在python中,您可以尝试:
import cv2
print(cv2.getBuildInformation())

在输出中,如果您看到类似以下内容:

GStreamer:                   NO

那么您的opencv库没有gstreamer支持(如果可用,您可以使用另一个后端(如FFMPEG(读取它(。

  1. 仅在NVIDIA硬件上运行时使用nvvidconv。对于普通计算机,您可以只使用:
udpsrc address=169.254.84.12 port=5004 auto-multicast=0 ! application/x-rtp,media=video,encoding-name=H264 ! rtpjitterbuffer latency=0 ! rtph264depay ! decodebin ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1
  1. 创建捕获时指定后端:
cap = cv2.VideoCapture(camSet, cv2.CAP_GSTREAMER)

最新更新