Python OpenCV:重新连接断开连接的摄像头馈送的代码工作正常,但前端捕获的视频帧未加载
我编写了这个简单的python代码来重新连接我的系统所连接的IP摄像头,以防摄像头断开连接Python OpenCV:重新连接断开连接的摄像头馈送的代码工作正常,但前端捕获的视频帧未加载,python,opencv,video-streaming,ip-camera,Python,Opencv,Video Streaming,Ip Camera,我编写了这个简单的python代码来重新连接我的系统所连接的IP摄像头,以防摄像头断开连接 import numpy as np import cv2 import time def work_with_captured_video(): while True: ret, frame = camera.read() if not ret: print("Camera is disconnected!") c
import numpy as np
import cv2
import time
def work_with_captured_video():
while True:
ret, frame = camera.read()
if not ret:
print("Camera is disconnected!")
camera.release()
return False
break
else:
cv2.imshow('frame', frame)
return True
if cv2.waitKey(1) & 0xFF == ord('q'):
break
while True:
camera = cv2.VideoCapture('rtsp://<ip specific to my camera>')
if camera.isOpened():
print('Camera is connected')
#call function
response = work_with_captured_video()
if response == False:
time.sleep(10)
continue
else:
print('Camera not connected')
camera.release()
time.sleep(10)
continue
将numpy导入为np
进口cv2
导入时间
使用捕获的视频()进行def工作:
尽管如此:
ret,frame=camera.read()
如果不是ret:
打印(“相机断开连接!”)
摄影机。释放()
返回错误
打破
其他:
cv2.imshow(“帧”,帧)
返回真值
如果cv2.waitKey(1)&0xFF==ord('q'):
打破
尽管如此:
摄像机=cv2.VideoCapture('rtsp://')
如果摄影机.isOpened():
打印('相机已连接')
#调用函数
响应=与捕获的视频一起工作()
如果响应==False:
时间。睡眠(10)
持续
其他:
打印('相机未连接')
摄影机。释放()
时间。睡眠(10)
持续
我可以说代码工作正常,相机在断开一段时间后重新连接。
因为在日志中,我可以看到预期的打印语句(我将其放入代码中以检查连接状态)
请参见随附的图片以了解相同的信息:
面临的问题:
1.虽然我编写了代码cv2.imshow来查看视频提要,但我无法查看任何视频提要
只加载了一个空白窗口
处理捕获的视频()
,您将在cv2.waitKey(1)
之前返回状态True。
基本上应该是这样的,
def work_with_captured_video():
while True:
ret, frame = camera.read()
if not ret:
print("Camera is disconnected!")
camera.release()
return False
#break --> Not required.
else:
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
return True
完成while循环后,将返回状态True
在做了这些修改之后,您的代码就可以工作了 您好,您还记得当摄像头断开时,抓取功能需要多少时间才能以false响应吗?这需要很长时间还是很快?谢谢
def work_with_captured_video():
while True:
ret, frame = camera.read()
if not ret:
print("Camera is disconnected!")
camera.release()
return False
#break --> Not required.
else:
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
return True
while True:
camera = cv2.VideoCapture('rtsp://<ip specific to my camera>')
if camera.isOpened():
print('Camera is connected')
#call function
response = work_with_captured_video()
if response == False:
time.sleep(10)
continue
else:
print('Camera not connected')
camera.release()
time.sleep(10)
continue
def work_with_captured_video(camera):
while True:
ret, frame = camera.read()
if not ret:
print("Camera is disconnected!")
camera.release()
return False
else:
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
return True
while True:
camera = cv2.VideoCapture('rtsp://<ip specific to my camera>')
if camera.isOpened():
print('Camera is connected')
#call function
response = work_with_captured_video(camera)
if response == False:
time.sleep(10)
continue
else:
print('Camera not connected')
camera.release()
time.sleep(10)
continue