Monday, January 2, 2017

macos opencv jupyter destroyAllWindows hangs

When running python notebooks in jupyter I can't get the windows to close w/a key press.

The code below should use 'q'

Add cv2.waitKey(1) after the cv2.destroyAllWindows()

Debugging steps.

Add cv2.waitKey(10000) to allow timeout to occur. Reduce the timeout to smaller values and you should see the image disappear after short delays. If this doesn't work then something is wrong. To get this to work in my particular OS Sierra/MAC I had to add cv2.waitKey(1) after cv2.destroyAllWindows().

The standard line if cv2.waitKey(1) & 0XFF ==ord('q') does not work. This is in all the webposts on the web. Other webposts have cv2.waitKey(1) before the cap.release() statement. This statement does nothing for my platform. Probably works on theirs.

Break up the code into 2 sections add a long timeout via the first cv2.waitKey() or change the while loop to while( time.time()-startTime < 10000): to include a default timeout.


while True:
     //code here
     cv2.imshow(img)
     cv2.waitKey(10000)
     key = cv2.waitKey(1) * 0XFF
     if key == ord('q'):
        break;

#this code below not in loop
cap.release()
cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.waitKey(1)
cv2.waitKey(1)


No comments:

Post a Comment