user2455916
user2455916

Reputation:

opencv cv2.destroyAllWindows() doesnt respond

I have 64bit, ubuntu system. Im running the code in idel. I was facing opencv hang issue , the image shows up but I have to force kill the image window. So reffered to this thread -- Using other keys for the waitKey() function of opencv

import cv2
img = cv2.imread('sof.jpg') # load a dummy image
while(1):
    cv2.imshow('img',img)
    k = cv2.waitKey(3000) & 0xff
    if k==32:    # SpaceBar key to stop
        break
    elif k==-1:  # normally -1 returned,so don't print it
        continue
    else:
        print k # else print its value

Still its not working image hangs and I have to close it manually.

Upvotes: 0

Views: 2933

Answers (2)

R K Bhalodia
R K Bhalodia

Reputation: 101

Add cv2.waitkey(0) and cv2.destroyallwindows() but If you have used python notebooks then there is a problem in Unix based system to run a program of opencv. It will cause system freeze so you will need to restart kernel everytime when you try to execute code.

I have an alternative method which would prevent from freezing your system

Steps:

  1. -Copy the code from python notebooks and create new filename.py and paste it
    • Open terminal
    • cd path/to/file
    • source activate VirtualEnvironment
    • python filename.py

This will run code directly from terminal. Hope this helps you. Example Link: https://youtu.be/8O-FW4Wm10s

Upvotes: 0

berak
berak

Reputation: 39796

try :

k = cv2.waitKey(3000) & 0xff

Upvotes: 0

Related Questions