Video processing pipeline with OpenCV

Jarosław Gilewski
Nov 9, 2019 · 7 min read
Source: Pixabay

This story will show you how to extend the modular image processing pipeline using OpenCV and Python generators with a video stream.


Sequence of tasks for a face detection pipeline from a video stream.
Sequence of tasks for a face detection pipeline from a video stream.
Face detection pipeline from a video stream
$ git clone git://github.com/jagin/image-processing-pipeline.git
$ cd image-processing-pipeline
$ git checkout 7df1963247caa01b503980fe152138b88df6c526
$ conda env create -f environment.yml
$ conda activate pipeline
$ git pull
$ git checkout 7df1963247caa01b503980fe152138b88df6c526
$ conda env update -f environment.yml

Capture video

Pipeline generator task capturing video frames (pipeline/capture_video.py)
data = {
"image_id": f"{image_idx:05d}",
"image": image,
}

Detect faces

An example image from the movie “Friends” with detected faces (notice no false-positives).
Face detector class (pipeline/libs/face_detector.py)
Face detector test (tests/pipeline/libs/test_face_detector.py)
Detect faces pipeline task (pipeline/detect_faces.py)

Save faces and summary

output
├── 00000
│ └── 00000.jpg
├── 00001
│ └── 00000.jpg
├── 00002
│ └── 00000.jpg
...
├── 00260
│ ├── 00000.jpg
│ ├── 00001.jpg
│ ├── 00002.jpg
│ └── 00003.jpg
├── 00261
│ └── 00000.jpg
...
├── 00457
│ ├── 00000.jpg
│ └── 00001.jpg
├── 00458
│ └── 00000.jpg
├── 00459
│ └── 00000.jpg
└── summary.json

Video output

Image with an annotated face (green box and confidence value).

Pipeline in action

pipeline = (capture_video |
detect_faces |
save_faces |
annotate_image |
display_video |
save_video |
save_summary)
$ python process_video_pipeline.py -i assets/videos/faces.mp4 -p -d -ov faces.avi
$ python process_video_pipeline.py -i assets/videos/faces.mp4 -p
--batch-size 1
100%|███████████████████████████| 577/577 [00:11<00:00, 52.26it/s]
[INFO] Saving summary to output/summary.json...

$ python process_video_pipeline.py -i assets/videos/faces.mp4 -p
--batch-size 4
100%|███████████████████████████| 577/577 [00:09<00:00, 64.66it/s]
[INFO] Saving summary to output/summary.json...
$ python process_video_pipeline.py -i assets/videos/faces.mp4 -p
--batch-size 8
100%|███████████████████████████| 577/577 [00:10<00:00, 56.04it/s]
[INFO] Saving summary to output/summary.json...

$ python process_video_pipeline.py -d -p

Resources

DeepVision.guru

Deep Learning in Computer Vision

Jarosław Gilewski

Written by

I’m a senior software engineer involved in software development for more than 20 years. Currently, I’m focused on computer vision and deep learning.

DeepVision.guru

Deep Learning in Computer Vision

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade