3

I'm very new to opencv, and I've been trying to get user input to exit out of all my open windows and escape the while loop. I've tried a bunch of different conditions, and checked that my Mac is giving keyboard permissions to python, but for some reason the waitKey function doesn't seem to be registering when I input anything into the keyboard. Any suggestions on what I should do ?

while True:
    screenshot = py.screenshot()
    screenshot = np.array(screenshot)
    screenshot = cv.cvtColor(screenshot, cv.COLOR_BGR2RGB)

    cv.imshow('Computer Vision', screenshot)

    if cv.waitKey(1) == ord('q'):
        print("shit")
        cv.destroyAllWindows()
        break
Hank Helmers
  • 33
  • 1
  • 8
  • Does the window that OpenCV creates have focus when you're pressing the key? – Dan Mašek Jun 18 '21 at 15:51
  • yeah it does, but it doesn't seem to update even when its selected and im pressing a key. – Hank Helmers Jun 18 '21 at 15:54
  • OK. [edit] the question and add the version of OpenCV that this is occurring with, and make your example a proper [mcve] to allow others that use Mac to easily reproduce it. – Dan Mašek Jun 18 '21 at 18:09

1 Answers1

1

Try cv2.waitKey(1) & 0xFF == ord('q'): as 0xFF can be necessary on certain OSs.

You may also try inspecting the values of cv.waitKey(1) and ord('q') to see how they differ.

mhudnell
  • 121
  • 1
  • 8
  • im trying to get a constant screenshots displayed, and when I use waitKey(0) it won't continuously show me the screenshots, just a singular one – Hank Helmers Jun 18 '21 at 15:26
  • Apologies, i see. Perhaps try `if cv2.waitKey(1) & 0xFF == ord('q'):`. It appears 0xFF can be necessary on certain OSs. Refer to https://stackoverflow.com/questions/53357877/usage-of-ordq-and-0xff – mhudnell Jun 18 '21 at 15:36
  • You may also try inspecting the values of `cv.waitKey(1)` and `ord('q')` to see how they differ. – mhudnell Jun 18 '21 at 15:39
  • right, so I tried the & 0xFF thing didn't seem to make a difference, and then the values of waitKey and ord('q), are -1 and 113 respectively. I just can't get the wait key to return any value other than -1 – Hank Helmers Jun 18 '21 at 15:44
  • Hm, the only other thing I can think of would be to make sure the image window is selected before pressing `q`. I'm at a loss for why it would return -1 if a key is pressed. – mhudnell Jun 18 '21 at 15:51
  • yeah same idk haha, thanks for all your help ! I think I just need to get a pc lol – Hank Helmers Jun 18 '21 at 15:54
  • 1
    it returns -1 upon timeout (1 means 1 millisecond of timeout). call `waitKey(10000)` so you have 10 seconds to press a key. – Christoph Rackwitz Jun 18 '21 at 16:28
  • oh okay I see, yeah now it works! but it now also only gives me a new screenshot after that amount of time as well, could I get the screenshots to still update while simultaneously checking for the keys, instead of having to wait 10 seconds for each new screenshot. – Hank Helmers Jun 18 '21 at 17:20
  • it should be enough to `waitKey(1)`, which should *not* lose any keypresses. I only suggested that as a test. – Christoph Rackwitz Jun 19 '21 at 11:55