O'Reilly logo

OpenCV with Python By Example by Prateek Joshi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required