Python freezes after cv2.destroyWindow ()

I use openCV under Python 2.7 on Mac OS X (Lion) ... Whenever I run the code to just display the camera channel (from iSight), Python freezes. It seems that the camera is not actually cleaning. I used several different versions of the same code (below, from another question about SO) and get the same results (in cv or cv2). Does anyone know why this is happening? Here is the code:

import cv2 cv2.namedWindow("camera",1) capture = cv2.VideoCapture() capture.open(0) while True: img = capture.read()[1] cv2.imshow("camera", img) if cv2.waitKey(10) == 27: break cv2.destroyWindow("camera") 
+7
source share
4 answers

This is a problem with the whole nux based system. Please check this question and the answer from another question about StackOverflow.

DestroyWindow does not close window on Mac using Python and OpenCV

In short, you will need to call waitKey () to pump messages in OpenCV.

+3
source

Check out this alternative method, IT REQUIRES ANACONDA ENVIRONMENT My suggestion is to run the code in python in a terminal. You will not run into any problem.

Copy the same code and save using filename.py

 import cv2 input = cv2.imread('path_to_image.png') cv2.imshow('Hello World', input) cv2.waitKey(0) cv2.destroyAllWindows() 

then open a specific directory and then open a terminal

Open terminal - cd path/to/filename.py

  • source activates YOURPROFILE

  • python filename.py

This will solve the problem.

https://youtu.be/8O-FW4Wm10s

+1
source

I had the same problem and this is my solution.

 if cv2.waitKey(1) & 0xFF == ord('q'): break 

and after the cycle put this

 cap.release() cv2.destroyAllWindows() 

PD: in this line of code capture = cv2.VideoCapture () you must define one camera value.

 capture = cv2.VideoCapture(0) 
0
source

What IDE are you using? I ran into the same problem when I used default IDE (IDLE), but then I installed PyCharm , now it works fine, the image window closes instantly, also use waitkey(0) .

0
source

All Articles