Loading Video Source: Let us learn to program using OpenCV with Python by an example. Here, you will learn to read video, display video.

In other words, you will learn to capture video from Camera and display it on the screen.

You will be using these functions:

  1. cv2.VideoCapture(),
  2. cv2.cvtColor(),
  3. cv2.imshow(), and
  4. cv2.waitKey().

In the last tutorial, I have shared about how to install OpenCV and some other import libraries. If you have not installed them, refer the post: OpenCV with Python3.x Installation

Capture Video from Webcam and Display it

Often video streams are fed through webcam to computers. In this section let us look into how we can capture video using the computer’s webcam.

Example:

Here is the program to capture video using the webcam, convert it to grayscale and display it.

import numpy as np import cv2 #create object VideoCapture cap = cv2.VideoCapture(0) while(True): # Capture video frame-by-frame ret, frame = cap.read() # Convert frames to gray scale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the gray frames cv2.imshow('THIS IS MY FRAME',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the capture cap.release() cv2.destroyAllWindows()
Code language: PHP (php)

Understanding the Program

As we have discussed earlier, OpenCV and Numpy are libraries. These libraries need to be imported to use them, as:

import numpy as np import cv2
Code language: JavaScript (javascript)

Then we have to create the object of VideoCapture(). It is the function that that captures video from different sources, either video files or camera. If the video source is from the webcam or camera, indexes of cameras are fed in numerical format. If the video source is a video file, the file’s name with its extension is fed as the argument.

In the above example, the video source is from the first webcam of the computer denoted by the number zero

cap = cv2.VideoCapture(0)

Then start an infinite loop to capture video until it encounters a break statement. Define ret and frame as cv2.read().

  1. ret: It returns a Boolean value by obtaining return value.
  2. frame: It captures video in frames, frames by frames.
  3. read(): It returns a Boolean value, true or false (1 or 0). It returns value True when the frame is read correctly and vice versa.

After that, convert the frames into grayscale using cv2.cvtColor(). Here in the example, it takes two parameters, frame, and the color conversion code.

Now, the gray video feed is ready to show. To do this, we use cv2.imshow(). Here, the first parameter is the name of the frame, and the other is the processed gray frames. In the given example, the name of the frames is “THIS IS MY FRAME”

Run the statement once for each frame. Exit the loop when the keystroke from the user is a ‘q‘.

while(True): # Capture video frame-by-frame ret, frame = cap.read() # Convert frames to gray scale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the gray frames cv2.imshow('THIS IS MY FRAME',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break
Code language: PHP (php)

Finally, release the webcam and close all the imshow() windows.

cap.release() cv2.destroyAllWindows()
Code language: CSS (css)

Sample Output

Run the program. The following image is a sample output. The frames of video will be shown infinitely until you press ‘q’ from your keyboard.

OpenCV with Python Sample Code Output
OpenCV with Python Sample Code Output

Conclusion

In this section, I introduced you to some of the basic functions in OpenCV and how to use them. Here, we learned how to capture video and display it on the screen in frames.

Leave a Reply

Scroll to Top