Error Handling for RTSP Stream

I am trying to do error handling where I want to show an image on screen when that the media player is waiting for the connection when the Ethernet cable is detached or in case the URL is not sending any data. For now I have written the below code so, how can I implement the code for error handling

        cap = cv2.VideoCapture('rtsp://127.0.0.1:5000/tested')
        self.thread_is_active = True
        while self.thread_is_active:
            ret, frame = cap.read()
            if ret:
                image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                h,w,ch=image.shape
                bytesperline=ch*w
                convertqtformat=QtGui.QImage(image.data, w, h, bytesperline, QtGui.QImage.Format_RGB888)
                p=convertqtformat.scaled(640, 480, QtCore.Qt.KeepAspectRatio)
                self.change_pixmap.emit(QtGui.QPixmap.fromImage(p))
def stop(self):
        self.thread_is_active = False
        self.quit()

One error handling I have to do for data is streaming in the URL provided, also another is that if the ethernet cable is connected or not. There might be instances that the server stops sending data for some time and then again resume the live feed.

sorry for being ‘mr. obvious’ here, but:

you should at least check, IF the capture opened (at all !) and use the ret value to see, if it broke (& bail out, then … )

Thank you for your reply and help but I have moved a step forward where I got help from the following link.

I used the following line to come to this step:

cap = cv2.VideoCapture(' rtsp://127.0.0.1:5000/tested', apiPreference=cv2.CAP_ANY, params=[cv2.CAP_PROP_OPEN_TIMEOUT_MSEC, 2000])

But this seems to be half of my solution. When I remove the ethernet cable from the server side, the above code freezes at:

ret, frame=cap.read()

So, it going to the next instruction after 1-2 mins. And the next instruction is to handle this error and just print waiting for the video

How can i reduce this time?

OpenCV is the wrong library for this.

use gstreamer directly. it’s a library.