Faster Video Processing in Python using Parallel Computing | by Sabina Pokhrel | Towards Data Science

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 prallel 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.
Lets get started.
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
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
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.
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.
Now, lets 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.
I ran this experiment on Lenovo Yoga 920 with Ubuntu18.04 operating system. The number of logical processors available on this device are 8.
From this experiment, we can observe 2x more frames being processed per second when using all cores to process the video.
Found this post helpful? Leave your thoughts as comments below.
Originally published in www.xailient.com/blog.
About the author
Sabina Pokhrel works at Xailient, a computer-vision start-up that has built the world’s fastest Edge-optimized object detector.