0

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.

Community
  • 1
  • 1
  • Your code works fine for me. Can you try it from the command line shell instead of from idle? I had problems with idle and GUIs (I think it was tkinter) a long time ago. – KobeJohn Mar 27 '14 at 08:46

2 Answers2

0

try :

k = cv2.waitKey(3000) & 0xff
berak
  • 39,159
  • 9
  • 91
  • 89
0

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