【问题标题】:Remotely accesssing RTSP url with Python & OpenCV使用 Python 和 OpenCV 远程访问 RTSP url
【发布时间】:2022-11-01 13:31:13
【问题描述】:

我可以使用以下python代码查看局域网上的相机,这不是问题。但是,当从远程站点运行相同的代码时,它会以空帧中断。 VLC播放器可以通过远程连接查看这个网络地址就好了。这是代码:

import os
import cv2

os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"]="rtsp_transport;udp|analyzeduration;2000|"
os.environ["OPENCV_FFMPEG_DEBUG"] = "1"
os.environ["OPENCV_LOG_LEVEL"] = "VERBOSE"


x='rtsp://home_routers_ip:cameras_fwded_port/onvif1'  

cap = cv2.VideoCapture(x,cv2.CAP_FFMPEG)

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == False:
       print("Frame is empty")
       break;
    else:
       cv2.imshow('VIDEO', frame)
       if cv2.waitKey(20) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

从其工作的家庭 LAN 输出:

[OPENCV:FFMPEG:40] Starting connection attempt to Home_Router's_IP port 42???
[OPENCV:FFMPEG:40] Successfully connected to Home_Router's_IP port 42???
[OPENCV:FFMPEG:40] SDP:
v=0
o=- 1421069297525233 1 IN IP4 192.168.1.21
s=H.264 Video, RtspServer_0.0.0.2
t=0 0
a=tool:RtspServer_0.0.0.2
a=type:broadcast
a=control:*
a=range:npt=0-
m=video 0 RTP/AVP 96
c=IN IP4 0.0.0.0
b=AS:500
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;profile-level-id=42001F;sprop-parameter-sets=Z0IAH5WoFAFuQA==,aM48gA==
a=control:track1
m=audio 0 RTP/AVP 8
a=control:track2
a=rtpmap:8 PCMA/8000


[OPENCV:FFMPEG:40] setting jitter buffer size to 500
[OPENCV:FFMPEG:40] setting jitter buffer size to 500
[OPENCV:FFMPEG:40] max_analyze_duration 2000 reached at 20000 microseconds st:1
[OPENCV:FFMPEG:24] Could not find codec parameters for stream 0 (Video: h264, 1 reference frame, none): unspecified size
Consider increasing the value for the 'analyzeduration' (2000) and 'probesize' (5000000) options
[OPENCV:FFMPEG:40] Reinit context to 1280x720, pix_fmt: yuv420p


WORKING! A live camera window opens up.

最后, 从远程站点运行代码时的输出:

[OPENCV:FFMPEG:40] Starting connection attempt to Home_Router's_IP port 42???
[OPENCV:FFMPEG:40] Successfully connected to Home_Router's_IP port 42???
[OPENCV:FFMPEG:40] SDP:
v=0
o=- 1421069297525233 1 IN IP4 192.168.1.21
s=H.264 Video, RtspServer_0.0.0.2
t=0 0
a=tool:RtspServer_0.0.0.2
a=type:broadcast
a=control:*
a=range:npt=0-
m=video 0 RTP/AVP 96
c=IN IP4 0.0.0.0
b=AS:500
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;profile-level-id=42001F;sprop-parameter-sets=Z0IAH5WoFAFuQA==,aM48gA==
a=control:track1
m=audio 0 RTP/AVP 8
a=control:track2
a=rtpmap:8 PCMA/8000

[OPENCV:FFMPEG:40] setting jitter buffer size to 500
[OPENCV:FFMPEG:40] setting jitter buffer size to 500
[OPENCV:FFMPEG:24] Could not find codec parameters for stream 0 (Video: h264, 1 reference frame, none): unspecified size
Consider increasing the value for the 'analyzeduration' (2000) and 'probesize' (5000000) options
Frame is empty

PROGRAM Aborts :(

【问题讨论】:

  • 你如何从远程使用 vlc 访问它?
  • 如果你在阅读之前sleep 会发生什么?可能尚未捕获任何帧。
  • 从远程访问 Micka vlc 与本地、ctrl/command N 和粘贴 rtsp url 没有什么不同。 Kesh,测试了在阅读前添加睡眠,没有区别。
  • 问题似乎是这条线'o = - 1421069297525233 1 IN IP4 192.168.1.21' A tcpdump 输出显示'cap = cv2.VideoCapture(x,cv2.CAP_FFMPEG)'正在将udp数据报发送到192.168.1.21(而不是home_routers_ip:cameras_fwded_port ),这恰好是本地 LAN 上的 rtsp 服务器地址。如果从本地局域网访问一切都很好,但远程它没有意义。

标签: python opencv ffmpeg rtsp


【解决方案1】:

这是一种使用 opencv 从远程 LAN 访问 rtsp 摄像头流的方法。这样做需要两台基本计算机,在我的情况下是 rpi3s,每个 LAN 中一台。

http://sensoranalytics.com.au/misc/Sof.jpg

在远程 rpi 上使用以下命令在两个 rpi 之间建立串行 vpn 链接。

sudo apt update
sudo apt upgrade
sudo apt install ppp

sudo /usr/sbin/pppd updetach connect-delay 60000 noauth pty
 "sudo -u myusername /usr/bin/ssh -t -t home_router_ip -l myusername -p myrouter_port_forwarded to ipcam's 554
  sudo /usr/sbin/pppd noauth 192.168.186.2:192.168.186.1" 

sudo ip r add 192.168.1.0/24 dev ppp0
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

然后在本地rpi上执行以下命令。

sudo ip route add 192.168.0.0/24 dev ppp0
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

最后,必须将我原始帖子的 python 脚本中的以下行更改为 cam 的本地地址,例如就我而言:

x='rtsp://192.168.1.21/onvif1'

【讨论】:

    猜你喜欢
    • 2017-09-30
    • 2017-06-04
    • 2014-04-19
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多