The question may seem duplicate but this answer didn't work for me. I'm making a loop in which i collect images from a webcam and it works fine. My goal is to handle errors that may occur while the script is running. The best case scenario is that i can catch the error when it happens, log it in my log file and continue looping so when the problem is fixed it automatically resume collecting images.
This is my code :
while ret and not is_stop:
try :
timestamp = int(time.time()*1000)
if (timestamp - last_pic) > images_interval :
last_pic = timestamp
resize = cv2.resize(frame, (height, width))
th_save = threading.Thread(target=save_image, args=(resize, folder+"/"+str(timestamp)+".jpg"))
th_save.start()
ret, frame = cap.read()
except cv2.error as e:
s = str(e)
write_log(s)
except Exception as e:
s = str(e)
write_log(s)
To test an error i unplug the webcam and i get this error message :
[ WARN:1] videoio(MSMF): OnReadSample() is called with error status: -1072873851
[ WARN:1] videoio(MSMF): async ReadSample() call is failed with error status: -1072873851
[ WARN:2] videoio(MSMF): can't grab frame. Error: -1072873851
[ WARN:2] terminating async callback
And the whole thread is interrupted. No exception is caught because nothing is written in the logs file. I want to be able to catch the exception / error so that the thread is not stopped in order to be able to re run from teh except clause.
Any help would be appreciated.