-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflaskapp.py
More file actions
167 lines (142 loc) · 6.15 KB
/
Copy pathflaskapp.py
File metadata and controls
167 lines (142 loc) · 6.15 KB
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
from flask import Flask, render_template,redirect,request
import pandas as pd
import time
from datetime import datetime
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/view_attendance', methods=['POST'])
def view_attendance():
ts = time.time()
date = datetime.fromtimestamp(ts).strftime("%d-%m-%Y")
message ="Here is a list of student"
df = pd.read_csv("attendanceFolder/attendance_" + date + ".csv")
return render_template('view.html',message=message, df=df.to_html(classes="table table-striped"))
@app.route('/submit_student', methods=['POST'])
def add():
import cv2, pickle, os
import numpy as np
video=cv2.VideoCapture(0)
facedetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
faces_data=[]
i=0
name = request.form['studentName']
ID = request.form['studentID']
Class = request.form['studentClass']
while True:
ret,frame=video.read()
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces=facedetect.detectMultiScale(gray, 1.3 ,5)
for (x,y,w,h) in faces:
crop_img=frame[y:y+h, x:x+w, :]
resized_img=cv2.resize(crop_img, (50,50))
if len(faces_data)<=30 and i%30==0:
faces_data.append(resized_img)
i=i+1
cv2.putText(frame, str(len(faces_data)), (50,50), cv2.FONT_HERSHEY_COMPLEX, 1, (50,50,255), 1)
cv2.rectangle(frame, (x,y), (x+w, y+h), (50,50,255), 1)
cv2.imshow("Frame",frame)
k=cv2.waitKey(1)
if k==ord('q') or len(faces_data)==30:
break
video.release()
cv2.destroyAllWindows()
faces_data=np.asarray(faces_data)
faces_data=faces_data.reshape(30, -1)
if 'names.pkl' not in os.listdir('dataset/'):
names=[name]*30
with open('dataset/names.pkl', 'wb') as f:
pickle.dump(names, f)
else:
with open('dataset/names.pkl', 'rb') as f:
names=pickle.load(f)
names=names+[name]*30
with open('dataset/names.pkl', 'wb') as f:
pickle.dump(names, f)
print("NAME ADDED")
if 'faces_data.pkl' not in os.listdir('dataset/'):
with open('dataset/faces_data.pkl', 'wb') as f:
pickle.dump(faces_data, f)
else:
with open('dataset/faces_data.pkl', 'rb') as f:
faces=pickle.load(f)
faces=np.append(faces, faces_data, axis=0)
with open('dataset/faces_data.pkl', 'wb') as f:
pickle.dump(faces, f)
print("faces added")
return redirect('/')
@app.route('/record', methods=['POST'])
def record():
return render_template('add.html')
@app.route('/take_attendance', methods=['POST'])
def take_attendance():
from sklearn.neighbors import KNeighborsClassifier
import cv2, os, csv, pickle, time
from datetime import datetime
video = cv2.VideoCapture(0)
facedetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
model =KNeighborsClassifier(n_neighbors=10)
columns = ["Student Name", "Arrival Time"]
bgimage = cv2.imread("ashesi.jpg")
with open('dataset/names.pkl','rb') as dnames:
names = pickle.load(dnames)
with open('dataset/faces_data.pkl','rb') as dfaces:
faces = pickle.load(dfaces)
print(faces.shape)
print(len(names))
model.fit(faces,names)
while True:
ret, frame =video.read()
grayscale = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces=facedetect.detectMultiScale(grayscale,1.3,5)
for (x,y,w,h) in faces:
cropped_image = frame[y:y+h, x:x+w,:]
resized_image = cv2.resize(cropped_image, (50,50)).flatten().reshape(1,-1)
output = model.predict(resized_image)
current_time= time.time()
datestamp = datetime.fromtimestamp(current_time).strftime("%d-%m-%Y")
timestamp = datetime.fromtimestamp(current_time).strftime("%H:%M:%S")
existance = os.path.isfile("attendanceFolder/attendance_"+ datestamp + ".csv")
color_outer = (128, 0, 128)
color_inner = (128, 0, 128)
cv2.rectangle(frame, (x, y), (x+w, y+h), color_outer, 1)
cv2.rectangle(frame, (x, y), (x+w, y+h), color_outer, 3)
cv2.rectangle(frame, (x, y-40), (x+w, y), color_inner, -1)
cv2.putText(frame, str(output[0]), (x, y-15), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 255, 255), 1)
cv2.rectangle(frame, (x, y), (x+w, y+h), color_inner, 5)
attendance = [str(output[0]),str(timestamp)]
bgimage[162:162+480,55:55+640]= frame
cv2.imshow("Frame",bgimage)
k = cv2.waitKey(1)
if k==ord("0"):
if existance:
column_index = 0
column_values = []
with open("attendanceFolder/attendance_"+ datestamp+ ".csv", 'r') as csvfile:
reader = csv.reader(csvfile)
next(reader, None)
for row in reader:
if len(row) > column_index:
column_values.append(row[column_index])
with open("attendanceFolder/attendance_"+ datestamp+ ".csv","+a") as attendancefile:
writer = csv.writer(attendancefile)
if str(output[0]) not in column_values:
writer.writerow(attendance)
else:
print("Attendance already taken")
attendancefile.close()
else:
with open("attendanceFolder/attendance_"+ datestamp+ ".csv","+a") as attendancefile:
writer = csv.writer(attendancefile)
writer.writerow(columns)
writer.writerow(attendance)
print("Hurray you are the first in class")
attendancefile.close()
if k == ord('q'):
break
video.release()
cv2.destroyAllWindows()
return redirect('/')
if __name__ == '__main__':
app.run(debug=True)