Welcome back to our computer vision blog series! Thus far, we have discussed Working with Images using OpenCV, Feature Extraction, and Foundations of Image Processing. Today, we’ll delve deeper into the exciting realm of real-time streaming and video processing with OpenCV. Videos are simply collections of images, or frames, and many of the ideas we have discussed thus far can also be used to video processing.

This blog post will discuss:

  1. Setting up OpenCV for video processing.
  2. Reading and displaying video files.
  3. Capturing video from a webcam.
  4. Performing real-time video processing.
  5. Saving processed video output.

Setting Up OpenCV for Video Processing

Verify that OpenCV is installed. Installing it with pip is possible:

pip install opencv-python

Reading and Displaying Video Files

Using OpenCV to read and display video files is simple. Here’s a quick script that reads a video file and shows each frame individually:

import cv2

# Open a video file
video = cv2.VideoCapture('path_to_your_video.mp4')

# Check if the video was successfully opened
if not video.isOpened():
    print('Error: Could not open video.')
else:
    while True:
        # Read a new frame
        ret, frame = video.read()

        # Break the loop if no frame is read
        if not ret:
            break

        # Display the frame
        cv2.imshow('Video', frame)

        # Break the loop on 'q' key press
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # Release the video capture object
    video.release()
    cv2.destroyAllWindows()

This script uses video.read() to read each frame of the video and cv2.VideoCapture() to open the video file. The loop ends if either the 'q' key is pressed or no frame is read. The frames are displayed by the cv2.imshow() function.

Capturing Video from a Webcam

Similar to reading from a video file, capturing video from a webcam requires using the camera device index, which is typically 0 for the default camera:

import cv2

# Open the default camera (0)
video = cv2.VideoCapture(0)

# Check if the camera was successfully opened
if not video.isOpened():
    print('Error: Could not open webcam.')
else:
    while True:
        # Read a new frame
        ret, frame = video.read()

        # Break the loop if no frame is read
        if not ret:
            break

        # Display the frame
        cv2.imshow('Webcam', frame)

        # Break the loop on 'q' key press
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # Release the video capture object
    video.release()
    cv2.destroyAllWindows()

Performing Real-Time Video Processing

Using image processing techniques to each frame of the video as it is being recorded is known as real-time video processing. As an illustration, let’s apply a grayscale filter to every webcam feed frame:

import cv2

# Open the default camera (0)
video = cv2.VideoCapture(0)

# Check if the camera was successfully opened
if not video.isOpened():
    print('Error: Could not open webcam.')
else:
    while True:
        # Read a new frame
        ret, frame = video.read()

        # Break the loop if no frame is read
        if not ret:
            break

        # Convert the frame to grayscale
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Display the grayscale frame
        cv2.imshow('Webcam - Grayscale', gray_frame)

        # Break the loop on 'q' key press
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # Release the video capture object
    video.release()
    cv2.destroyAllWindows()

Saving Processed Video Output

The processed video output can also be saved to a file. This is an example of how to record video from the webcam, turn it into grayscale, and then save the result:

import cv2

# Open the default camera (0)
video = cv2.VideoCapture(0)

# Get the width and height of the frames
frame_width = int(video.get(3))
frame_height = int(video.get(4))

# Define the codec and create a VideoWriter object
out = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width, frame_height), isColor=False)

# Check if the camera was successfully opened
if not video.isOpened():
    print('Error: Could not open webcam.')
else:
    while True:
        # Read a new frame
        ret, frame = video.read()

        # Break the loop if no frame is read
        if not ret:
            break

        # Convert the frame to grayscale
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Write the frame to the output file
        out.write(gray_frame)

        # Display the grayscale frame
        cv2.imshow('Webcam - Grayscale', gray_frame)

        # Break the loop on 'q' key press
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # Release the video capture and writer objects
    video.release()
    out.release()
    cv2.destroyAllWindows()

In this blog, we looked at using OpenCV to work with video. We gained knowledge on how to read and play back video files, record video straight from a webcam, process video in real time, and store the finished product. Numerous computer vision applications, such as real-time monitoring, video analysis, and surveillance, depend on these abilities.

Keep checking back for our upcoming blog post, where we’ll discuss more sophisticated computer vision methods and applications. Have fun with coding!

Categories: Uncategorized