Path: blob/master/Background-Subtraction/README.md
3118 views
Background Subtraction with OpenCV and BGS Libraries
This folder contains code for tutorial on Background Subtraction with OpenCV and BGS Libraries
Getting Started
Please, follow the instructions to set up the environment for Linux-based systems. This installation guide is based on the official instruction, with some additions and corrections.
If you are using Windows OS follow the Building BGSLibrary with Python support on Windows section from the same instruction.
Our code is tested using Python 3.7.5, but it should also work with any other python3.x. If you'd like to check your version run:
Note: We assume, that your current location is learnopencv/Background-Subtraction
and will refer it as work_dir
.
Virtual Environment
Let's create a new virtual environment. You'll need to install virtualenv package if you don't have it:
Now we can create a new virtualenv variable and call it env
:
The last thing we have to do is to activate it:
To will need also to install numpy package:
OpenCV
In this blog post we are using BGS Library, which is heavily based on OpenCV. That is why, we first need to build the OpenCV library. To do so:
Install dependencies:
Download the latest OpenCV version from the official repository:
Unzip the downloaded archives:
Rename the directories to match CMake paths:
Compile OpenCV Create and enter a build directory:
Run CMake to configure the OpenCV build. Don't forget to set the right pass to the PYTHON_EXECUTABLE
.
Check the output and make sure that everything is set correctly. After that we are ready to build it with:
Make sure, you didn't get any errors. Then run the following commands:
which creates the necessary links and cache to our freshly-built shared library.
Put lib/python3/cv2.cpython-37m-x86_64-linux-gnu.so
into the virtual environment installed packages:
BGS Library
Now everything is ready to build BGS Library.
Download the source code:
Make
build
folder and navigate to it:
Run CMake to configure the build. Don't forget to set
PYTHON_EXECUTABLE
to your virtual environment python.
Check the output. Pay attention to the
Python library status
section. It should look similar to this:
Make sure, that your python library is build as a shared library (.so file), not as a static (.a file). That might cause an error if you are using pyenv, that builds python library as a static library by default. To rebuild it as a shared library, run:
Build the BGS Library:
Make sure, you didn't get any errors. You can check, that everything is working by running the demo script:
Running the Demo
Python
The proposed for experiments background_subtr_opencv.py
and background_subtr_bgslib.py
scripts support --input_video
key to customize the background subtraction pipeline. --input_video
contains the path to the input video. By default its value is "space_traffic.mp4"
. Please, follow the below instructions for each case.
OpenCV Library
To run OpenCV case, please, choose one of the described below scenarios:
for the default input video:
for the custom input video:
BGS Library
To run BGSLibrary case, please, choose one of the described below scenarios:
for the default input video:
for the custom input video:
C++
The first step is compiling of the proposed .cpp
files with the appropriate commands for each case.
OpenCV Library
To compile background_subtr_opencv.cpp
you need to run the below command:
After background_subtr_opencv.out
was obtained, we can run the BS-pipeline:
By default space_traffic.mp4
will be used. To provide another video as input, you need to define --input
key value:
BGS Library
To compile background_subtr_bgslib.cpp
you need to run the below command:
After background_subtr_bgslib.out
was obtained, we can run the BS-pipeline:
By default space_traffic.mp4
will be used. To provide another video as input, you need to define --input
key value:
AI Courses by OpenCV
Want to become an expert in AI? AI Courses by OpenCV is a great place to start.