-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.

Community
  • 1
  • 1
Jrawat
  • 107
  • 2
  • 8
  • You might want to post an example stacktrace. That usually helps troubleshooting issues like this. – Eric Ed Lohmar Dec 19 '18 at 18:22
  • There's nothing to catch here, those are [warnings](https://github.com/opencv/opencv/blob/7fb70e170154d064ef12d8fec61c0ae70812ce3d/modules/videoio/src/cap_msmf.cpp#L1112)... – Dan Mašek Dec 19 '18 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? – Jrawat Dec 19 '18 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. – ZdaR Dec 20 '18 at 07:04
  • @Jrawat Possibly. Have a look at [this answer](https://stackoverflow.com/a/49604578/3962537) – Dan Mašek Dec 20 '18 at 12:10

1 Answers1

1

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

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • 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(...)` – Christoph Rackwitz Jul 14 '22 at 00:03