-1

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.

5
  • You might want to post an example stacktrace. That usually helps troubleshooting issues like this. Commented Dec 19, 2018 at 18:22
  • There's nothing to catch here, those are warnings... Commented Dec 19, 2018 at 21:18
  • @DanMašek Thanks for the answer. Is there any way i can get hold of the warning message to write it in the logs file? Commented Dec 19, 2018 at 23:21
  • You may need to insert the try, catch block where you are creating the frame using cap.read(). Please paste the whole source code. Commented Dec 20, 2018 at 7:04
  • @Jrawat Possibly. Have a look at this answer Commented Dec 20, 2018 at 12:10

1 Answer 1

2

cap.setExceptionMode() worked for me (using opencv-python-headless 4.6.0.66) and didn't require modifying package code:

cap = VideoCapture()
cap.setExceptionMode(True)

# cap.open(source)

try:
    ret, frame = cap.read()
except Exception as e:
    print(type(e), e.code)  # output: <class 'cv2.error'> -2

Error codes are documented here: https://docs.opencv.org/4.6.0/d1/d0d/namespacecv_1_1Error.html#a759fa1af92f7aa7377c76ffb142abcca

Sign up to request clarification or add additional context in comments.

1 Comment

neat. this was introduced in v4.1.1. I hadn't noticed that... this only catches failing isOpened() if used like so: cap = VideoCapture(); cap.setExceptionMode(True); cap.open(...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.