OpenCV-Python VideoCapture.read() does not return False when rtsp connection is interrupted

核能气质少年 提交于 2019-12-11 05:18:40

问题


I have the following class for streaming video through rtsp using OpenCV 3.1 with Python 3.4.3. Everything works fine, but if the camera is suddenly disconnected while running (ie. unplug camera), the program would hang at self.capture.read() and never returns the False value (or any value for that matter) to handle closing the connection. That's my understanding of handling sudden disconnections for VideoCapture in OpenCV. Is there a better way?

    """Classes for video processing"""
import cv2
from PyQt5 import QtGui, QtCore

from settings import CAMERA_IP_ADDRESS, CAMERA_PORT, FRAME_RATE
from structures import MessageLevel


class VideoStream(QtCore.QObject):
    """Class for displaying and recording video data."""
    logEvent = QtCore.pyqtSignal(MessageLevel, str)
    video_frame = QtCore.pyqtSignal(QtGui.QPixmap)
    video_timer = QtCore.QTimer()

    def __init__(self, parent=None):
        super(VideoStream, self).__init__(parent)
        self.connected = False
        self.stream = False
        self.video_timer.timeout.connect(self.stream_loop)
        self.video_width = 1280
        self.video_height = 720
        self.frame_rate_milliseconds = int(round(1/FRAME_RATE * 1000))
        self.save_path = None
        self.address_ip = None
        self.capture = None
        self.video_file = None

        self.url = 'rtsp://ip_path_to_camera' 

    def setup(self):
        """Function to set defautls and update GUI, should only be called once during GUI setup"""
        # Initialize variables
        self.set_address_ip(CAMERA_IP_ADDRESS)

    def connect(self):
        """Connect to rtsp video stream"""
        if not self.connected:
            self.video_file = cv2.VideoWriter(self.save_path, cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'),
                                              FRAME_RATE, (self.video_width, self.video_height))
            self.capture = cv2.VideoCapture(self.url)
        if self.capture.isOpened():
            self.connected = True
            self.stream = True
            self.video_timer.start(self.frame_rate_milliseconds)
        else:
            print('Device failed to connect')

    def stop(self):
        self.pause()
        self.connected = False
        self.capture.release()
        self.video_file = None

    def pause(self):
        self.video_timer.stop()
        self.stream = False

    def stream_loop(self):
        if self.stream:
            ret, frame = self.capture.read()
            if ret:
                self.video_file.write(frame)
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                image = QtGui.QImage(frame, self.video_width, self.video_height, QtGui.QImage.Format_RGB888)
                self.video_frame.emit(QtGui.QPixmap.fromImage(image))
            else:
                print('Device diconnected')
                self.stop()

    def set_address_ip(self, ip_address):
        self.address_ip = ip_address
        self.address_ip_signal.emit(self.address_ip)

    def log_message(self, level, message):
        self.logEvent.emit(level, message)

来源:https://stackoverflow.com/questions/36268054/opencv-python-videocapture-read-does-not-return-false-when-rtsp-connection-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!