Ask Your Question

Revision history [back]

NamedWindow followed by imshow and waitkey opens several windows which prevent the program from closing normally

Hello! I am using Python 2.7.15 and OpenCV version 3.4.1 on Windows 10. When I run this program I want a preview window to show up with an image of the webcam stream so I can fix my face in front of the webcam, then I want this window to close when the 'q' button is pushed and the main part of the program to run. This does work right now, but for some reason when the main part of the program is ended I see three open windows, all named 'preview' which say 'not responding' and are not destroyed by a call to cv2.destroyAllWindows() at the end of the program. I can not figure out how more than one preview window is being opened, since namedWindow is called outside of any loop and inside of the loop once 'q' is pushed destroyWindow and cap.release() are called and the loop is broken out of. if preview: cap = cv2.VideoCapture(0) cv2.namedWindow('preview', cv2.WINDOW_NORMAL) cv2.resizeWindow('preview', 450, 600) while preview: ret, im = cap.read() if ret: cv2.imshow('preview', im) key = cv2.waitKey(1) & 0xFF if (key == ord('q')): preview = False cap.release() cv2.destroyWindow('preview') break cv2.destroyAllWindows() preview is a boolean switch set to true at the top of the program, so that this preview window can be skipped. image description After the main program finishes, 3 preview windows are left like this, which must be manually closed before the program will exit. Can anyone help me figure out what is going wrong here? I can provide a larger amount of the code if it helps, but this is really the only relevant part. Thanks!

click to hide/show revision 2
None

updated 2018-06-19 01:48:23 -0600

berak gravatar image

NamedWindow followed by imshow and waitkey opens several windows which prevent the program from closing normally

Hello! I am using Python 2.7.15 and OpenCV version 3.4.1 on Windows 10. When I run this program I want a preview window to show up with an image of the webcam stream so I can fix my face in front of the webcam, then I want this window to close when the 'q' button is pushed and the main part of the program to run. This does work right now, but for some reason when the main part of the program is ended I see three open windows, all named 'preview' which say 'not responding' and are not destroyed by a call to cv2.destroyAllWindows() at the end of the program. I can not figure out how more than one preview window is being opened, since namedWindow is called outside of any loop and inside of the loop once 'q' is pushed destroyWindow and cap.release() are called and the loop is broken out of.

if preview: 
    cap = cv2.VideoCapture(0)
    cv2.namedWindow('preview', cv2.WINDOW_NORMAL)
    cv2.resizeWindow('preview', 450, 600)
    while preview:
        ret, im = cap.read()
        if ret:
            cv2.imshow('preview', im)
            key = cv2.waitKey(1) & 0xFF
            if (key == ord('q')):
                preview = False
                cap.release()
                cv2.destroyWindow('preview')
                break
cv2.destroyAllWindows()cv2.destroyAllWindows()`
 

preview is a boolean switch set to true at the top of the program, so that this preview window can be skipped. image description After the main program finishes, 3 preview windows are left like this, which must be manually closed before the program will exit. Can anyone help me figure out what is going wrong here? I can provide a larger amount of the code if it helps, but this is really the only relevant part. Thanks!