Skip to content

Commit

Permalink
#12 Started implementing email notifications
Browse files Browse the repository at this point in the history
EmailSender class was created which sends the image to the given receiver. The class constructor receives the camera number and data from the camera in byte format, saves the image to temp folder and sends an email with the image as attachment.
Until a better solution is found, the email is sent from newly created gmail account and through gmail's SMTP server.
  • Loading branch information
mkristofic committed Jan 5, 2020
1 parent 0ec907a commit 52ca4fb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
6 changes: 6 additions & 0 deletions server/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import Flask, render_template, Response, jsonify
from modules.Detector import Detector
from modules.EmailSender import EmailSender
import socket, sys, signal

# FLASK CONFIG
Expand Down Expand Up @@ -36,10 +37,15 @@ def receive_signal(signal_number, frame):

def rec(sock, camera_num):
global face_detected
email_sent = False
while True:
data, addr = sock.recvfrom(65535)
if(detector.find_faces(data)):
face_detected[camera_num] = True
if(email_sent == False):
sender = EmailSender(camera_num, data)
sender.send_email('kristoficmiro0@gmail.com')
email_sent = True
else:
face_detected[camera_num] = False
yield (b'--frame\r\n'
Expand Down
55 changes: 55 additions & 0 deletions server/modules/EmailSender.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import cv2
import numpy as np
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from datetime import datetime

class EmailSender(object):

def __init__(self, camera_num, img_data):
self.data = img_data
self.camera_num = camera_num

def send_email(self, receiver_mail):
filename = 'face.jpg'
path = '/tmp/' + filename
sender = "sissurvailance@gmail.com"
receiver = receiver_mail
nparr = np.fromstring(self.data, np.uint8)
img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
cv2.imwrite(path, img_np)

now = datetime.now()
date_and_time = now.strftime("%d.%m.%Y. %H:%M:%S")

msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = "Face detected"
body = f"At {date_and_time}, camera #{self.camera_num} detected a face. Find attached image with the detected face."
msg.attach(MIMEText(body, 'plain'))

attachment = open("/tmp/face.jpg", "rb")
p = MIMEBase('application', 'octet-stream')
p.set_payload((attachment).read())
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(p)

username = "sissurvailance@gmail.com"
password = "fw#22xCz"

try:
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login(username, password)
text = msg.as_string()
smtpObj.sendmail(sender, receiver, text)
smtpObj.quit()
print(f"Email sent to {receiver}.")
except Exception as e:
print("Unable to send the email.")

0 comments on commit 52ca4fb

Please sign in to comment.