diff --git a/server/app.py b/server/app.py index bbf187e..f594251 100644 --- a/server/app.py +++ b/server/app.py @@ -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 @@ -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' diff --git a/server/modules/EmailSender.py b/server/modules/EmailSender.py new file mode 100644 index 0000000..41116e8 --- /dev/null +++ b/server/modules/EmailSender.py @@ -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.")