Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python OpenCV:重新连接断开连接的摄像头馈送的代码工作正常,但前端捕获的视频帧未加载_Python_Opencv_Video Streaming_Ip Camera - Fatal编程技术网

Python OpenCV:重新连接断开连接的摄像头馈送的代码工作正常,但前端捕获的视频帧未加载

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

我编写了这个简单的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!")
            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来查看视频提要,但我无法查看任何视频提要

只加载了一个空白窗口

  • 在键盘上按“q”键时,视频馈送不会停止(更具体地说,在我的例子中:空白窗口不会关闭),尽管有为此编写的代码
  • 注意:我使用的是Ubuntu(CPU),但我也试着从Windows系统运行代码,但也只有一个空白窗口被加载,没有显示任何捕获的视频帧。 更多详情:

    在windows系统中,我可以看到一个错误通知:“python停止工作”

    我的疑问是:如果python已经停止工作,那么代码的其余部分是如何执行的,我看到msg像预期的那样:“disconnected”…“Connected”等等

    如果你能为解决这个问题提供一些帮助,那将是很有帮助的

    提前谢谢

    视频馈送没有停止(更具体地说,在我的例子中:那是 空白窗口未关闭)从键盘上按“q”时, 尽管有专门为此编写的代码

    在函数中,
    处理捕获的视频()
    ,您将在
    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

  • 虽然我编写了代码cv2.imshow来查看视频提要,但我无法查看任何视频提要

  • 在做了这些修改之后,您的代码就可以工作了

    您好,您还记得当摄像头断开时,抓取功能需要多少时间才能以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