Campello
Campello

Reputation: 325

OpenCV Python Assertion Failed

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()

Upvotes: 2

Views: 15470

Answers (1)

cwallenpoole
cwallenpoole

Reputation: 82078

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.

Upvotes: 2

Related Questions