-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app.py
214 lines (181 loc) · 6.99 KB
/
flask_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
import cv2
import os
from flask import Flask, request, render_template
from datetime import date
from datetime import datetime
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
import pandas as pd
import joblib
# 定义Flask应用
app = Flask(__name__)
nimgs = 10 # 捕获的图片数量
# 两种不同格式保存日期
datetoday = date.today().strftime("%m_%d_%y")
datetoday2 = date.today().strftime("%d-%B-%Y")
# web_cam
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# 目录不存在则创建
if not os.path.isdir('attendance'):
os.makedirs('attendance')
if not os.path.isdir('static'):
os.makedirs('static')
if not os.path.isdir('static/faces'):
os.makedirs('static/faces')
if f'Attendance-{datetoday}.csv' not in os.listdir('attendance'):
with open(f'attendance/Attendance-{datetoday}.csv', 'w') as f:
f.write('Name,Roll,Time')
# 获取用户总数
def totalreg():
return len(os.listdir('static/faces'))
# 人脸提取
def extract_faces(img):
try:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_points = face_detector.detectMultiScale(gray, 1.2, 5, minSize=(20, 20))
return face_points
except:
return []
# 加载模型人脸识别
def identify_face(facearray):
model = joblib.load('static/face_recognition_model.pkl')
return model.predict(facearray)
# 训练模型
def train_model():
faces = []
labels = []
userlist = os.listdir('static/faces')
for user in userlist:
for imgname in os.listdir(f'static/faces/{user}'):
img = cv2.imread(f'static/faces/{user}/{imgname}')
resized_face = cv2.resize(img, (50, 50))
faces.append(resized_face.ravel())
labels.append(user)
faces = np.array(faces)
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(faces, labels)
joblib.dump(knn, 'static/face_recognition_model.pkl')
# 从当天的考勤文件中提取信息
def extract_attendance():
df = pd.read_csv(f'attendance/Attendance-{datetoday}.csv')
names = df['Name']
rolls = df['Roll']
times = df['Time']
l = len(df)
return names, rolls, times, l
# 添加特定用户的考勤记录
def add_attendance(name):
MIN_TIME_BETWEEN_ATTENDANCE = 60
username = name.split('_')[0]
userid = name.split('_')[1]
current_time = datetime.now()
current_time_str = current_time.strftime("%H:%M:%S")
df = pd.read_csv(f'attendance/Attendance-{datetoday}.csv')
last_attendance = df[df['Roll'] == int(userid)].tail(1)
if last_attendance.empty or (current_time - datetime.strptime(last_attendance['Time'].values[0], "%H:%M:%S")).seconds > MIN_TIME_BETWEEN_ATTENDANCE:
with open(f'attendance/Attendance-{datetoday}.csv', 'a') as f:
f.write(f'\n{username},{userid},{current_time_str}')
# 获取所有用户信息
def getallusers():
userlist = os.listdir('static/faces')
names = []
rolls = []
l = len(userlist)
for i in userlist:
name, roll = i.split('_')
names.append(name)
rolls.append(roll)
return userlist, names, rolls, l
# 删除用户
def deletefolder(duser):
pics = os.listdir(duser)
for i in pics:
os.remove(duser+'/'+i)
os.rmdir(duser)
# Flask路由函数
# 主页面
@app.route('/')
def home():
names, rolls, times, l = extract_attendance()
return render_template('index.html', names=names, rolls=rolls, times=times, l=l, totalreg=totalreg(), datetoday2=datetoday2)
# 列出用户页面
@app.route('/listusers')
def listusers():
userlist, names, rolls, l = getallusers()
return render_template('listusers.html', userlist=userlist, names=names, rolls=rolls, l=l, totalreg=totalreg(), datetoday2=datetoday2)
# 删除用户功能
@app.route('/deleteuser', methods=['GET'])
def deleteuser():
duser = request.args.get('user')
deletefolder('static/faces/'+duser)
# 如果所有面孔都被删除了,则删除训练好的文件
if os.listdir('static/faces/') == []:
os.remove('static/face_recognition_model.pkl')
try:
train_model()
except:
pass
userlist, names, rolls, l = getallusers()
return render_template('listusers.html', userlist=userlist, names=names, rolls=rolls, l=l, totalreg=totalreg(), datetoday2=datetoday2)
# 人脸识别功能 路由
@app.route('/start', methods=['GET'])
def start():
names, rolls, times, l = extract_attendance()
if 'face_recognition_model.pkl' not in os.listdir('static'):
return render_template('index.html', names=names, rolls=rolls, times=times, l=l, totalreg=totalreg(), datetoday2=datetoday2, mess='There is no trained model in the static folder. Please add a new face to continue.')
ret = True
cap = cv2.VideoCapture(0)
while ret:
ret, frame = cap.read()
if len(extract_faces(frame)) > 0:
(x, y, w, h) = extract_faces(frame)[0]
cv2.rectangle(frame, (x, y), (x+w, y+h), (86, 32, 251), 1)
cv2.rectangle(frame, (x, y), (x+w, y-40), (86, 32, 251), -1)
face = cv2.resize(frame[y:y+h, x:x+w], (50, 50))
identified_person = identify_face(face.reshape(1, -1))[0]
add_attendance(identified_person)
cv2.putText(frame, f'{identified_person}', (x+5, y-5),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
cv2.imshow('Attendance System', frame)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()
names, rolls, times, l = extract_attendance()
return render_template('index.html', names=names, rolls=rolls, times=times, l=l, totalreg=totalreg(), datetoday2=datetoday2)
# 添加新用户 路由
@app.route('/add', methods=['GET', 'POST'])
def add():
newusername = request.form['newusername']
newuserid = request.form['newuserid']
userimagefolder = 'static/faces/'+newusername+'_'+str(newuserid)
if not os.path.isdir(userimagefolder):
os.makedirs(userimagefolder)
i, j = 0, 0
cap = cv2.VideoCapture(0)
while 1:
_, frame = cap.read()
faces = extract_faces(frame)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 20), 2)
cv2.putText(frame, f'Images Captured: {i}/{nimgs}', (30, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 20), 2, cv2.LINE_AA)
if j % 5 == 0:
name = newusername+'_'+str(i)+'.jpg'
cv2.imwrite(userimagefolder+'/'+name, frame[y:y+h, x:x+w])
i += 1
j += 1
if j == nimgs*5:
break
cv2.imshow('Adding new User', frame)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()
print('Training Model')
train_model()
names, rolls, times, l = extract_attendance()
return render_template('index.html', names=names, rolls=rolls, times=times, l=l, totalreg=totalreg(), datetoday2=datetoday2)
# flask app
if __name__ == '__main__':
app.run(debug=True, host='127.0.0.1', port=8999)