简体   繁体   English

Python OpenCV - waitKey(0) 没有响应?

[英]Python OpenCV - waitKey(0) does not respond?

I'm using opencv 2.4.7 on ubuntu 12.04.我在 ubuntu 12.04 上使用 opencv 2.4.7。 I'm programming with python and I have a problem when i run this script:我正在使用 python 编程,但在运行此脚本时遇到问题:

import cv2

img = cv2.imread('347620923614738322_233985812.jpg')
cv2.namedWindow("window")
cv2.imshow("window", img)
cv2.waitKey(0)

The problem is that the script doesn't stop when I close the image.问题是当我关闭图像时脚本不会停止。 I searched information about waitKey and I found that using cv2.waitKey(0) is correct.我搜索了有关waitKey信息,发现使用cv2.waitKey(0)是正确的。

I don't understand, where is the problem?不明白,问题出在哪里?

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如果命令行处于焦点,则什么也不会发生

Adding a cv2.waitKey(1) after you destroy the window should work in this case.在这种情况下,在销毁窗口后添加 cv2.waitKey(1) 应该可以工作。

cv2.imshow('imgae',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)

This code works for me from IDLE:此代码适用于 IDLE:

# -*- coding: utf-8 -*-

# Objectif : découvrir le fonctionnement d'opencv-python
# http://opencv-python-tutroals.readthedocs.org/en/latest/index.html


import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('Lena.tiff',0)
WINDOW_NAME = 'Image de Lena'
cv2.namedWindow(WINDOW_NAME, cv2.CV_WINDOW_AUTOSIZE)
cv2.startWindowThread()

# Display an image
cv2.imshow(WINDOW_NAME,img)
cv2.waitKey(0) 


cv2.destroyAllWindows()

Hope this helps for future readers.希望这对未来的读者有所帮助。

单击图像窗口(活动)然后按 和 键,不要在终端窗口中写入。

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.当用户想要通过按下0关闭窗口时,他/她必须确保在窗口处于焦点时按下0键。 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.因为如上所述,如果终端处于焦点状态,则什么也不会发生并且代码执行会停留在cv2.waitKey(0)直到在窗口处于焦点状态时正确按下0键。

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 .在窗口处于焦点时按下0键是关闭窗口的正确方法,并确保一旦窗口在cv2.destroyAllWindows()行中被销毁并且程序结束,用户可以重新获得对终端的控制.

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.在这种情况下,用户可能想要关闭没有响应的终端并打开一个新终端。

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 :尝试直接从终端执行脚本对我来说是 100% 的工作但不是从 IDE 中执行,例如,我解释说:我使用的是 Fedora 20 并遇到了同样的问题,从官方 opencv python 教程中复制了第一个示例,我是使用:

  • Fedora 20 64bit Fedora 20 64 位
  • Spyder IDE for python适用于 Python 的 Spyder IDE
  • Python Version 2.7.5 Python 版本 2.7.5
  • Fedora 64 bit Fedora 64 位
  • OpenCV 2.4.7 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 :当使用来自 Spyder 的 F5 运行此脚本时,它使用嵌入式 python 终端运行它:

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%在这种情况下, cv2.waitKey(0) 或 cv2.waitKey(-1) 不起作用,并且在使用示例代码按下键后窗口保持打开状态尝试关闭窗口将导致“无响应,强制退出” " Alert但是当从终端执行脚本时,它 100% 工作

didn't found the problem origin , will update if i find it.没有找到问题的根源,如果我找到了会更新。

在Ubuntu下的Spyder中按照【运行】->【按文件配置】->【在外部系统终端中执行】解决。

Well the use cv2.waitKeyEx(0).那么使用 cv2.waitKeyEx(0)。

It runs in the same way and waits for your response until you revoke it.它以相同的方式运行并等待您的响应,直到您撤销它。

I hope it helps.我希望它有帮助。 Nithesh varmma尼特什瓦玛

cv2.waitKey(0) means that script is in infinity loop with 0 miliseconds wait after loop .only specified key can stop it. cv2.waitKey(0) 表示脚本处于无限循环中,循环后等待 0 毫秒。只有指定的键才能停止它。

you did not specified end of app condition.您没有指定应用程序结束条件。

Try this code: Using other keys for the waitKey() function of opencv试试这个代码: Using other keys for the waitKey() function of opencv

There is a problem with unix based system running opencv programs from python notebooks.基于 Unix 的系统从 python 笔记本运行 opencv 程序存在问题。

Check this alternate method My suggestion is to run code in python in terminal.检查这个替代方法 我的建议是在终端中的 python 中运行代码。 You will not face any kind of problem你不会遇到任何问题

Copy the same code and save with filename.py复制相同的代码并使用 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 https://youtu.be/8O-FW4Wm10s

This will work even if you close the window using the cross button on the window.即使您使用窗口上的十字按钮关闭窗口,这也将起作用。

import numpy as np
import cv2

img = cv2.imread('arvid.jpg', 0)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', img)

while True:
    k = cv2.waitKey(30) & 0xFF
    if k == 27:         # wait for ESC key to exit
        cv2.destroyAllWindows()
    elif k == ord('s'):  # wait for 's' key to save and exit
        cv2.imwrite('arvid2.jpg', img)
        cv2.destroyAllWindows()
    if cv2.getWindowProperty("image", 0) == -1:
        break

This problem occurs in some earlier version of opencv.这个问题出现在一些早期版本的 opencv 中。 So you just need to update opencv to the latest version.所以你只需要更新opencv到最新版本。 Version 4.0.0.21 worked for me.版本4.0.0.21对我4.0.0.21 Use the command below.使用下面的命令。

update opencv to version 4.0.0.21.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM