gpt4 book ai didi

Python OpenCV : Play UDP video streaming while sending keep alive messages

转载 作者:太空宇宙 更新时间:2023-11-03 21:52:13 27 4
gpt4 key购买 nike

我有一个通过套接字连接相机的程序。当我通过此套接字发送消息时,此摄像机开始直播视频,并在我发送保持事件消息时继续。视频通过 UDP 作为 mpeg-ts 数据包传输。我正在尝试使用 OpenCV 播放视频,但我有

[udp @ 0x10d07e0] bind failed: Address already in use

执行错误。这是我的代码(ip4 和 LIVEPORT 是常量):

dataLive = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
td = threading.Timer(0, send_keepalive_msg, [dataLive, KA_DATA_MSG, (ip4,LIVEPORT)])
td.start()

videoLive = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
tl = threading.Timer(0, send_keepalive_msg, [videoLive, KA_VIDEO_MSG, (ip4, LIVEPORT)])
tl.start()

videourl = "udp://@{0}:{1}/".format(ip4, LIVEPORT)
cap = cv2.VideoCapture(videourl)

while cap.isOpened():
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

cv2.imshow("frame", gray)
if cv2.waitKey(30) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()

此代码是从之前使用 Gstreamer 的代码演变而来的,效果很好:

PIPELINE_DEF = "udpsrc do-timestamp=true name=src closefd=false !" \
"mpegtsdemux !" \
"queue !" \
"ffdec_h264 max-threads=0 !" \
"ffmpegcolorspace !" \
"xvimagesink name=video"

dataLive = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
td = threading.Timer(0, send_keepalive_msg, [dataLive, KA_DATA_MSG, (ip4,LIVEPORT)])
td.start()

videoLive = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
tl = threading.Timer(0, send_keepalive_msg, [videoLive, KA_VIDEO_MSG, (ip4, LIVEPORT)])
tl.start()

# Create gstreamer pipeline to stream video
pipeline = gst.parse_launch(PIPELINE_DEF)

# Source element: video socket. Sockfd file for UDP reception. socket.fileno() returns socket descriptor
src = pipeline.get_by_name("src")
src.set_property("sockfd", videoLive.fileno())

pipeline.set_state(gst.STATE_PLAYING)

while True:
state_change_return, state, pending_state = pipeline.get_state(0)
if gst.STATE_CHANGE_FAILURE == state_change_return:
break

send_keep_alive 函数并不重要,但代码如下:

def send_keepalive_msg(socket, msg, peer):
while True:
socket.sendto(msg, peer)
time.sleep(timeout)

如何将 opencv CaptureVideo 与套接字正确绑定(bind)?提前致谢!

最佳答案

也许你和我从事同一个项目:]。我已经查看了您在此之前发布的一页。它说你想尝试 gst 1.0 而不是 gst 0.10。我仍然使用它,几乎放弃了。现在我只能使用 gst 0.10。现在,我已经为 gst 管道编辑了一些命令行。它运作良好。希望它能帮助你,并回复我任何改进。下一行是我自己发布的答案: Grab the frame from gst pipeline to opencv with python

关于Python OpenCV : Play UDP video streaming while sending keep alive messages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37660982/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com