Motion Detection using OpenCV
Sixth Installment of my Project Series
In the 4th post of my series I explained SMTP based surveillance system. To demonstrate a trigger based system, I used a switch to trigger video recording whenever I want to do so.
But why are we talking about a trigger based system?
It doesn’t make sense to be storing video when there is no suspicious activity going on, when there is no motion at all. Making empty videos is just waste space. And searching through those unwanted recordings is just a waste of time. A redundant system.
This post will explain how to implement basic motion detection, that can be used later on for our trigger based surveillance system.
What is OpenCV? —
OpenCV is a open source Computer Vision Library, and it’s goal is to provide a simple-to-use computer vision infrastructure which can help people build sophisticated applications quickly. It contains over 500 functions that span many areas in vision, including factory product inspection, medical imaging, security, user interface, camera calibration, stereo vision, and robotics.
— Learning OpenCV, Gary Bradski and Adrian Kaehler, O’Reilly
Before start installation, just update the system —
sudo apt update
1. Installing libraries and dependencies
$ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev
2. Installing GTK (GUI backend)
$ sudo apt-get install libgtk-3-dev
$ sudo apt-get install libcanberra-gtk*
3. Numerical optimization for OpenCV
$ sudo apt-get install libatlas-base-dev gfortran
4. Python3 development headers
$ sudo apt-get install python3.7-devAfter installing to check if the dev files are installed in correct directory check using this command
$ python3.7-config --includes-I/usr/include/python3.7m -I/usr/include/python3.7mThe first path is expected and second is current path.
If they are not the same then you need to perform a copy command to change the current path
$ sudo cp /current/path/to/dev/file/pyconfig.h /usr/include/python3.7mpyconfig.h is the dev file that you need to copy to correct directory
5. Install pip
$ sudo apt-get install python3-pipIf this won't work then use the following commands
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py$ sudo apt autoremoveIf pip is already installed then make sure it's the latest version
$ pip --versionIf not then -
$ pip install --upgrade pip
6. Virtual Environment
$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/get-pip.py ~/.cache/pipUpdate .bashrc to finish installing the tools, use nano or similar text editor -
# virtualenv and virtualenvwrapper
source /usr/local/bin/virtualenvwrapper.shsave and exit
And finally create the virtual environment-
$ mkvirtualenv cv -p python3Verify if cv environment is working or not -
$ workon cv
(cv) pi@raspberrypi:~$(cv) indicates that we are inside a virtual environment
7. Other Libraries
$ pip install numpyInstall imutils
$ pip install imutils
8. Finally OpenCV
When I was trying to install OpenCV it was taking a lot of time to get installed, and RasPi was hanging even after I tried installing repeatedly. Increasing Swap Memory let’s OpenCV compile without memory exhaustion and Pi will not hang. Even after increasing swap memory it took around 5 hrs for installation to complete on my Pi 3.
$ sudo nano /etc/dphys-swapfile
Edit CONF_SWAPSIZE as
save and exit. Check swap memory using these commands —
$ free -m
$ swapon -s
Increasing the swap memory takes up space from SD card only. So after increasing the swap memory only perform OpenCV installation
Now to install OpenCV
$ pip install opencv-python
$ pip install opencv-contrib-python // Ref.
After the installation is done revert the swap memeory back to it’s original value.
Verify if OpenCV is installed successfully —
I am using piimagesearch’s tutorial to implement basic motion detection. He have provided his code as Open Source, and is downloadable. When I run this code on my Pi it gave this error
When I searched for
imshow function, I understood that the code needs X-server to run. Because I am doing ssh to my Pi, the code was not running. Running this command gave me access to X-server during ssh.
ssh -Y pi@<IP>
Then I ran the python code again. And I got these three windows —
According to the tutorial, The code needs the first frame to be empty to detect if the area is occupied or unoccupied.
The 3 windows are —
Thresh is basically created using Image Thresholding which is used to make an image easier to analyze.
Frame Delta, which is a grey-scale image.
Security Feed, is the final video feed where it shows if someone is there in the frame, whether the area is occupied/unoccupied.
This code is very basic, as you can see even if only I am present in the frame, the code is sensing other movements as well like one block is for the ceiling fan, and there are multiple blocks. So more logic needs to be build upon it.
This implementation also helps me understand if I can even implement OpenCV in a low spec system like a Raspberry Pi. Though the response was slow as you can see from the GIF at the intro.
But this is a basic demonstration of motion detection, and I will cover more use cases in later posts.