OpenCV Python Assertion Failed

10,539

It's not clear from your question, but it looks like you're specifically running into a situation where the video completes playing without being interrupted. I think the issue is that the VideoCapture object is already closed by the time you get to cap.release(). I'd recommend putting the call to release inside of the if statement by the break.

I've not had time to experiment, but I normally follow this pattern:

reader = cv2.VideoCapture(<stuff>)
while True:
  success, frame = reader.read()
  if not success:
     break

I'd not had to call release explicitly in those contexts.

Share:
10,539

Related videos on Youtube

Campello
Author by

Campello

Postdoc, Imperial College London

Updated on May 29, 2022

Comments

  • Campello
    Campello about 2 years

    I am trying to run opencv-python==3.3.0.10 on a macOS 10.12.6 to read from a file and show the video in a window. I have exactly copied the code from here http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html, section 'Playing' video from file.

    The code runs correctly and shows the video, however after termination of the video it breaks the program, giving the following error:

    Assertion failed: (ec == 0), function unlock, file /BuildRoot/Library/Caches/com.apple.xbs/Sources/libcxx/libcxx-307.5/src/mutex.cpp, line 48.

    Does anyone have any idea of what might cause this?


    Code snippet for your convenience (edited to include some suggestions in the comment)

    cap = cv2.VideoCapture('vtest.avi')
    
    while(True):
        ret, frame = cap.read()
    
        if not ret:
            break
    
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
        cv2.imshow('frame',gray)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
     cv2.destroyAllWindows()
    
    • zindarod
      zindarod over 6 years
      After reading frame, add if ret == False: break.
    • Campello
      Campello over 6 years
      Thank you for the comment. I did add that, but still does not fix the problem. I tried to put a flag after the loop while, and the program does not arrive there. It seems that it does not go out of the loop..
    • zindarod
      zindarod over 6 years
      comment out the cap.release() function and use while True: .... What's the output?
    • Campello
      Campello over 6 years
      It gives me the same error. Apparently there is a related issue on GitHub for mac: github.com/dthpham/butterflow/issues/12
    • Campello
      Campello over 6 years
      Ok, if I uncomment cv2.destroyAllWindows() it seems to stop crashing. Any ideas why?
    • Campello
      Campello over 6 years
      Update, it stops crashing SOMETIMES....
    • zindarod
      zindarod over 6 years
      In terminal python -c "import cv2; print(cv2.getBuildInformation())" >> result.txt. Post the file to pastebin.com, upload link here.
    • Campello
      Campello over 6 years
  • Campello
    Campello over 6 years
    Thank you for your answer. It did not solve the problem. It does break the loop but if crashes right after (anything after the loop is not executed)
  • Campello
    Campello over 6 years
    Ok, if I uncomment cv2.destroyAllWindows() it seems to stop crashing. Any ideas why?
  • cwallenpoole
    cwallenpoole over 6 years
    What about using cv2.namedWindow('frame') at the beginning of the script?