-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.py
261 lines (231 loc) · 9.15 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
import math
from concurrent.futures import ThreadPoolExecutor
from flask import Flask, render_template, request, jsonify, session,redirect,url_for,Response,flash
import os
from flask_mysqldb import MySQL
import json
import io
import numpy as np
from enum import Enum
import warnings
import threading
import utils
import random
import time
import cv2
import keyboard
#variables
studentInfo=None
camera=None
profileName=None
#Flak's Application Confguration
warnings.filterwarnings("ignore")
app = Flask(__name__, template_folder='templates', static_folder='static')
app.secret_key = 'xyz'
# app.config["MONGO_URI"] = "mongodb://localhost:27017/"
os.path.dirname("../templates")
#Flak's Database Configuration
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'examproctordb'
mysql = MySQL(app)
executor = ThreadPoolExecutor(max_workers=4) # Adjust the number of workers as needed
#Function to show face detection's Rectangle in Face Input Page
def capture_by_frames():
global camera
utils.cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while True:
success, frame = utils.cap.read() # read the camera frame
detector=cv2.CascadeClassifier('Haarcascades/haarcascade_frontalface_default.xml')
faces=detector.detectMultiScale(frame,1.2,6)
#Draw the rectangle around each face
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 3)
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')
#Function to run Cheat Detection when we start run the Application
@app.before_request
def start_loop():
task1 = executor.submit(utils.cheat_Detection2)
task2 = executor.submit(utils.cheat_Detection1)
task3 = executor.submit(utils.fr.run_recognition)
task4 = executor.submit(utils.a.record)
#Login Related
@app.route('/')
def main():
return render_template('login.html')
@app.route('/login', methods=['POST'])
def login():
global studentInfo
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM students where Email='" + username + "' and Password='" + password + "'")
data = cur.fetchone()
if data is None:
flash('Your Email or Password is incorrect, try again.', category='error')
return redirect(url_for('main'))
else:
id, name, email,password, role = data
studentInfo={ "Id": id, "Name": name, "Email": email, "Password": password}
if role == 'STUDENT':
utils.Student_Name = name
return redirect(url_for('rules'))
else:
return redirect(url_for('adminStudents'))
@app.route('/logout')
def logout():
return render_template('login.html')
#Student Related
@app.route('/rules')
def rules():
return render_template('ExamRules.html')
@app.route('/faceInput')
def faceInput():
return render_template('ExamFaceInput.html')
@app.route('/video_capture')
def video_capture():
return Response(capture_by_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/saveFaceInput')
def saveFaceInput():
global profileName
if utils.cap.isOpened():
utils.cap.release()
cam = cv2.VideoCapture(0)
success, frame = cam.read() # read the camera frame
profileName=f"{studentInfo['Name']}_{utils.get_resultId():03}" + "Profile.jpg"
cv2.imwrite(profileName,frame)
utils.move_file_to_output_folder(profileName,'Profiles')
cam.release()
return redirect(url_for('confirmFaceInput'))
@app.route('/confirmFaceInput')
def confirmFaceInput():
profile = profileName
utils.fr.encode_faces()
return render_template('ExamConfirmFaceInput.html', profile = profile)
@app.route('/systemCheck')
def systemCheck():
return render_template('ExamSystemCheck.html')
@app.route('/systemCheck', methods=["POST"])
def systemCheckRoute():
if request.method == 'POST':
examData = request.json
output = 'exam'
if 'Not available' in examData['input'].split(';'): output = 'systemCheckError'
return jsonify({"output": output})
@app.route('/systemCheckError')
def systemCheckError():
return render_template('ExamSystemCheckError.html')
@app.route('/exam')
def exam():
utils.cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
keyboard.hook(utils.shortcut_handler)
return render_template('Exam.html')
@app.route('/exam', methods=["POST"])
def examAction():
link = ''
if request.method == 'POST':
examData = request.json
if(examData['input']!=''):
utils.Globalflag= False
utils.cap.release()
utils.write_json({
"Name": ('Prohibited Shorcuts (' + ','.join(list(dict.fromkeys(utils.shorcuts))) + ') are detected.'),
"Time": (str(len(utils.shorcuts)) + " Counts"),
"Duration": '',
"Mark": (1.5 * len(utils.shorcuts)),
"Link": '',
"RId": utils.get_resultId()
})
utils.shorcuts=[]
trustScore= utils.get_TrustScore(utils.get_resultId())
totalMark= math.floor(float(examData['input'])* 6.6667)
if trustScore >=30:
status="Fail(Cheating)"
link = 'showResultFail'
else:
if totalMark < 50:
status="Fail"
link = 'showResultFail'
else:
status="Pass"
link = 'showResultPass'
utils.write_json({
"Id": utils.get_resultId(),
"Name": studentInfo['Name'],
"TotalMark": totalMark,
"TrustScore": max(100-trustScore, 0),
"Status": status,
"Date": time.strftime("%Y-%m-%d", time.localtime(time.time())),
"StId": studentInfo['Id'],
"Link" : profileName
},"result.json")
resultStatus= studentInfo['Name']+';'+str(totalMark)+';'+status+';'+time.strftime("%Y-%m-%d", time.localtime(time.time()))
else:
utils.Globalflag = True
print('sfdsfsdsfdsfdsfdsfdsfdsfdsfds')
resultStatus=''
return jsonify({"output": resultStatus, "link": link})
@app.route('/showResultPass/<result_status>')
def showResultPass(result_status):
return render_template('ExamResultPass.html',result_status=result_status)
@app.route('/showResultFail/<result_status>')
def showResultFail(result_status):
return render_template('ExamResultFail.html',result_status=result_status)
#Admin Related
@app.route('/adminResults')
def adminResults():
results = utils.getResults()
return render_template('Results.html', results=results)
@app.route('/adminResultDetails/<resultId>')
def adminResultDetails(resultId):
result_Details = utils.getResultDetails(resultId)
return render_template('ResultDetails.html', resultDetials=result_Details)
@app.route('/adminResultDetailsVideo/<videoInfo>')
def adminResultDetailsVideo(videoInfo):
return render_template('ResultDetailsVideo.html', videoInfo= videoInfo)
@app.route('/adminStudents')
def adminStudents():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM students where Role='STUDENT'")
data = cur.fetchall()
cur.close()
return render_template('Students.html', students=data)
@app.route('/insertStudent', methods=['POST'])
def insertStudent():
if request.method == "POST":
name = request.form['username']
email = request.form['email']
password = request.form['password']
cur = mysql.connection.cursor()
cur.execute("INSERT INTO students (Name, Email, Password, Role) VALUES (%s, %s, %s, %s)", (name, email, password,'STUDENT'))
mysql.connection.commit()
return redirect(url_for('adminStudents'))
@app.route('/deleteStudent/<string:stdId>', methods=['GET'])
def deleteStudent(stdId):
flash("Record Has Been Deleted Successfully")
cur = mysql.connection.cursor()
cur.execute("DELETE FROM students WHERE ID=%s", (stdId,))
mysql.connection.commit()
return redirect(url_for('adminStudents'))
@app.route('/updateStudent', methods=['POST', 'GET'])
def updateStudent():
if request.method == 'POST':
id_data = request.form['id']
name = request.form['name']
email = request.form['email']
password = request.form['password']
cur = mysql.connection.cursor()
cur.execute("""
UPDATE students
SET Name=%s, Email=%s, Password=%s
WHERE ID=%s
""", (name, email, password, id_data))
mysql.connection.commit()
return redirect(url_for('adminStudents'))
if __name__ == '__main__':
app.run(debug=True)