I check other question on google or stackoverflow, they are talking about run cv2.imshow in script, but my code run in jupyter notebook.
Here is my configuration:
ubuntu 16.4x64
python 3.5
opencv 3.1.0
I start a jupyter notebook: here is the code I put it notebook:
%pylab notebook
import cv2
cvim2disp = cv2.imread('data/home.jpg')
cv2.imshow('HelloWorld', cvim2disp)
cv2.waitKey() #image will not show until this is called
cv2.destroyWindow('HelloWorld') #make sure window closes cleanly
When I execute these code. image will show in a pop up window, but I can not close this window by clicking the x on the top right corner, and a moment later, system will prompt me that the window is not responding, it will give me 2 choices: "wait" , "fore quit". if I hit wait, then It will show the same prompt later, If I hit 'fore quit', then the jupyter notebook kernel die and I have to start over.
I google around, many solution suggest that I should add this code
cv2.startWindowThread()
before imshow
, but situation get worse, the kernel hang forever!.
anybody have some idea what's going on.
The API documentation for cv2.waitKey() notes the following:
So perhaps calling the function in an endless loop would make the window responsive? I haven't tested this, but maybe you would like to try the following:
The new window that opens up from Jupyter uses the same kernel as notebook. Just add this below to the code and it would work fine.
The following code works fine in Jupyter to show one image
If you want to show the video instead of an image in a separate window, use the following code:
Make sure the window name match, otherwise it will not work. In this case I use 'image' as window name.
This will help you understand what is happening:
waitKey(0)
method is waiting for an input infinitely. When you see a frame of the corresponding image, do not try to close the image using close in top right corner.Instead press some key.
waitkey
method will take that as an input and it will return back a value. Further you can also check which key was pressed to close the frame.Additionally
waitKey(33)
will keep the frame active for 33 ms and then close it automatically.destroyWindow()
will destroy the current frame if there.destroyAllWindows()
will destroy all the frames currently present.This will solve.
I am not sure if you can open a window from Jupyter Notebook. cv2.imshow expects a waitKey which doesn't work in Jupyter.
Here is what I have done (using OpenCV 3.3):
If you don't need to use cv2, just: