Assigning cv2.waitKey() to a variable in python?

Question:

I know you can use the ‘waitKey()’ function to wait for a while, but I saw a code block online for a video camera live recorder in which the function is assigned a variable to detect what key was pressed. What does this do?

Here is the code with added comments (code without comment below this one)(not the same as the one from the internet but this script works too):

# Importing stuff to be able to install libraries
from subprocess import check_call 
from sys import executable

# Installing the required libraries
check_call([executable, '-m', 'pip', 'install', 'numpy'])
check_call([executable, '-m', 'pip', 'install', 'opencv-python'])

# Importing cv2
import cv2

#Creating a window and assigning the video capture to a variable
cv2.namedWindow('Video Feed')
vc = cv2.VideoCapture(0)

# The while loop to always record
while True:
    
    # Reading the video
    rval, frame = vc.read()
    
    # Showing the correct frame to the 'Video Feed' window
    cv2.imshow('Video Feed', frame)
    
    # Assigning the function waitKey() to the variable 'key'
    # This is what I do not understand, why does it work and
    # why is can't I find any record on the internet of the function being able to do this?
    key = cv2.waitKey(20)

    # Checking if the variable key is 27
    # This is a true statement when you press 'ESC'
    # Where can I find the table for what numbers correspond to what keys?
    # Does this mean that the function outputs a number when assigned to a variable?
    if key == 27:
        
        # Exiting loop
        break

# Closing stuff
vc.release()
cv2.destroyWindow('Video Feed')

Code without comments because I can’t read with that many comments:

from subprocess import check_call
from sys import executable
    
check_call([executable, '-m', 'pip', 'install', 'numpy'])
check_call([executable, '-m', 'pip', 'install', 'opencv-python'])

import cv2

cv2.namedWindow('Video Feed')
vc = cv2.VideoCapture(0)

while True:
    
    rval, frame = vc.read()
    cv2.imshow('Video Feed', frame)
    
    key = cv2.waitKey(20)
    if key == 27:
        break

vc.release()
cv2.destroyWindow('Video Feed')

Basically, where do I find documentation on this way of using the function, and how can I use it for other keyboard presses?

Asked By: Hamligt

||

Answers:

Waitkey returns the ASCII code for the key pressed on the keyboard. In the above case, escape = 27, so the loop will exit when escape is pressed.

I’m not 100% sure how it works in python, but you should be able to say something like:

if 'k' == cv2.waitKey(1):
    # some code here after k key pressed

Here is a similar question, where it is explained for c++: OPENCV waitKey() method return type

And here is the opencv docs: https://docs.opencv.org/4.x/d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7

And here is the ASCII table:
https://www.asciitable.com/

Answered By: Matthew Copley
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.