-
-
Notifications
You must be signed in to change notification settings - Fork 55.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cv2.imshow() freezes #7343
Comments
Can not reproduce with latest master version in Ubuntu 16.04 with GTK 3.18.9 highgui backend and ffmpeg video capture backend. |
maybe |
Actually in the documentation its already specified that to use cv2.waitKey() after each call or cv2.imshow... It works for me..... |
your code freezes when camera gets on. There is some mistake in line 11 where you made the break condition. This is not the right way fr breaking video read loop in opencv it will not work . The following you can use to do your task: import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while cap.isOpened():
flags, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('img', gray)
key = cv2.waitKey(0) & 0xFF
if key == ord("q"):
break
cap.release()
cv2.destroyAllWindows() Hope it helps. Feel free to report if you encounter any mistake |
@arpit1997 + others - I'm getting a similar issue when trying to run the example code from the OpenCV documentation for capturing video from camera, in an interactive python session (ipython/jupyter notebook). The window displaying video pops up normally, but when I press 'q' to exit it freezes. There is no problem when running a script from terminal so I'm thinking it's an issue with interactivity. I'm running: Example code from OpenCV
I tried inserting this line too but no difference: Would really appreciate any advice/help! |
I'm having the same issue. I'm following along with "Python plays GTA V" video series. I have the call to waitkey() after imshow(). I'm copying his code exactly. I get a window with no content, just the titlebar. The only difference is that he is developing his code on Windows and it works fine.
macOS 10.12.5 (Sierra)
I tried toying around with a few things (like having no bbox, calling imshow and waitKey() just once, etc.). Thanks in advance for any help or comments. I appreciate the opencv work |
I'm having the same problem with the code posted by propellerhat. I'm using MacOS 10.12.5, Anaconda 4.4.0, and installed OpenCV 3 based on instructions from http://www.pyimagesearch.com/2016/11/28/macos-install-opencv-3-and-python-2-7/ . Everything works fine except I get title-bar-only windows. |
Any progress on this ? |
cv2.waitKey(0) will display the window infinitely until any keypress. Maybe that is why it is freezing. |
Sample from issue's description is not applicable for video streams - it works well for static images, but as mentioned @sturkmen72 @sarveshkhandu Sample by @propellerhat with "window with no content" problem probably related to empty |
As increase in waitKey() parameter with natural numbers , it freezes(Waits) for number of milliseconds to display next frame. Thank you @sarveshkhandu , @alalek |
Which IDE are you using? I was facing the same problem when I was using the default IDE (IDLE) but then I installed PyCharm, it works perfectly now, the image window closes instantly, also use waitkey(0). |
I have the same issue in macOSX HighSierra. A friend with Ubuntu has the same problem. |
@alalek why 'question invalid' label? seems more like a 'bug' |
Nope, it is just incorrect usage of OpenCV. Usage questions should go to Users OpenCV Q/A forum: http://answers.opencv.org |
When in MacOSX High Sierra, when running iPython notebook and opening image in |
@benedictchen try this way: cv2.imshow('image', im) hit key to exit then. Closing the window will keep it still running and eventually, on quitting python kernel will die. |
It works if not using ipython notebook. Otherwise freezes no matter.
…Sent from my iPhone
On Jun 13, 2018, at 5:33 AM, Harsh Thaker ***@***.***> wrote:
@benedictchen try this way:
cv2.imshow('image', im)
cv2.waitKey(0)
cv2.destroyAllWindows()
hit key to exit then. Closing the window will keep it still running and eventually, on quitting python kernel will die.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
As @benedictchen mentioned, nothing worked for me if I run it on Jupyter Notebook (the window hangs when closing and you need to force quit Python to close the window). I am on macOS High Sierra 10.13.4, Python 3.6.5, OpenCV 3.4.1. The below code works if you run it as a .py file (source: https://www.learnopencv.com/read-write-and-display-a-video-using-opencv-cpp-python/). It opens the camera, records the video, closes the window successfully upon pressing 'q', and saves the video in .avi format. import cv2
import numpy as np
# Create a VideoCapture object
cap = cv2.VideoCapture(0)
# Check if camera opened successfully
if (cap.isOpened() == False):
print("Unable to read camera feed")
# Default resolutions of the frame are obtained.The default resolutions are system dependent.
# We convert the resolutions from float to integer.
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
# Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
out = cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height))
while(True):
ret, frame = cap.read()
if ret == True:
# Write the frame into the file 'output.avi'
out.write(frame)
# Display the resulting frame
cv2.imshow('frame',frame)
# Press Q on keyboard to stop recording
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release the video capture and video write objects
cap.release()
out.release()
# Closes all the frames
cv2.destroyAllWindows() |
Worked for me as well. |
Hey please help me, I am facing problem while running my program. ` faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") while True:
` |
I had this problem,too. Until I add |
No solution I've seen works, including all mentioned here. Python: 3.6.3 |
I have been working on opencv with |
|
you can use "import matplotlib.pyplot as plt" |
Yeah this issue most definitely IS a bug. No rational user would ever expect an innocent command like |
the below code waits for user to press any key and then closes the image window: cv2.imshow( 'title' , img) |
Just make sure your other instances of python are closed. cv2.imshow uses your IDLE window to give output of your camera/video player feed. So, if any other instance/window is open, python(or jupyter notebook will run into a priority error and crash. Also if you want to close your output window, don't close it using the red cross button, but instead press any key on the keyboard. Hope these little tips solve your error. :) |
But it won't work if you are changing the image presets, like changing it into Grayscale. The image would still show greenish color in output for grescaled images. |
hi, I had some problem with cv2.imshow() too and with plt.imshow() the problem solved. but my task is hand detection and it should detect hand in a real-time video not separated frames. |
How come cv2.imshow() does not freeze on PyCharm, but freezes on VS Code? On Pycharm, cv2.imshow() opens in a new window without freezing Pycharm console |
Facing the same issue. After cv2.resize and cv2.cvtColor, cv2.imshow freezes but matplotlib.pyplot.imshow is working though grayscale still shows colors |
Try the same on Jupyter Notebook? It hangs |
this solved it for me, |
The cv2.imshow() only tends to freeze when I use Jupyter (ipynb) notebooks on my M1 MacBook Air. But where are if I run the complete code as a .py script in any IDE it is running perfectly fine.
The above code is running perfectly fine!! |
It's not only m1 notebooks. Also, I doubt anybody is gonna put in a fix
because it's been several years now.
…On Thu, May 6, 2021 at 10:28 PM Akella Niranjan ***@***.***> wrote:
The cv2.imshow() only tends to freeze when I use Jupyter (ipynb) notebooks
on my M1 MacBook Air. But where are if I run the complete code as a .py
script in any IDE it is running perfectly fine.
img = cv2.imread('img.png')
# cv2.imshow('image',img)
while(1):
cv2.imshow('img',img)
k = cv2.waitKey(33)
if k==27: # Esc key to stop
break
elif k==-1: # normally -1 returned,so don't print it
continue
The above code is running perfectly fine!!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#7343 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAJPSZ5Q2XTYJU4SQGVYMWLTMN26BANCNFSM4CQ2YBWA>
.
|
your video playback probably freezes because you have 0 in your waitkey call. the cv2 docs say that 0 means it will wait infinitely until any keypress occurs, so you should try having 32 in your waitkey function as a value i have deduced that works well. the use of the waitkey call is to take any key input while showing the frames. 0 means waiting infinitely until any event occurs and anything above zero means waiting for that many miliseconds. hope that helped |
Nice try, but no.
…On Wed, Sep 8, 2021 at 3:55 AM ADI ***@***.***> wrote:
your video playback probably freezes because you have 0 in your waitkey
call. the cv2 docs say that 0 means it will wait infinitely until any
keypress occurs, so you should try having 32 in your waitkey function as a
value i have deduced that works well. the use of the waitkey call is to
take any key input while showing the frames. 0 means waiting infinitely
until any event occurs and anything above zero means waiting for that many
miliseconds. hope that helped
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#7343 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAJPSZ5YVO47FTWR72WNXDDUA46KNANCNFSM4CQ2YBWA>
.
|
At least I'm glad it's not just me. I couldn't understand why my code, which runs perfectly from shell or from PyCharm, is always freezing my Jupyter notebook. All the code suggestions in this thread did not help. How can this still be a thing after 5 years, in such a famous project? macOS 11.6 |
Solved (sort of, sometimes)It seems the issue is isolated to the Notebook's iffy interaction with the Python GUI. Note, its not actually frozen because if you attempt to run again, or add more code below and continue execution, it will work fine and new calls to The fix that worked for me is adding a
|
OpenCV(4.5.4-dev) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:1274: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage In line cv2.imshow(" ", img) how should I overcome this error? |
|
I am running system: cap.release() This worked for me after I installed opencv-python, opencv-python-headless. |
it worked for me.
|
I realized opencv was installed twice on my machine, one coming from apt-get and the other from pip3 (python package manager). What worked for me is to uninstall the pip3 opencv package. |
This solved it for me too. I understand it's not a "bug bug", but I think it's still something that should be treated as such or documented, in that to be pragmatic many users are likely try this code in a jupyter/ipython environment first. |
I realize this isn't a support forum, but it was the first result Google search gave me. Try this:
, as others have suggested. My guess is the problem during interactive sessions is the python prompt forces focus to it, so hitting a key is directed towards the frozen command line and not the new imshow window. |
When I call for imshow() from python it automatically freeze.... it always happen when i try for reading video file or using VideoCapture()... please try to help me.... this is my code
The text was updated successfully, but these errors were encountered: