2

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()
asked Nov 9, 2017 at 16:27
8
  • After reading frame, add if ret == False: break. Commented Nov 9, 2017 at 16:39
  • 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.. Commented Nov 9, 2017 at 16:43
  • comment out the cap.release() function and use while True: .... What's the output? Commented Nov 9, 2017 at 16:49
  • It gives me the same error. Apparently there is a related issue on GitHub for mac: github.com/dthpham/butterflow/issues/12 Commented Nov 9, 2017 at 16:56
  • Ok, if I uncomment cv2.destroyAllWindows() it seems to stop crashing. Any ideas why? Commented Nov 9, 2017 at 17:04

1 Answer 1

2

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.

answered Nov 9, 2017 at 16:49

3 Comments

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)
Ok, if I uncomment cv2.destroyAllWindows() it seems to stop crashing. Any ideas why?
What about using cv2.namedWindow('frame') at the beginning of the script?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.