【发布时间】:2018-05-10 03:52:48
【问题描述】:
我的网络摄像机似乎有点不稳定,并且随机断开连接。我希望我的脚本能够确定它何时断开连接并尝试重新连接几次,可能在尝试之间等待 5-10 秒。我已经尝试了一些东西,但没有任何效果。
这是我的基本脚本,当 ret 为 false 时脚本结束:
#!/usr/local/bin/python3
import cv2
import time
import datetime
print("start time: " + datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"))
cap = cv2.VideoCapture('rtsp://<ip><port>/live0.264')
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Confirm we have a valid image returned
if not ret:
print("disconnected!")
break
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
# Display the resulting frame
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
print("end time: " + time.strftime("%X"))
# When everything is done, release the capture
cap.release()
cv2.destroyAllWindows()
编辑:我还希望脚本在我的网络暂时中断或类似情况下尝试重新连接到相机。
【问题讨论】:
-
什么不起作用?有错误吗?
-
实际上我不知道解决方案,但我认为你必须修复你的网络摄像头我从未见过随机断开的网络摄像头
-
这是一款便宜的相机。我无法控制它的固件或导致它断开连接的任何东西。即使问题不是相机,我也希望脚本能够在因任何其他原因断开连接时恢复与相机的连接。
-
你试过什么?你说“几件事”?问题出在延迟还是重新连接本身?看起来您已经确定了一种检查相机是否断开连接的方法,所以我猜这不是您要问的吗?最好不要使用 While True,而是将
if cv2.waitKey合并到 While 检查中。 -
问题是重新连接。我尝试创建一个调用
cv2.VideoCapture()的函数。当cap.read()在if not ret:中没有返回任何内容时,将调用该函数
标签: python python-3.x opencv surveillance