简体   繁体   English

现在无法使用python和opencv获取密钥代码

[英]can't get the keycode now, using python and opencv

okay, i've been using this basic thing for last few months to get key events such as ESC key, or E key etc, to do things when they are pressed in my opencv program. 好的,最近几个月我一直在使用这种基本功能来获取按键事件,例如ESC键或E键等,以便在我的opencv程序中按下它们时执行操作。

Now earlier, for detecting ESC key, the code was there already in all the examples in the samples folder and on the web. 现在,为了检测ESC键,示例文件夹和Web上的所有示例中都已经存在代码。 I understood that. 我明白。 From that i hacked up a way to get the keycodes for other keys, such as E and S etc. Because i wanted to use them to take snapshots of the current frames under processing in my program. 从那以后,我想出了一种获取其他键(例如E和S等)的键代码的方法。因为我想使用它们来获取程序中正在处理的当前帧的快照。 And it worked, i found out for E its 101, like for ESC its 27 But that's the ASCII value or something? 它起作用了,我为E找到了101,就像为ESC找到了27,但这是ASCII值还是什么? I'm not sure. 我不确定。

This is what i'm doing now also, but its just not printing the keycode: 这也是我现在正在做的,但是它只是不打印键码:

import cv

while True:

   k = cv.WaitKey(100) % 0x100

   if k != 255:
        print k

So that when i press some key which doesn't have a value of 255, i should be notified that the that key was pressed. 这样,当我按下某个键而不是255时,应该通知我该键已按下。 It works in my opencv programs: 它在我的opencv程序中有效:

while True:



  k = cv.WaitKey(10)

  if k % 0x100 == 27: # if ESC key is pressed....

    #cv.SaveImage(filename,threshed2)
    cv.DestroyWindow("threshed") #Destroy the window of detection results
    cv.DestroyWindow("gray")
    cv.DestroyWindow("edge")
    cv.DestroyWindow("pupil")
    #cv.DestroyWindow('smooth')
    break

  elif k == 101: #if E is pressed
        eyeSnap = "C:\\Users\\ronnieaka\\Desktop\\PROJ\\openCV\\eyeSnaps\\det_pupil.jpg"
        cv.SaveImage(eyeSnap,image)

I know its a basic thing and i just was playing around to get the keys pressed and do something else not related to OpenCV Why i'm i not getting 101 from the previous code when i press E or the code for other keys when i press them? 我知道这是基本的事情,我只是在玩耍以使按键被按下,并执行其他与OpenCV不相关的操作为什么我在按E时没有从上一个代码中获得101或在按I时获得其他键的代码他们?

This used to be super simple: 过去非常简单:

k = cv.WaitKey(10)
if k == 27:         
    # ESC key was pressed

The code above don't work on your system? 上面的代码在您的系统上不起作用?


I have the same problem as you, but I always could not get keycode (only get 255) 我和您有同样的问题,但我始终无法获取键码(只能获取255)
I find it because the cv.WaitKey only receive key from the windows you create not python command line :) 我找到它是因为cv.WaitKey仅从您创建的Windows接收密钥,而不是python命令行:)

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

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