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.
berak
June 18, 2024, 5:29am
2
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.
opened 10:03PM - 31 Jan 23 UTC
question (invalid tracker)
### System Information
OpenCV python version: 4.6.0.66
Operating System: Win… dows 11
Python version: 3.11
### Detailed description
If a camera stream is not found or a camera is offline (unreachable IP address) opencv will throw an connection error connection of `-138` and softlock the main thread for around 30 seconds until an exception is thrown.
For some reason after the error is encountered it takes opencv a very long time before realizing this and actually throwing an exception.
At times no exception is even thrown even with `cv2.VideoCapture.setExceptionMode(True)` set
![image](https://user-images.githubusercontent.com/49711232/215896217-ae11b028-faac-4350-b7b7-63f7de8b0645.png)
### Steps to reproduce
```python
import cv2
from datetime import datetime
while True:
try:
print(f"{datetime.now().strftime('%H:%M:%S')} || Trying to connect to camera")
camera = cv2.VideoCapture("http://10.0.0.114")
camera.setExceptionMode(True)
if camera.isOpened():
print("Camera connected!")
break
print(f"{datetime.now().strftime('%H:%M:%S')} || Failed to connect to camera, no exception was thrown")
except:
print(f"{datetime.now().strftime('%H:%M:%S')} || Failed to connect to camera, exception was thrown") # wont run
```
### Issue submission checklist
- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)
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.