OpenCV Python Assertion Failed
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.
Related videos on Youtube
Comments
-
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 over 6 yearsAfter reading frame, add
if ret == False: break
. -
Campello over 6 yearsThank 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 over 6 yearscomment out the
cap.release()
function and usewhile True: ...
. What's the output? -
Campello over 6 yearsIt gives me the same error. Apparently there is a related issue on GitHub for mac: github.com/dthpham/butterflow/issues/12
-
Campello over 6 yearsOk, if I uncomment cv2.destroyAllWindows() it seems to stop crashing. Any ideas why?
-
Campello over 6 yearsUpdate, it stops crashing SOMETIMES....
-
zindarod over 6 yearsIn terminal
python -c "import cv2; print(cv2.getBuildInformation())" >> result.txt
. Post the file to pastebin.com, upload link here. -
Campello over 6 yearsLet us continue this discussion in chat.
-
-
Campello over 6 yearsThank 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 over 6 yearsOk, if I uncomment cv2.destroyAllWindows() it seems to stop crashing. Any ideas why?
-
cwallenpoole over 6 yearsWhat about using
cv2.namedWindow('frame')
at the beginning of the script?