繁体   English   中英

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.尽管我编写了代码cv2.imshow来查看视频提要,但是我看不到任何视频提要。

只是一个空白窗口正在加载

  1. 按下键盘上的“ q”按钮后,视频Feed不会停止(在我的情况下更具体:空白窗口不会关闭),尽管为此编写了代码

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

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

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

如果您可以提出解决任何问题的方法,将很有帮助。

提前致谢!

按下键盘上的“ q”按钮后,视频Feed不会停止(在我的情况下更具体:空白窗口不会关闭),尽管为此编写了代码

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)之前返回状态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。

  1. 尽管我编写了代码cv2.imshow来查看视频提要,但是我看不到任何视频提要。
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:该行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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM