简体   繁体   English

等效于 function 到 openCV Python 中的 input()

[英]Equivalent function to input() in openCV Python

I have written a small application in which a user clicks regions of an image to label, then labels that region of the image with a string.我编写了一个小型应用程序,其中用户将图像的区域单击到 label,然后用字符串标记图像的该区域。 I use openCV's onMouseCallback() to manage the clicks, and I read strings in using the input() function.我使用 openCV 的onMouseCallback()来管理点击,并使用input() function 读取字符串。 However, this is a less-than-desirable solution because the user must click to the console, type the string into the console, press enter, and then click back to the image window, for each labeling string.然而,这是一个不太理想的解决方案,因为用户必须单击控制台,在控制台中键入字符串,按回车键,然后针对每个标签字符串单击返回图像 window。

Is there a function in OpenCV that would allow for inputting a string without ever clicking away from the openCV window, essentially analogous to the input() function? Is there a function in OpenCV that would allow for inputting a string without ever clicking away from the openCV window, essentially analogous to the input() function?

One could probably achieve this result by defining a series of keystroke handlers, but this would be somewhat tedious to implement.可以通过定义一系列击键处理程序来实现这个结果,但这实现起来有点乏味。

I came up with a fairly simple solution, but I figured I'd post it here rather than deleting my question because I couldn't find this solution elsewhere on SE or online.我想出了一个相当简单的解决方案,但我想我会把它贴在这里而不是删除我的问题,因为我在 SE 或在线的其他地方找不到这个解决方案。 This doesn't currently work for special characters:这目前不适用于特殊字符:

import string    

def keyboard_input():
    text = ""
    letters = string.ascii_lowercase + string.digits
    while True:
        key = cv2.waitKey(1)
        for letter in letters:
            if key == ord(letter):
                text = text + letter
        if key == ord("\n") or key == ord("\r"): # Enter Key
            break
    return text

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

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