I am using OpenCV 3.3 with Python 2.7 on a Windows 10 PC. In one of my projects, I am connecting an external webcam to do some tracking. Everything works well as long as the webcam is connected. If the webcam is disconnected before the program runs/during the program, the program crashes. When I tried a dummy program to check what's going wrong, I noticed that the program crashes in the line where i call cv2.VideoCapture(0) itself.
Code Snippet:
cap=cv2.VideoCapture(0) -- this line is in the main program and if camera is not connected, the program crashes at this point
--The below lines are inside a function and is called by the main program based on some other input
r, a = cap.read()
while not r:
print 'Reconnect Camera'
r , a = cap.read()
print 'All Okay!'
I put the while loop in there to prevent problems if the camera is disconnected after the cap=cv2.VideoCapture(0) but before cap.read() inside the function. This also does not do the work as 'r' value becomes true in the second iteration even after the camera disconnects. I have put a graph below to represent the behavior of 'r' with camera's connection.
I might be doing something wrong! How do I fix this?! Thanks in advance guys!