-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathapp.py
360 lines (283 loc) · 11.4 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
from flask import Flask, render_template
from flask import request, redirect, url_for, Response, flash
from werkzeug.security import check_password_hash
from werkzeug.utils import secure_filename
import os
import cv2
from datetime import datetime
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
from firebase_admin import storage
from detection.face_matching import detect_faces, align_face
from detection.face_matching import extract_features, match_face
from utils.configuration import load_yaml
config_file_path = load_yaml("configs/database.yaml")
TEACHER_PASSWORD_HASH = config_file_path["teacher"]["password_hash"]
# print(TEACHER_PASSWORD_HASH)
# Initialize Firebase
cred = credentials.Certificate(config_file_path["firebase"]["pathToServiceAccount"])
firebase_admin.initialize_app(
cred,
{
"databaseURL": config_file_path["firebase"]["databaseURL"],
"storageBucket": config_file_path["firebase"]["storageBucket"],
},
)
def upload_database(filename):
"""
Checks if a file with the given filename already exists in the
database storage, and if not, uploads the file to the database.
"""
valid = False
# If the fileName exists in the database storage, then continue
if storage.bucket().get_blob(filename):
valid = True
error = f"<h1>{filename} already exists in the database</h1>"
# First check if the name of the file is a number
if not filename[:-4].isdigit():
valid = True
error = f"<h1>Please make sure that the name of the {filename} is a number</h1>"
if not valid:
# Image to database
filename = os.path.join(app.config["UPLOAD_FOLDER"], filename)
bucket = storage.bucket()
blob = bucket.blob(filename)
blob.upload_from_filename(filename)
error = None
return valid, error
def match_with_database(img, database):
'''The function "match_with_database" takes an image and a database as input, detects faces in the
image, aligns and extracts features from each face, and matches the face to a face in the database.
'''
global match
# Detect faces in the frame
faces = detect_faces(img)
# Draw the rectangle around each face
for x, y, w, h in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 4)
# save the image
cv2.imwrite("static/recognized/recognized.png", img)
for face in faces:
try:
# Align the face
aligned_face = align_face(img, face)
# Extract features from the face
embedding = extract_features(aligned_face)
embedding = embedding[0]["embedding"]
# Match the face to a face in the database
match = match_face(embedding, database)
if match is not None:
return f"Match found: {match}"
else:
return "No match found"
except:
return "No face detected"
# break # TODO: remove this line to detect all faces in the frame
app = Flask(__name__, template_folder="template")
app.secret_key = "123456" # Add this line
# Specify the directory to save uploaded images
UPLOAD_FOLDER = "static/images"
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
@app.route("/")
def home():
return render_template("home.html")
@app.route("/add_info")
def add_info():
return render_template("add_info.html")
@app.route("/teacher_login", methods=["GET", "POST"])
def teacher_login():
if request.method == "POST":
password = request.form.get("password")
if check_password_hash(TEACHER_PASSWORD_HASH, password):
return redirect(url_for("attendance"))
else:
flash("Incorrect password")
return render_template("teacher_login.html")
@app.route("/attendance")
def attendance():
ref = db.reference("Students")
number_student = len(ref.get())
# attandence
students = {}
for i in range(1, number_student):
studentInfo = db.reference(f"Students/{i}").get()
students[i] = [
studentInfo["name"],
studentInfo["email"],
studentInfo["userType"],
studentInfo["classes"],
]
return render_template("attendance.html", students=students)
@app.route("/upload", methods=["POST"])
def upload():
global filename
# Check if a file was uploaded
if "file" not in request.files:
return "No file uploaded", 400
file = request.files["file"]
# Check if the file is one of the allowed types/extensions
if file.filename == "":
return "No selected file", 400
if file and allowed_file(file.filename):
# Make the filename safe, remove unsupported chars
filename = secure_filename(file.filename)
# change the name of the file to the studentId
# Information to database
ref = db.reference("Students")
try:
# Obtain the last studentId number from the database
studentId = len(ref.get())
except TypeError:
studentId = 1
filename = f"{studentId}.png"
# Move the file from the temporal folder to
# the upload folder we setup
file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
# Upload the file to the database
val, err = upload_database(filename)
if val:
return err
# Redirect the user to the uploaded_file route, which
# will basically show on the browser the uploaded file
return redirect(url_for("add_info"))
return "File upload failed", 400
def allowed_file(filename):
# Put your allowed file types here
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg", "gif"}
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/uploads/<filename>")
def uploaded_file(filename):
timestamp = datetime.now().strftime("%Y%m%d%H%M%S") # for browser cache
# Generate the URL of the image
url = url_for("static", filename="images/" + filename, v=timestamp)
# Return an HTML string that includes an <img> tag
return f'<h1>File uploaded successfully</h1><img src="{url}" alt="Uploaded image">'
@app.route("/video_feed")
def video_feed():
return Response(gen_frames(), mimetype="multipart/x-mixed-replace; boundary=frame")
@app.route("/capture", methods=["POST"])
def capture():
global filename
ret, frame = video.read()
if ret:
# Information to database
ref = db.reference("Students")
try:
# Obtain the last studentId number from the database
studentId = len(ref.get())
except TypeError:
studentId = 1
# Save the image
filename = f"{studentId}.png"
# Save the frame as an image
cv2.imwrite(os.path.join(app.config["UPLOAD_FOLDER"], filename), frame)
# Upload the file to the database
val, err = upload_database(filename)
if val:
return err
# Redirect to the success page
return redirect(url_for("add_info"))
@app.route("/success/<filename>")
def success(filename):
timestamp = datetime.now().strftime("%Y%m%d%H%M%S") # for browser cache
# Generate the URL of the image
url = url_for("static", filename="images/" + filename, v=timestamp)
# Return an HTML string that includes an <img> tag
return f'<h1>{filename} image uploaded successfully to the database</h1><img src="{url}" alt="Uploaded image">'
@app.route("/submit_info", methods=["POST"])
def submit_info():
# Get the form data
name = request.form.get("name")
email = request.form.get("email")
userType = request.form.get("userType")
classes = request.form.getlist("classes") # Get all selected classes
password = request.form.get("password")
# Get the last uploaded image
studentId, _ = os.path.splitext(filename)
fileName = os.path.join(app.config["UPLOAD_FOLDER"], filename)
data = cv2.imread(fileName)
# Detect faces in the image
faces = detect_faces(data)
for face in faces:
# Align the face
aligned_face = align_face(data, face)
# Extract features from the face
embedding = extract_features(aligned_face)
break
# Add the information to the database
ref = db.reference("Students")
data = {
str(studentId): {
"name": name,
"email": email,
"userType": userType,
"classes": {class_: int("0") for class_ in classes},
"password": password,
"embeddings": embedding[0]["embedding"],
}
}
for key, value in data.items():
ref.child(key).set(value)
return redirect(url_for("success", filename=filename))
@app.route("/recognize", methods=["GET", "POST"])
def recognize():
global detection
ret, frame = video.read()
if ret:
# Information to database
ref = db.reference("Students")
# Obtain the last studentId number from the database
number_student = len(ref.get())
print("There are", (number_student - 1), "students in the database")
database = {}
for i in range(1, number_student):
studentInfo = db.reference(f"Students/{i}").get()
studentName = studentInfo["name"]
studentEmbedding = studentInfo["embeddings"]
database[studentName] = studentEmbedding
detection = match_with_database(frame, database)
# Return a successful response
return redirect(url_for("select_class"))
@app.route("/select_class", methods=["GET", "POST"])
def select_class():
if request.method == "POST":
# Get the selected class from the form data
selected_class = request.form.get("classes")
# Generate the URL of the image
timestamp = datetime.now().strftime("%Y%m%d%H%M%S") # for browser cache
url = url_for("static", filename="recognized/recognized.png", v=timestamp)
# Information to database
ref = db.reference("Students")
# Obtain the last studentId number from the database
number_student = len(ref.get())
for i in range(1, number_student):
studentInfo = db.reference(f"Students/{i}").get()
if match == studentInfo["name"]:
# Check if the selceted class is in the list of studentInfo['classes']
print(studentInfo["classes"])
if selected_class in studentInfo["classes"]:
# Update the attendance in the database
ref.child(f"{i}/classes/{selected_class}").set(
int(studentInfo.get("classes", {}).get(selected_class)) + 1
)
# Render the template, passing the detection result and image URL
return f'<h2>Selected Class: {selected_class} - {detection}</h2><img src="{url}" alt="Recognized face">'
else:
return f'<h2>Student not in class - {detection}</h2><img src="{url}" alt="Recognized face">'
else:
# Render the select class page
return render_template("select_class.html")
def gen_frames():
global video
video = cv2.VideoCapture(0)
while True:
success, frame = video.read()
if not success:
break
else:
ret, buffer = cv2.imencode(".jpg", frame)
frame = buffer.tobytes()
yield (b"--frame\r\n" b"Content-Type: image/jpeg\r\n\r\n" + frame + b"\r\n")
if __name__ == "__main__":
app.run(debug=True)