-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
executable file
·150 lines (122 loc) · 4.46 KB
/
app.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
from flask import Flask, render_template, send_from_directory, session, url_for, redirect, request
from flask_socketio import SocketIO, emit
import base64
from datetime import datetime
import cv2
import numpy as np
import face_recognition
import time
import hashlib
print('[INFO] loading face detection algorithm...')
face_cascade = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
USER_AUTH = False
NONCES = []
app = Flask(__name__, static_url_path='')
app.config['SECRET_KEY'] = 'Do you wanna be my lover ? You have the right to remain silent !'
socketio = SocketIO(app)
@app.route('/main_assets/<path:path>')
def send_main(path):
return send_from_directory('main_assets', path)
@app.route('/team_assets/<path:path>')
def send_team(path):
return send_from_directory('team_assets', path)
@app.route('/about_assets/<path:path>')
def send_about(path):
return send_from_directory('about_assets', path)
@app.route('/login_assets/<path:path>')
def send_login(path):
return send_from_directory('login_assets', path)
@app.route('/')
def index():
"""Home page."""
return render_template('main.html')
@app.route('/about')
def about():
"""About company."""
return render_template('about_us.html')
@app.route('/team')
def team():
"""About team."""
return render_template('team.html')
@app.route('/login')
def login():
"""Login page."""
if 'failure' in session:
session.pop('failure', None)
return render_template('login.html', failure='Login failed !')
else:
return render_template('login.html', failure='')
@app.route('/flagzz')
def flagzz():
"""flagzz page."""
authenticated = False
if request.args.get('nonce') is not None:
for items in NONCES:
if str(hashlib.sha256(items).hexdigest()) == request.args.get('nonce'):
authenticated = True
NONCES.remove(items)
if authenticated:
return render_template('flagz.html')
else:
session['failure'] = True
return redirect(url_for('login'))
else:
session['failure'] = True
return redirect(url_for('login'))
@socketio.on('message')
def handle_message(data):
print('received {}'.format(data))
@socketio.on('authentication')
def authenticate(data):
global USER_AUTH
image = base64.b64decode(data['data'].split(',')[1])
image = np.frombuffer(image, dtype=np.uint8)
image = cv2.imdecode(image, flags=1)
dim = (320, 240)
print('Authenticating ...')
# resize image
image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
#gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw rectangle around the faces
#for (x, y, w, h) in faces:
#d_rect = dlib.rectangle(x, y, w, h)
#shape = sp(image, d_rect)
ceo_image = face_recognition.load_image_file("1.jpg")
person_encoding = face_recognition.face_encodings(image)[0]
ceo_encoding = face_recognition.face_encodings(ceo_image)[0]
results = face_recognition.compare_faces([ceo_encoding], person_encoding)
print(results)
if results[0] == True:
USER_AUTH = True
user_nonce = str(datetime.now()).encode()
NONCES.append(user_nonce)
hash = str(hashlib.sha256(user_nonce).hexdigest())
emit('authentication', {'data': 'yes', 'nonce': hash})
else:
session['failure'] = True
emit('authentication', {'data': 'nope'})
@socketio.on('stream')
def handle_image(data):
# print('received img data' + str(data))
image = base64.b64decode(data['data'].split(',')[1])
image = np.frombuffer(image, dtype=np.uint8)
image = cv2.imdecode(image, flags=1)
dim = (320, 240)
# resize image
image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
image = cv2.imencode('.png', image)
image = base64.b64encode(image[1])
emit('streamback', {'data' : image})
# with open(str(datetime.now()) + '.jpg', 'wb') as f:
# f.write(image)
# print(data)
# print('#'*50)
if __name__ == '__main__':
socketio.run(app, '0.0.0.0', 5000, keyfile='key.pem', certfile='cert.pem')
print('[INFO] app running ..')