Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Smart-Security-Camera
# Smart-Security-Camera MQTT support
IoT Raspberry Pi security camera running open-cv for object detection. The camera will send an email with an image of any objects it detects. It also runs a server that provides a live video stream over the internet.

[Watch the original video here](https://youtu.be/Y2QFu-tTvTI)
Expand Down Expand Up @@ -110,3 +110,20 @@ Visit `<raspberrypi_ip>:5000` in your browser to view the stream.
Note: To view the live stream on a different network than your Raspberry Pi, you can use [ngrok](https://ngrok.com/) to expose a local tunnel. Once downloaded, run ngrok with `./ngrok http 5000` and visit one of the generated links in your browser.

Note: The video stream will not start automatically on startup. To start the video stream automatically, you will need to run the program from your `/etc/rc.local` file see this [video](https://youtu.be/51dg2MsYHns?t=7m4s) for more information about how to configure that.

## MQTT

Update following line with appropiate MQTT server, it can be any MQTT server

```
client.connect("192.168.0.xxx", 1883, 60)
```

Update the topics that will reveice the motion notification

```
client.publish("home/door/front/motion","ON",0,False)
client.publish("home/door/front/camera",frame,0,True)
```

The topic "home/door/front/camera" can be read as a MQTT camera stream https://home-assistant.io/components/camera.mqtt/
2 changes: 1 addition & 1 deletion camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class VideoCamera(object):
def __init__(self, flip = False):
self.vs = PiVideoStream().start()
self.vs = PiVideoStream(resolution=(1280,720),framerate=30).start()
self.flip = flip
time.sleep(2.0)

Expand Down
19 changes: 16 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import cv2
import sys
from mail import sendEmail
import paho.mqtt.client as mqtt
from flask import Flask, render_template, Response
from camera import VideoCamera
import time
import threading


def on_connect(client, userdata, flags, rc):
print("Connected to MQTT server with result code :"+str(rc))

client = mqtt.Client()
client.on_connect = on_connect
client.connect("192.168.0.xxx", 1883, 60) # use your MQTT server name
client.loop_start()

email_update_interval = 600 # sends an email only once in this time interval
video_camera = VideoCamera(flip=True) # creates a camera object, flip vertically
object_classifier = cv2.CascadeClassifier("models/fullbody_recognition_model.xml") # an opencv classifier
Expand All @@ -21,11 +31,14 @@ def check_for_objects():
frame, found_obj = video_camera.get_object(object_classifier)
if found_obj and (time.time() - last_epoch) > email_update_interval:
last_epoch = time.time()
print "Sending email..."
print("Sending email...")
sendEmail(frame)
print "done!"
print("Sending MQTT message...")
client.publish("home/door/front/motion","ON",0,False)
client.publish("home/door/front/camera",frame,0,True)
print("done!")
except:
print "Error sending email: ", sys.exc_info()[0]
print("Error sending email: ", sys.exc_info()[0])

@app.route('/')
def index():
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
imutils
flask
picamera[array]
paho-mqtt
9 changes: 9 additions & 0 deletions runserver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

HOME=/home/pi
VENVDIR=$HOME/.virtualenvs/cv
BINDIR=$HOME/Smart-Security-Camera

cd $BINDIR
source $VENVDIR/bin/activate
python $BINDIR/main.py
3 changes: 3 additions & 0 deletions systemd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1. Place picam.service inside /etc/systemd/system folder
2. Activate service sudo systemctl activate picam.service
3. Start the service sudo systemctl start picam.service
12 changes: 12 additions & 0 deletions systemd/picam.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=Pi WebCam Service with CV
After=network.target

[Service]
Type=simple
User=pi
ExecStart=/home/pi/Smart-Security-Camera/runserver.sh
Restart=always

[Install]
WantedBy=multi-user.target