Keyboard inputs

Now that we know how to capture a live video stream from the webcam, let's see how to use the keyboard to interact with the window displaying the video stream.

import argparse

import cv2

def argument_parser():
    parser = argparse.ArgumentParser(description="Change color space of the 
            input video stream using keyboard controls. The control keys are: 
            Grayscale - 'g', YUV - 'y', HSV - 'h'")
    return parser

if __name__=='__main__':
    args = argument_parser().parse_args()

    cap = cv2.VideoCapture(0)

    # Check if the webcam is opened correctly
    if not cap.isOpened():
        raise IOError("Cannot open webcam")

    cur_char = -1
    prev_char = -1

    while True:
        # Read the current frame from webcam
        ret, frame = cap.read()

        # Resize the captured image
        frame = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)

        c = cv2.waitKey(1)

        if c == 27:
            break

        if c > -1 and c != prev_char:
            cur_char = c
        prev_char = c

        if cur_char == ord('g'):
            output = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        elif cur_char == ord('y'):
            output = cv2.cvtColor(frame, cv2.COLOR_BGR2YUV)

        elif cur_char == ord('h'):
            output = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

        else:
            output = frame

        cv2.imshow('Webcam', output)

    cap.release()
    cv2.destroyAllWindows()

Interacting with the application

This program will display the input video stream and wait for the keyboard input to change the color space. If you run the previous program, you will see the window displaying the input video stream from the webcam. If you press G, you will see that the color space of the input stream gets converted to grayscale. If you press Y, the input stream will be converted to YUV color space. Similarly, if you press H, you will see the image being converted to HSV color space.

As we know, we use the function waitKey() to listen to the keyboard events. As and when we encounter different keystrokes, we take appropriate actions. The reason we are using the function ord() is because waitKey() returns the ASCII value of the keyboard input; thus, we need to convert the characters into their ASCII form before checking their values.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset