Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Generating movie barcodes with OpenCV and Python

$
0
0

Generating movie barcodes with OpenCV and Python

In last week’s blog post I demonstrated how to count the number of frames in a video file .

Today we are going to use this knowledge to help us with a computer vision and image processing task ― visualizing movie barcodes , similar to the one at the top of this post.

I first became aware of movie barcodes a few years back from this piece of software which was used to generate posters and trailers for the 2013 Brooklyn Film Festival .

Since I started PyImageSearch I’ve received a handful of emails regarding generating movie barcodes, and awhile I normally don’t cover visualization methods , I ended up deciding to write a blog post on it. It is a pretty neat technique after all!

In the remainder of this tutorial I’ll be demonstrating how to write your own python + OpenCV application to generate movie barcodes of your own.

Looking for the source code to this post?

Jump right to the downloads section. Generating movie barcodes with OpenCV and Python

In order to construct movie barcodes we need to accomplish three tasks:

Task #1: Determine the number of frames in a video file. Computing the total number of frames in amovie allows us to get a sense of how many frames we should be including in the movie barcode visualization. Too many frames and our barcode will be gigantic ; too little frames and the movie barcode will be aesthetically unpleasing. Task #2: Generating the movie barcode data. Once we know the total number of videoframes we want to include in the movie barcode, we can loop over every N -th frame and compute the RGB average, maintaininga list of averages as we go. This serves as our actual movie barcode data. Task #3: Displaying the movie barcode. Given the list of RGB averages for a set of frames, we can take this data and create the actual movie barcode visualization that is displayed to our screen.

The rest of this post will demonstrate how to accomplish each of these tasks.

Movie barcode project structure

Before we get too far in this tutorial, let’s first discuss our project/directory structure detailed below:

|--- output/ |--- videos/ |--- count_frames.py |--- generate_barcode.py |--- visualize_barcode.py

The output directory will store our actual movie barcodes (both the generated movie barcode images and the serialized RGB averages).

We then have the videos folder where our input video files reside on disk.

Finally, we need three helper scripts: count_frames . py , generate_barcode . py , and visualize_barcode . py . We’ll be discussing each of these Python files in the following sections.

Installing prerequisites

I’ll assume you already have OpenCV installed on your system (if not, please refer tothis page where I provide tutorials for installing OpenCV on a variety of different platforms).

Besides OpenCV you’ll also need scikit-image and imutils . You can install both using pip :

$ pipinstall --upgradescikit-imageimutils

Take the time to install/upgrade these packages now as we’ll need them later in this tutorial.

Counting the number of frames in a video

In last week’s blog post I discussed how to (efficiently) determine the number of frames in a video file . Since I’ve already discussed the topic in-depth, I’m not going to provide a complete overview of the code today.

That said, you can find the source code for count_frames . py below:

# import the necessary packages from imutils.videoimport count_frames import argparse import os # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-v", "--video", required=True, help="path to input video file") ap.add_argument("-o", "--override", type=int, default=-1, help="whether to force manual frame count") args = vars(ap.parse_args()) # count the total number of frames in the video file override = False if args["override"] < 0 else True total = count_frames(args["video"], override=override) # display the frame count to the terminal print("[INFO] {:,} total frames read from {}".format(total, args["video"][args["video"].rfind(os.path.sep) + 1:]))

As the name suggests, this script simply counts the number of frames in a videofile.

As an example, let’s take the trailer to my favorite movie, Jurassic Park :

After downloading the .mp4 file of this trailer (included in the “Downloads” section at the bottom of this tutorial), I can execute count_frames . py on the video:

$ pythoncount_frames.py --videovideos/jurassic_park_trailer.mp4 [INFO] 4,790 totalframesread fromjurassic_park_trailer.mp4

As my output demonstrates, there are 4,790 frames in the video file.

Why does the frame count matter?

I’ll discuss the answer in the following section.

Generating a movie barcode with OpenCV

At this point we know how to determine the total number of frames in a video file ― although the exact reasoning as to why we need to know this information is unclear.

To understand why it’s important to know the total number of frames in a video file before generating your movie barcode, let’s dive into generate_barcodes . py :

# import the necessary packages import argparse import json import cv2 # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-v", "--video", required=True, help="path to input video") ap.add_argument("-o", "--output", required=True, help="path to output JSON file containing frame averages") ap.add_argument("-s", "--skip", type=int, default=0, help="# of frames to skip (to create shorter barcodes)") args = vars(ap.parse_args())

Lines 2-4import our required Python packages while Lines 7-14 parse our command line arguments.

We’ll require two command line arguments along with an optional switch, each of which are detailed below:

-- video : This is the path to our input video file that we are going to generate the movie barcode for. -- output : We’ll be looping over the frames in the input video file and computing the RGB average for every N- th frame. These RGB av

Viewing all articles
Browse latest Browse all 9596

Trending Articles