I cannot exit from "while loop" while showing images (python 2.7.13/OpenCV 3.2.0)

asked 2017-04-21 14:56:38 -0600

noctuelles gravatar image

updated 2017-04-24 04:21:45 -0600

Dear all, I have a MacBook Air Sierra 10.12.4, Python 2.7.13 installed with Homebrew and OpenCV 3.2.0. I would like to show two superposed images using the command cv2.addWeighted and random.uniform() in a while loop. I wrote the following code:

import random
import cv2

img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')

cv2.startWindowThread()
cv2.imshow('test',cv2.resize(img1,(1440,900)))

#1440,900 is the resolution of my laptop

while True :

    alpha = random.uniform(0,1)
    wei = cv2.addWeighted(img2,alpha,img1,1-alpha,0)
    cv2.imshow('test',cv2.resize(wei,(1440,900)))
    cv2.waitKey(100)

My problem is how to exit from the loop e.g. hitting the Esc key. I tried by including in the while loop

key=cv2.waitKey(0)
    if (key==27):
        cv2.destroyAllWindows()
        for i in range (1,5):
        cv2.waitKey(1)

(I found on the web that you need five times "cv2.waitKey(1)" to destroy effectively the window), but it does not work. The only way to interrupt the code is by clicking on the terminal window and hitting Ctrl+c.

To be more precise, I would like to show these two alternating images in fullscreen mode. This is obtained by adding the following two lines after the "cv2.startWindowThread()" command:

cv2.namedWindow('test',cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty('test',cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)

The further problem in this case is that I cannot interrupt the script, because I cannot see and select the terminal window: keyboard is frozen as I can see only the mouse pointer but without being able to click.

The only way to stop everything... is rebooting my mac, so I am doing something terribly wrong, of course.

I hope to have been clear and thank you in advance for any suggestions.

EDIT

Thanks to LBerger suggestion, I put the while loop inside a function so, the new version of the code is:

import random
import cv2

img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')

cv2.startWindowThread()
# cv2.namedWindow('test',cv2.WND_PROP_FULLSCREEN)
# cv2.setWindowProperty('test',cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN) 
cv2.imshow('test',cv2.resize(img1,(1440,900)))

#1440,900 is the resolution of my laptop

def main() :
    while True :

        alpha = random.uniform(0,1)
        wei = cv2.addWeighted(img2,alpha,img1,1-alpha,0)
        cv2.imshow('test',cv2.resize(wei,(1440,900)))
        ch = cv2.waitKey(1)
        if ch == 27:
            break
        cv2.waitKey(100)

if __name__ == '__main__':
    main()

cv2.destroyAllWindows()
cv2.waitKey(1)

But I still have a problem. If I add the two commented lines after "cv2.startWindowThread()" to display the images fullscreen, when I press the ESC key, the two images stop alternating, so I think we are out of the while loop, but the fullscreen window does not close! Everything is frozen again, the keyboard "beeps" when pressing any key... and I have to reboot the computer. Does anyone have the same problem running the above script (uncommenting the two lines for fullscreen mode? Thanks in advance.

edit retag flag offensive close merge delete

Comments

I'm a noob in python but I think this code is good

LBerger gravatar imageLBerger ( 2017-04-21 15:03:43 -0600 )edit

Dear LBerger, thank you very much for your suggestion. I edited my question, because I still have a problem when running the code to show the images in fullscreen mode.

noctuelles gravatar imagenoctuelles ( 2017-04-21 15:41:40 -0600 )edit

a single cv2.waitKey(10) call should be enough !

berak gravatar imageberak ( 2017-04-22 02:11:22 -0600 )edit

Thank you, berak, but I still have the same problem if I add the two lines to run the script for fullscreen mode. Pressing the ESC key, the image freezes, my mac is like frozen and the keyboard "beeps" for any pressed key and I can only reebot the mac.

Does anyone have the same problem running the above script (for fullscreen mode) on his/her computer? Thanks in advance.

noctuelles gravatar imagenoctuelles ( 2017-04-22 03:33:37 -0600 )edit
2

@noctuelles, this sounds more like a specific os/gui problem, can't help, sorry.

berak gravatar imageberak ( 2017-04-22 03:37:05 -0600 )edit

@noctuelles can you reproduce problem with opencv example?

LBerger gravatar imageLBerger ( 2017-04-22 03:51:23 -0600 )edit

@berak : I see, thank you for your efforts anyway!

noctuelles gravatar imagenoctuelles ( 2017-04-22 05:12:31 -0600 )edit

@LBerger : the lk_track.py script works smoothly. The problem appears when I add the two lines in order to show the images fullscreen. I do not want to show the upper bar, just to see the images, but for some reason I cannot close the window and end the script.

noctuelles gravatar imagenoctuelles ( 2017-04-22 05:14:21 -0600 )edit

There is not reason for the last cv2.waitKey(1) after cv2.destroyAllWindows() and maybe they come in some thread conflict. Did you tried to remove it?

pklab gravatar imagepklab ( 2017-04-22 10:33:51 -0600 )edit

@pklab : Thank you for your comment. I had to add cv2.waitKey(1) since it seems to be a well-known problem on Mac. Without this command cv2.destroyAllWindows() does not work (see e.g. here )

noctuelles gravatar imagenoctuelles ( 2017-04-24 03:58:54 -0600 )edit

@noctuelles ... and what if you remove cv2.destroyAllWindows() too. This should be performed anyway from class destructor ... in C++ isn't necessary to call `destroyAllWindows' at the end of application.

pklab gravatar imagepklab ( 2017-04-24 10:09:11 -0600 )edit

@pklab : It does not work... the only way to close the window (not in fullscreen mode) is using both commands: cv2.destroyAllWindows() and cv2.waitKey(1). But again, in fullscreen mode, it does not work...

noctuelles gravatar imagenoctuelles ( 2017-04-24 11:03:10 -0600 )edit