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

OpenCV: Code to Reconnect a Disconnected Camera Feed is Working Fine but in the Front End Captured Video Frame is Not getting Loaded

我写了这个简单的 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

我可以说代码工作正常,相机在断开一段时间后正在重新连接。 因为在日志中,我可以按预期看到打印语句(我将其放入我的代码中以检查连接状态)

请看附件图片:

面临的问题:

1.Although 我编写了代码 cv2.imshow 来查看视频源,但我无法看到任何视频源。

正在加载空白 window

  1. 从键盘按下 'q' 后,视频源没有停止(更具体地说,在我的情况下:空白 window 没有关闭),尽管为此编写了代码

注意:我正在使用 Ubuntu (CPU) ,但我也尝试了 运行 来自 Windows 系统的代码,但也只有一个空白 window 正在加载,但未显示捕获的任何视频帧。 更多内容:

在 windows 系统中,我可以看到一条错误通知:'python stopped working'

我的疑问是:如果 python 会停止工作,那么代码的其余部分将如何执行,我看到的消息如下:'disconnected'..'Connected'等如预期?

如果您能提出任何建议来解决问题,那将会很有帮助。

提前致谢!

Video feed is not getting stopped(more specifically in my case: that blank window is not going off) upon pressing 'q' from keyboard, although there is code written for this

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 # Here You are returning the status.
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

在函数中,work_with_captured_video(),您在 cv2.waitKey(1) 之前 return 状态为 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 循环后,您将 return 状态为 True。

  1. Although I wrote code cv2.imshow to see the video feed, I am not able to see any video feed.
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

请为函数 work_with_captured_video() 提供参数 camera。该函数未由任何参数提供,因此行 if not ret: 具有 ret 变量作为 False

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

进行这些修改后,您的代码就可以工作了。