-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
241 lines (206 loc) · 7.41 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import cv2
import crypto
from flask import Flask, Response, render_template, request, redirect, session, url_for, flash, jsonify
from flask_mail import Mail, Message
from werkzeug.utils import secure_filename
from facedetector import faceencodingvalues, predata, detect
import tokens
import db
import os
app = Flask(__name__)
mail= Mail(app)
UPLOAD_FOLDER = 'static/uploads/'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = os.getenv('SECRET_KEY')
dbconnect = db.connect()
global recorded,cap
recorded = "NO"
# =============================================================================================================
@app.route("/")
@app.route("/home")
def home():
if "user" in session:
if "verify" in session:
return render_template("index.html", user = session["user"])
else:
return render_template("verify.html", user = session["user"])
elif "admin" in session:
return redirect(url_for("admin"))
else:
return redirect(url_for("login"))
# =============================================================================================================
@app.route("/login",methods=["GET","POST"])
def login():
if request.method == "GET":
if "user" in session:return redirect(url_for("home"))
return render_template("login.html",RECAPTCHA_SITE_KEY = os.getenv('RECAPTCHA_SITE_KEY'))
if request.method == "POST":
email = request.form.to_dict()["email"]
q = "select username,email from tempusers where email = '{}'".format(email)
result = db.select(q)
if len(result) == 0:
flash("Invalid email")
return redirect(url_for("login"))
elif len(result) == 1:
result = result[0]
username,email = result[0],result[1]
token = tokens.generate_confirmation_token(email)
return redirect(url_for("logincheck",token=token))
# q = "update tempusers set token = '{}' where email = '{}'".format(token,email)
# if db.insert(q):
# if mailing(email,username,token):
# flash("Check your email to for login link")
# return redirect(url_for("login"))
# else:
# flash("Something went wrong during mailing")
# return redirect(url_for("login"))
# else:
# flash("Something went wrong with our database")
# return redirect(url_for("login"))
# return redirect(url_for("login"))
else:
flash("Something went wrong")
return redirect(url_for("login"))
# ============================================================================================================
@app.route("/logincheck/<token>")
def logincheck(token):
email = tokens.confirm_token(token)
if email == "The token has expired":
flash("The token has expired")
return redirect(url_for("login"))
elif email == "the token is invalid":
flash("the token is invalid")
return redirect(url_for("login"))
elif email:
session["user"] = email
return redirect(url_for("home"))
# q = "select username,email from tempusers where email = '{}'".format(email)
# result = db.select(q)
# if len(result) == 1:
# result = result[0]
# username,email = result[0],result[1]
# if token == result[2]:
# q = "update tempusers set token = 'no' where email = '{}'".format(email)
# if db.insert(q):
# session["user"] = email
# return redirect(url_for("home"))
# else:
# flash("Something went wrong with our database")
# return redirect(url_for("login"))
# else:
# flash("use the link that was last sent to your email")
# return redirect(url_for("login"))
# else:
# flash("Something went wrong")
# return redirect(url_for("login"))
else:
flash("Something went wrong")
return redirect(url_for("login"))
# ============================================================================================================
@app.route("/admin",methods=["GET","POST"])
def admin():
if request.method == "GET":
q = "select username,email from tempusers"
details = db.select(q)
if "admin" in session:return render_template("admin/admin.html",user=session["admin"],details = details)
else:return render_template("admin/adminlogin.html")
if request.method == "POST":
values = request.form.to_dict()
mode = values["mode"]
if mode == "login":
email,password = values["email"],values["password"]
if email == "perumallasasank123@gmail.com" and password == "12345":
session["admin"] = email
return redirect(url_for("admin"))
else:
flash("Invalid email or password")
return redirect(url_for("admin"))
if mode == "add":
username = values["username"]
email = values["email"]
q = "select * from tempusers where email = '{}'".format(email)
result = db.select(q)
if len(result) == 0:
q = "select * from tempusers where username = '{}'".format(username)
result = db.select(q)
if len(result) == 0:
file = request.files['file']
if file:
filename = username+os.path.splitext(secure_filename(file.filename))[1]
pathtoimg = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(pathtoimg)
faceencodings,facelocs = faceencodingvalues(pathtoimg)
if len(facelocs)==0:
flash("No face detected")
return redirect(url_for("admin"))
q = "insert into tempusers(username,email,token,encodings) values('{}','{}','{}','{}')".format(username,email,"no",crypto.encryption(str(faceencodings.tolist())))
db.insert(q)
flash("User added")
return redirect(url_for("admin"))
elif len(result) == 1:
flash("Username already exists")
return redirect(url_for("admin"))
else:
flash("Something went wrong")
return redirect(url_for("admin"))
elif len(result) == 1:
flash("Email already exists")
return redirect(url_for("admin"))
else:
flash("Something went wrong")
return redirect(url_for("admin"))
# ============================================================================================================
cap = cv2.VideoCapture()
def gen_frames(email):
global recorded,cap
cap.open(0, cv2.CAP_DSHOW)
# predata(regnosofstudent)
while True:
sucess,img = cap.read()
(frame,ans) = detect(img,email)
recorded = ans
if recorded == "YES":
print("verified*******************************************************************************************************************************")
break
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
print("yield condition exit")
cap.release()
cv2.destroyAllWindows()
@app.route("/video_feed")
def video_feed():
predata(session["user"])
return Response(gen_frames(session["user"]),
mimetype='multipart/x-mixed-replace; boundary=frame')
# ============================================================================================================
@app.route('/recorde')
def recorde():
global recorded
if recorded == "YES":
return jsonify("YES")
else:return jsonify("NO")
@app.route('/recorddone', methods = ['POST', 'GET'])
def recorddone():
global recorded
cap.release()
recorded = "NO"
if request.method == 'GET':
# return session["verify"]
session["verify"] = "True"
print("session verify========================================================")
if "verify" in session:
print("yes")
else:
print("no")
return redirect(url_for("home"))
# ============================================================================================================
@app.route("/logout")
def logout():
session.clear()
return redirect(url_for("login"))
@app.errorhandler(404)
def page_not_found(e):
flash("Page not found")
return redirect("/")
if __name__ == '__main__':
app.run(debug=True,port=8000)