【问题标题】:How does OpenCV handle TCP Connections?OpenCV 如何处理 TCP 连接?
【发布时间】:2020-06-01 22:17:59
【问题描述】:

我从我的 RPi 设置了一个 NetCat 视频流,并通过以下方式使用 OpenCV 访问它:

videoStream = cv2.VideoCapture("tcp://@<my_ip>:<my_port>/")
...
videoStream.release()

不幸的是,如果不重新初始化,我无法多次连接到 Stream。 OpenCV 如何处理我的 tcp 连接? .release() 是否正确关闭套接字或关闭它的正确方法是什么?

【问题讨论】:

标签: sockets opencv tcp stream netcat


【解决方案1】:

我会发表评论,但我没有足够的积分。我有一个类似的问题。最终,对我有用的是带有 -k 选项的运行 netcat,它确实允许重新连接:

关于 RPI:

/opt/vc/bin/raspivid -n -t 0 -w 640 -h 360 -fps 30 -ih -fl -l -o - | /bin/nc -klvp 5000

对于 nc,-k 选项会在第一个客户端断开连接后保持端口侦听,从而允许您重新连接。您不需要 -v 选项,它只是增加了一些冗长。

另一种选择是

在接收器上(Ubuntu、Win10):

nc x.x.x.x 5000 | mplayer -fps 200 -demuxer h264es -

gst-launch-1.0 -v tcpclientsrc host=10.60.66.237 port=5000 ! decodebin !  autovideosink

带有opencv的Python代码:

import cv2

cap = cv2.VideoCapture("tcp://10.60.66.237:5000")   
while(True): 
    ret, frame = cap.read() 
    cv2.imshow('frame', frame) 
      
    # the 'q' button is set as the 
    # quitting button you may use any 
    # desired button of your choice 
    if cv2.waitKey(1) & 0xFF == ord('q'): 
        break
cap.release() 
cv2.destroyAllWindows() 

断开并重新连接所有你想要的:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 2016-06-19
    • 2023-03-26
    • 1970-01-01
    • 2021-03-25
    • 2011-06-25
    相关资源
    最近更新 更多