Senior Research Engineer
I owned a Raspberry Pi long ago and it was just sitting in my tech wash box. After watching a Youtube session of creative Raspberry applications, with envy , I decided to try something by myself. The first obvious idea to me was a home security system to inspect your house while you are away.
The final thingy is able to detect and roughly localize any motion through a camera. It takes photos and mails them to your email account. Plus, we are able to interact with it in our local network using a simple web interface so we are able to activate or deactivate it in front of home door. I assume that if someone is able to reach the local wifi network, most probably s/he is one of us (fair enough ?).
For the rest of this text, I try to summarize the important implementation details. Hopefully, you will find it interesting. You can also directly go to Github and check the implementation.
Starting with the hardware. What you need for this project:
We need to install the related Python libraries. They are all eligible by “pip install”
Let’s start coding! The first thing we need to do is importing our libraries like a boss! and then initializing raspi camera to make it ready to stream frames.
We like to detect motion for each frame but we also like to ignore false alerts. Therefore; we count number of consecutive detections and if it is larger then a given value, we really alert; otherwise we just ignore. Also, we give some time to our system to gather itself before starting to run.
As the next step, we start to loop through the frames and run background subtraction algorithm. In details, we apply following operations for detecting the motion;
Above steps give us a mask of pixel changes. This is not really enough. Before going further, we need some more tuning to reduce noise, fill holes and gather contours indicating moving segments. To go further;
After all these old-school algorithmic stages, we detect motion. Now time to implement logic of alert system. We start by checking the number of consecutive motion detections. If the count is above the threshold, we send a mail including snapshots taken within a certain time interval. Here, we like to capture variety of views with reasonable number of frame snapshots.
When it is Python, doing things are always easy so is sending an email. Initially, we need to hire a email address (I hired a gmail address). Then, we just use the below code to send an email with all the content we need. The tricky part is converting frames into right data format before attaching to the email.
The last part of the project is to create a website, reachable from the local network, used for enabling and disabling security system. For this purpose, we use Flask framework which happens to be the world’s easiest web server framework.
Now, we need to create a nice landing page with a basic html code supported by jQuery in order to send on/off ajax requests to our server and to update the system status. We code this and place into a folder called ‘templates’ in the root folder of the project. This is the default location for Flask html contents.
Someone might have more robust detection results by better algorithmic care. Maybe a more robust background modelling, couple of more image processing steps or better parameters for algorithms.
You can also mobilize the same system with a powerbank. I tried my 10000 mAh Anker powerbank and it keeps the system ~15 hours up. It is enough for a day, if you like to use this on a plug free spot.
One simple enhancement would be using an infrared camera for dark places. The device I shared above has a simple darkness blind camera but I started to use a infrared version recently. It really works well.
We are done here and good to go! Here I also share my Github repo. Feel free to comment and contribute.
Hope you never need a surveillance system for your house. But if you still need a cheap one, here I give you one. If you are lazy to implement all these contact me ([email protected]) and let me arrange you something. Best of luck!
Before leaving checkout Flash Gordon (Dropbox solution) of my Star Wars.