From d96fe00be00b605c852f16f3739969e27f9e6ce6 Mon Sep 17 00:00:00 2001 From: debsahu Date: Thu, 19 Oct 2017 17:57:12 -0400 Subject: [PATCH 1/9] Update requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index a2ba31f..128965b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ imutils flask picamera[array] +paho-mqtt From bfa14d358bd9e8b5e3e84265f6928bf2da542a89 Mon Sep 17 00:00:00 2001 From: debsahu Date: Thu, 19 Oct 2017 18:02:22 -0400 Subject: [PATCH 2/9] Added MQTT publishing motion command to a topic as well as frame image on another topic --- main.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index fef7650..a59a949 100644 --- a/main.py +++ b/main.py @@ -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 @@ -21,11 +31,18 @@ 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!" + #cv2.imwrite("/tmp/motion.jpg",frame) + #tmpFile = open("/tmp/motion.jpg","rb") + #byteArr=bytearray(tmpFile) + #client.publish("home/door/front/camera",byteArr,0,True) + 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(): From b5b0149fc897f00bfad7b6fe4b4d12ad1d4c6c07 Mon Sep 17 00:00:00 2001 From: debsahu Date: Thu, 19 Oct 2017 18:07:04 -0400 Subject: [PATCH 3/9] Added MQTT description --- README.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 19860ed..83f743e 100644 --- a/README.md +++ b/README.md @@ -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) @@ -110,3 +110,20 @@ Visit `: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/ From 14f8c814899eb7625e36d222706afbfe294f9fa4 Mon Sep 17 00:00:00 2001 From: debsahu Date: Thu, 19 Oct 2017 18:09:55 -0400 Subject: [PATCH 4/9] Update main.py --- main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/main.py b/main.py index a59a949..8d7e115 100644 --- a/main.py +++ b/main.py @@ -36,6 +36,7 @@ def check_for_objects(): #cv2.imwrite("/tmp/motion.jpg",frame) #tmpFile = open("/tmp/motion.jpg","rb") #byteArr=bytearray(tmpFile) + print("Sending MQTT message...") #client.publish("home/door/front/camera",byteArr,0,True) client.publish("home/door/front/motion","ON",0,False) client.publish("home/door/front/camera",frame,0,True) From 1639b176d1183fae0a02619aafd27a0661cc2d85 Mon Sep 17 00:00:00 2001 From: debsahu Date: Mon, 23 Oct 2017 16:46:31 -0400 Subject: [PATCH 5/9] Bash script to start this server --- runserver.sh | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 runserver.sh diff --git a/runserver.sh b/runserver.sh new file mode 100644 index 0000000..1897ebc --- /dev/null +++ b/runserver.sh @@ -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 From 8e5fc3330eded8c4baac9c912b980914d7923e6a Mon Sep 17 00:00:00 2001 From: debsahu Date: Mon, 23 Oct 2017 16:48:17 -0400 Subject: [PATCH 6/9] Systemd service file to start bootup python script --- systemd/picam.service | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 systemd/picam.service diff --git a/systemd/picam.service b/systemd/picam.service new file mode 100644 index 0000000..8ce2200 --- /dev/null +++ b/systemd/picam.service @@ -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 From aa12713627db6ea11258ec6003427ace57e2fcbb Mon Sep 17 00:00:00 2001 From: debsahu Date: Mon, 23 Oct 2017 16:50:20 -0400 Subject: [PATCH 7/9] README --- systemd/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 systemd/README.md diff --git a/systemd/README.md b/systemd/README.md new file mode 100644 index 0000000..eae5ab0 --- /dev/null +++ b/systemd/README.md @@ -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 From 01fa8d77fb450608456f8723a57e63715f61726a Mon Sep 17 00:00:00 2001 From: debsahu Date: Tue, 24 Oct 2017 15:03:54 -0400 Subject: [PATCH 8/9] added custom resolution to camera --- camera.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/camera.py b/camera.py index 065a321..02c3545 100644 --- a/camera.py +++ b/camera.py @@ -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) From 39f3d2c7cc2989e28c98df4124bb197d0db2f59c Mon Sep 17 00:00:00 2001 From: debsahu Date: Tue, 7 Nov 2017 10:00:18 -0500 Subject: [PATCH 9/9] fix tabs and spaces --- main.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 8d7e115..b32e1b9 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,7 @@ def on_connect(client, userdata, flags, rc): - print("Connected to MQTT server with result code :"+str(rc)) + print("Connected to MQTT server with result code :"+str(rc)) client = mqtt.Client() client.on_connect = on_connect @@ -33,14 +33,9 @@ def check_for_objects(): last_epoch = time.time() print("Sending email...") sendEmail(frame) - #cv2.imwrite("/tmp/motion.jpg",frame) - #tmpFile = open("/tmp/motion.jpg","rb") - #byteArr=bytearray(tmpFile) print("Sending MQTT message...") - #client.publish("home/door/front/camera",byteArr,0,True) client.publish("home/door/front/motion","ON",0,False) - client.publish("home/door/front/camera",frame,0,True) - + client.publish("home/door/front/camera",frame,0,True) print("done!") except: print("Error sending email: ", sys.exc_info()[0])