Security camera for raspberry pi. It takes pictures at every 2 seconds and if any difference is detected between the original and the current images, it sends by e-mail with the timestamp (GMT) and stores in a Google Drive account
After installing the pip dependencies and configure e-mail credentials and google drive api credentials (see below), simply call
python3 run.py
To create a service that automatically runs the piSecurityCamera at startup we need to create a systemd service
- Create a unit file for our service
sudo nano /lib/systemd/system/picamera.service - Add the following lines
[Unit]
Description=piSecurityCamera
After=multi-user.target
[Service]
WorkingDirectory=/home/pi/piSecurityCamera
ExecStart=/usr/bin/python3 run.py
[Install]
WantedBy=multi-user.target
- Reload the systemd by calling
sudo systemctl daemon-reload
After rebooting your security camera will start automatically
The motion detection algorithm is pretty simple:
- First of all I take a picture and call it base image
- Then, inside an infinite loop I take another picture, called current picture
- There is a image diff method that calculates an image difference value
- Apply gaussian filter to both images
- Reduce both images to a 8x8 pixels matrix
- calculate pixel by pixel value difference (each pixel is the sum of the three band values) between both images
- get the average of all pixels difference
- If this difference value is above a stablished threshold (I set as 15 but can be different), I save the image locally and inside my Google Drive. I also replace base image by current image and discard the base.
- If this difference is above a higher threshold (I set as 50), I also send an e-mail to my address
They way Google drive creates and manages api credentials looked quite complex to me and as I didn't want to spend too much time on this (I just wanted to see it working at first), I created a quickstart credential (which also is not a simple task as you might see below).
So, to get a credential for our project:
- open the page [https://developers.google.com/drive/api/v3/quickstart/python]
- click on "Enable Drive API" button. It will download a file called credentials.json
- Copy the code example below in the same page and save in a py file in a pc with Window manager (cause my raspberry pi doesn't have)
- Change the line
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']toSCOPES = ['https://www.googleapis.com/auth/drive']. This will allow using Google Drive API not only for reading but also for file write - Run this python file (
Credentials.jsonmust be in the same folder). It will open a browser window asking for authorization to run the project. Next, Next, Next and finish - After all, you will get 2 files called
Credentials.json(from the second step) andtoken.picke(created in step 5). Copy both files underresourcesfolder of our project
If you are using GMail as e-mail provider to send the images, please remember to enable it from logging from less secure apps by clicking on the link below and enabling the option:
[https://www.google.com/settings/security/lesssecureapps]
Inside the /resources folder there is a template properties file that must be filled with your e-mail data and renamed to email.properties
- PIL (as Pillow)
pip3 install Pillow
- Google API
pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
- ConfigParser (to read e-mail properties)
pip3 install configparser