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

asked 2018-06-18 16:04:39 -0600

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

berak gravatar image

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!

edit retag flag offensive close merge delete

Comments

this is not normal, and looks like an installation problem.

how did you install cv2 ?

berak gravatar imageberak ( 2018-06-19 09:10:32 -0600 )edit

I used pip to install opencv, specifically I used pip install opencv-python. I tried uninstalling and reinstalling with the same method to no avail. Is there a better method I should be using?

awillia91 gravatar imageawillia91 ( 2018-06-19 11:59:02 -0600 )edit

To follow up again I have tried uninstalling opencv, and then downloading version 3.4.1 directly from the sourceforge ( https://sourceforge.net/projects/open... ) however the issue still persists. Thanks for any help!

awillia91 gravatar imageawillia91 ( 2018-06-19 12:24:04 -0600 )edit