Motion Detection using OpenCV

Sixth Installment of my Project Series

OpenCV Motion Detection on Raspberry Pi 3

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

Installing OpenCV —
I am referring to PiImageSearch’s tutorial to install OpenCV, and also this documentation.

Before start installation, just update the system — sudo apt update

1. Installing libraries and dependencies

2. Installing GTK (GUI backend)

3. Numerical optimization for OpenCV

4. Python3 development headers

5. Install pip

6. Virtual Environment

7. Other Libraries

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.

save and exit. Check swap memory using these commands —

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

After the installation is done revert the swap memeory back to it’s original value. CONF_SWAPSIZE=100

Verify if OpenCV is installed successfully —

Verifying OpenCV installation

Implementation —
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

Error while running the code

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 —

Running piimagesearch’s Python code

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.

Stay Tuned!

Hacking the Physical World | Senior Embedded Systems Engineer @ PiRhoAlpha Research (ActiveBuildings) | I write posts about AVR and Raspberry Pi.