Introduction: Home Security System With Computer Vision
I always wanted to create a system which can exchange an expensive security system with monthly costs and I always wanted to use my Raspberry Pi for some useful tasks. This is why I ordered the cheapest webcamera from Ebay and started my Home Security Project.
I am a Computer Vision and Deep Learning enthusiast so I used this knowledge just to only use a WebCamera for a project like this.
With this project you can easily make your home more secure when you are not at home! It is cheap and you only need a Raspberry Pi and a simple (cheap) WebCamera!
Let's get to it! :)
Step 1: What You Will Need
We will need a few things:
- Raspberry Pi - I use Raspberry Pi 2 B but any type is okay
- Simple Usb WebCamera - we will connect this to the Pi
- Internet connection
- Ethernet cable or Wifi dongle - just make sure your Pi is connected to the internet
That's it, we can go to the next step.
Step 2: Prepare the Pi
- Connect the webcamera to it.
- Turn the Pi on
- Make sure that it is connected to the internet
We will use Python 2.7 (default for Raspberry Pi) but the project is made for Python 3.5 so if you would like to you can use that! This makes things easier.
Now you have to install OpenCV. This library is responsible for the computer vision tasks. The code will work with both OpenCV 2 and OpenCV 3. (Just type the commands line by line in the terminal.)
You can find really good install instructions here for Pi 3 and here for Pi 2 or you can follow my simple easy install (disadvantage: this is a really old version).
This is how you can install OpenCV 2.4.9:
sudo apt-get -y update sudo apt-get -y upgrade sudo apt-get install -y python-pip sudo pip install numpy sudo apt-get install libopencv-dev python-opencv
Now we need Pyrebase. This module is required to use Firebase.
sudo pip install pyrebase
Flask is needed too. This is required to create the webpage for the user interactions.
sudo pip install flask
Now, we are done!
Step 3: Create Firebase Project
We need to create a Firebase project in order to use our Home Security System. With Firebase we are able to authenticate users and store captured images. We capture images when we detected motion with the camera. So only those can access and START or STOP the system who you give permission to.
- Go to https://console.firebase.google.com/
- Create a new Project (I named it Home Security)
- Setup Authentication:
- Enable Email/Password user authentication (Under Sing-in Method)
- Add users (for yourself, your mom, dad, etc...) - They will be able to start or stop the security system
Save apiKey, authDomain, databaseURL, storageBucket, messagingSenderId
Now you have everything, so we can start coding. Best part. ;)
Step 4: Coding!!!
Okay..., we won't code the whole thing from scratch because that would not be a small job. So this is why I created a Github Repo where you can find the code for the project. If you are brave enough or you are adventurous you can modify the code and experiment with it. I encourage you to play with the code.
Okay, so here you can find the repo. You have to options: if you are familiar with Git, you can clone it. Or you can download it and extract it. That's easy.
If you don't know what is cloning on Git or what is Git, check out this tutorial. I think it is really useful and you can learn a lot of things.
After you downloaded or cloned the repo:
I would like to talk just a little about the Image Processing/Computer Vision part because that is really interesting. So basically this security system is a motion detector. We can detect motion with computer vision algorithms. I wrote a blog post about this. You can read it here. I also uploaded the image processing images here, so you can easily understand what is going on in the "brain" of the system.
Do you remember that you savedapiKey, authDomain, databaseURL, storageBucket, messagingSenderId from you newly created Firebase Project? Now we will need it, because you have to update the config.ini file.
For example: (just replace <your_...> parts)
[Firebase] apiKey : your_key authDomain : your_domain databaseURL : your_database_url storageBucket : your_stotage_bucket messagingSenderId : your_messaging_sender_id
After this you can test it!
Step 5: Test Your System
As I said, now it is time for testing.
Navigate to the folder where you can find app_home_security.py
This is the main script, so you have to run this in order to start the whole system.
Get Pi's ip address
We will need the Pi's ip, so we will be able to reach the login page from a different device.
For example my Pi's ip is: 192.168.0.6 (I will use this)
- Pi is connected to the internet
- WebCamera is connected to the Pi
- Firebase project is ready
- You updated config.ini
Now run it and let's see what happens!
After that you can reach the System from your phone! It has started! :)
Just go to your phone's browser and go to: 192.168.0.6:5000. (The format should be: your_pi_ip_address:5000).
This works with everything as long as it is connected to your LAN and which has a browser because we just interact with a simple webpage.
You should see something like I included in the picture. That's my phone's browser.
After you login and start the system, try walking in front of the camera which is connected to the Pi. After that check out your Firebase Project's Storage. You will find the photos there when it detected movement!
You can also modify the script so it can notify you via Slack, Facebook, Email messages when it detected something. (in my github repo I wrote about that which I won't include here because that's another story)
Hooorrraaayyyy! That's it! You have a fully working Home Security System for just a price of a Raspberry Pi!
Step 6: Side Note
This project is made for Rapberry Pi, but if you would like to, you can test in on your laptop, PC, etc...
You only need to follow the steps which you can find in the Github repo's README (under the source codes).
And if you like the results, you can use your Raspberry Pi for an actual Home Security System!