• Sabina Pokhrel

Faster Video Processing in Python using Parallel Computing




If you want to process a number of video files, it might take a from minutes to hours, depending on the size of the video, frame count, and frame dimensions.

How can we speed up video processing?
Parallel processing is the answer!

If you are processing images in batches, you can utilize the power of parallel processing and speed-up the task.


In this post, we will look at how to use python for parallel processing of videos.

We will read video from the disk, perform face detection, and write the video with output of face detection (bounding boxes) back to the disk.


Let's get started.



Install dependencies


We will need the following packages:


OpenCV: is a computer vision library that is commonly used. In this post, we will use OpenCV to read and write video files.


To install OpenCV on your device, you can use either pip command or apt-get command.

pip3 install opencv-python

or

sudo apt-get install -y python3-opencv

FFmpeg: is a cross-platform solution to record, convert and stream audio and video. In this post, we will use FFmpeg to join multiple video files.

To install ffmpeg, use the following apt-get command:

sudo apt-get install -y ffmpeg



Import python libraries


Let’s import the required python libraries.



Details on libraries used:


cv2: OpenCV library to read from and write to video files.

time: to get current time for calculating code execution time.

subprocess: to start new processes, connect to their input/output/error pipes, and obtain their return codes.

multiprocessing: to parallelize the execution of a function across multiple input values, distributing the input data across processes

xailient: library for face detection

Video Processing Pipeline using Single Process

We will first define a method to process video using single process. This is the way how we would normally read a video file, process each frame and write the output frames back to the disk.


Video Processing Pipeline using Single Process

We will first define a method to process video using a single process. This is the way how we would normally read a video file, process each frame and write the output frames back to the disk.


Let’s create another function that calls the video processor, takes note of start and end time, and calculates the time taken to execute the pipeline and the frames processed per second.


Video Processing Pipeline using Multiprocess

Now, let's define another function for processing video that utilizes mutiprocessing.


The was the above function works is that the video processing job, that is normally done using one process, is now divided equally amongst the total number of processors available on the executing device.


If there are 4 processes, and total number of frames in the video to be processed is 1000, then each process gets 250 frames to process, which are executed in parallel. This means that each process will create a separate output file, and so when the video processing is completed, there are 4 different output videos.


To combine these output videos, we will use FFmpeg.


Create a pipeline to run the multiprocessing of video and calculate time to execute and frames processed per second.


Results

I ran this experiment on Lenovo Yoga 920 with Ubuntu18.04 operating system. The number of logical processors available on this device is 8.






From this experiment, we can observe 2x more frames being processed per second when using all cores to process the video.


195 views

8 The Green Suite 6970

Dover, DE 19901

USA

Tel: +1(310)359-8357

11 York Street, Level 7

Sydney, NSW 2000

Australia

Tel: +61 434 965 010