繁体   English   中英

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

[英]streaming gstreamer video to a different address using opencv with python

在我的最后一个问题中,我正在努力打开一个 gstreamer 管道到 stream 一个网络摄像头视频源,但在帮助下我设法在同一台计算机上构建我的视频发送器和视频接收器。 尝试在另一台计算机上托管该视频 stream 时出现了一个新问题。 两台计算机都连接到 ethe.net 连接,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

在 gst_str_rtp 上,我添加了主机,它是我要将 stream 发送到的计算机的 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)将始终是发件人)并将主机和地址更改为本地主机,则 stream 可以正常工作,但是当尝试 stream 结束时。具有不同 IP 地址的网络 我在服务器端的视频捕获中一直没有得到。

我所有的防火墙都关闭了 ** 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

如果这不起作用,您可以使用命令发布 output。

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

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

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

    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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM