Python OpenCV - waitKey(0) does not respond?

后端 未结 12 794
忘掉有多难
忘掉有多难 2020-12-29 02:58

I\'m using opencv 2.4.7 on ubuntu 12.04. I\'m programming with python and I have a problem when i run this script:

import cv2

img = cv2.imread(\'34762092361         


        
相关标签:
12条回答
  • 2020-12-29 03:20

    Solved in Spyder under Ubuntu by following [Run]->[Configuration per file]->[Execute in an external system terminal].

    0 讨论(0)
  • 2020-12-29 03:22

    click on the image window(active) and then press and key, don't write in the terminal window.

    0 讨论(0)
  • 2020-12-29 03:23

    I found that it works if i press the key whilst the window is in focus. If the command line is in focus then nothing happens

    0 讨论(0)
  • 2020-12-29 03:26

    There is a problem with unix based system running opencv programs from python notebooks.

    Check this alternate method My suggestion is to run code in python in terminal. You will not face any kind of problem

    Copy the same code and save with filename.py

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

    then open specific directory and then open terminal

    Steps:

    Open Terminal
    cd path/to/filename.py
    source activate YOURPROFILE 
    python filename.py
    

    This will solve problem

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

    0 讨论(0)
  • 2020-12-29 03:27

    Try to execute the script directly from the Terminal works 100% for me but not from an IDE for example , i explain : I'm using fedora 20 and got the same problem, copying the first example from official opencv python tutorial, i'm using :

    • Fedora 20 64bit
    • Spyder IDE for python
    • Python Version 2.7.5
    • Fedora 64 bit
    • OpenCV 2.4.7

    Here is the code for test

    import cv2
    img = cv2.imread('/path/image1.jpeg',0)
    cv2.imshow('Display',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    When running this script using F5 from Spyder, it runs it using the embedded python terminal with this line :

    runfile('/home/user/Workspace/test.py', wdir=r'/home/user/Workspace')
    

    In this instance, cv2.waitKey(0) or cv2.waitKey(-1) are not working and windows remains opened after pressing keys with the code of the example Trying to close the windows will result in a "Not Responding , Force to Quit" Alert But when executing the script from Terminal , it works 100%

    didn't found the problem origin , will update if i find it.

    0 讨论(0)
  • 2020-12-29 03:29

    Here a minimalistic code for best performance on all platforms:

    import cv2
    
    img = cv2.imread("image.jpg")
    cv2.imshow("Window", img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    And now some observations:

    When the user wants to close the window through the press of the 0 key, he/she has to make sure that the 0 key is pressed whilst the window is in focus. Because as stated above, if the terminal is in focus nothing happens and code execution stucks at the cv2.waitKey(0) until the 0 key is pressed properly whilst the window is in focus.

    Pressing the 0 key whilst the window is in focus is the right way to close the window and make sure that, once the window is destroyed in line cv2.destroyAllWindows() and the program ends, the user can get back the control of the terminal.

    If the window is exited by mouse click, the window will be destroyed yes, but the user will very much end up in the situation of not being able to get back the control of the terminal. In this kind of situation the user may want to shut down the unresponsive terminal and open a new one.

    0 讨论(0)
提交回复
热议问题