-
Notifications
You must be signed in to change notification settings - Fork 0
/
START.py
executable file
·202 lines (173 loc) · 6.36 KB
/
START.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import os
import cv2
import sys
import numpy as np
from flask_basicauth import BasicAuth
from mail import sendEmail
from time import sleep
from datetime import datetime
import time
import traceback
from flask import Flask, session, redirect, url_for, escape, request, render_template, Response, flash, abort
from camera import VideoCamera
from flask_mysqldb import MySQL,MySQLdb
import threading
import RPi.GPIO as GPIO
email_update_interval = 600 # sends an email only once in this time interval
video_camera = VideoCamera(flip=False) # creates a camera object, flip vertically
object_classifier = cv2.CascadeClassifier("models/facial_recognition_model.xml") # an opencv classifier
# App Globals (do not edit)
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'sorveglianza'
mysql = MySQL(app)
last_epoch = 0
#global var
global panServoAngle
global tiltServoAngle
#angle max
panServoAngle = 90
tiltServoAngle = 90
#pin servo
panPin = 27
tiltPin = 17
#computer vision
def check_for_objects():
global last_epoch
while True:
try:
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("Image captured...")
sendEmail(frame)
print("Sending email...")
print("done!")
except:
print(traceback.format_exc())
def gen(camera):
#streamRpiCamera
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
#mainpage
@app.route('/')
def index():
return render_template('login.html') #default LoginPage
@app.route('/video_feed')
def video_feed():
return Response(gen(video_camera),
mimetype='multipart/x-mixed-replace; boundary=frame')
#manage Login
@app.route('/login',methods=["GET","POST"])
def login():
if request.method == 'POST':
email = request.form['username']
password = request.form['password']
curl = mysql.connection.cursor()
curl.execute("SELECT * FROM utente u WHERE u.username=%s and u.psw =%s",(email,password,))
user = curl.fetchone()
if len(user) > 1:
curr = mysql.connection.cursor()
curr.execute("INSERT INTO accessi(FKutente) SELECT u.id FROM utente u WHERE u.username =%s",(email,))
cur = mysql.connection.cursor()
Accessi = cur.execute("SELECT a.dataora,u.nome,u.permessi FROM accessi a, utente u where u.ID=a.fkutente limit 5")
userDetails = cur.fetchall()
currr = mysql.connection.cursor()
USer = currr.execute("SELECT u.nome FROM utente u where u.username=%s",(email,))
Username = currr.fetchone()
Username=''.join(Username)
currrr = mysql.connection.cursor()
ril = currrr.execute("SELECT a.dataora,a.tipologia_rilevamento,a.email FROM allerte a")
rilevamento = currrr.fetchall()
mysql.connection.commit()
templateData = {
'userDetails' : userDetails,
'Username' : Username,
'rilevamento' : rilevamento
}
return render_template("index.html",**templateData)
else:
return index()#render_template("login.html")
else:
return index()#render_template("login.html")
#logout
@app.route('/logout', methods=["GET", "POST"])
def logout():
session.clear()
return index()
@app.route("/intrusion")
def intrusione():
cur = mysql.connection.cursor()
resultValue = cur.execute("SELECT a.dataora,a.tipologia_rilevamento,a.email FROM allerte a")
userDetails = cur.fetchall()
return render_template('intrusion.html',userDetails=userDetails)
@app.route("/motor")
def motori():
return render_template('motor.html')
@app.route('/settings',methods=["GET","POST"])
def impostazioni():
if request.method == 'POST':
Email_destinatario = request.form['Email_destinatario']
Email_mittente = request.form['toemail']
Password_mittente = request.form['toemailpass']
motor = request.form['motor']
cur = mysql.connection.cursor()
cur.execute("update impostazioni set motore = %s, psw = %s, toemail = %s, email =%s", (motor, Password_mittente, Email_destinatario, Email_mittente))
mysql.connection.commit()
return index()
else:
return render_template('settings.html')
@app.route("/streaming1")
def streaming():
return render_template('streaming.html')
@app.route('/register', methods=["GET", "POST"])
def register():
if request.method == 'GET':
return render_template("register.html")
else:
name = request.form['name']
username = request.form['username']
email = request.form['email']
password = request.form['password'].encode('utf-8')
hash_password = bcrypt.hashpw(password, bcrypt.gensalt())
cur = mysql.connection.cursor()
cur.execute("INSERT INTO utente(name, email, password) VALUES (%s,%s,%s)",(name,email,hash_password,))
mysql.connection.commit()
session['name'] = request.form['name']
session['email'] = request.form['email']
return render_template("index.html")
#route servo motion
@app.route("/<servo>/<angolo>")
def move(servo, angolo):
global panServoAngle
global tiltServoAngle
if servo == 'pan':
panServoAngle = int(angolo)
os.system("python3 angleServoCtrl.py " + str(panPin) + " " + str(panServoAngle))
if servo == 'tilt':
tiltServoAngle = int(angolo)
os.system("python3 angleServoCtrl.py " + str(tiltPin) + " " + str(tiltServoAngle))
if servo == 'auto':#TODO --> auto tracking
os.system("sudo modprobe bcm2835-v4l2")
os.system("python3 face_tracker.py") #BETA
templateData = {
'panServoAngle' : panServoAngle,
'tiltServoAngle' : tiltServoAngle
}
return render_template('motor.html', **templateData)
@app.route('/accessi')
def accessi():
cur = mysql.connection.cursor()
resultValue = cur.execute("SELECT a.dataora,u.nome,u.permessi FROM accessi a, utente u where u.ID=a.fkutente")
userDetails = cur.fetchall()
return render_template('tables.html',userDetails=userDetails)
if __name__ == '__main__':
app.secret_key = os.urandom(12) #crypt
t = threading.Thread(target=check_for_objects, args=())
t.daemon = True
t.start()
app.run(host='0.0.0.0', debug=False)