-
Notifications
You must be signed in to change notification settings - Fork 1
/
recognize_video_2.py
285 lines (225 loc) · 10.1 KB
/
recognize_video_2.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
# USAGE
# python recognize_video.py --detector face_detection_model \
# --embedding-model openface_nn4.small2.v1.t7 \
# --recognizer output/recognizer.pickle \
# --le output/le.pickle
from datetime import datetime
from time import gmtime, strftime, localtime
import pandas as pd
# import the necessary packages
from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import pickle
import time
import cv2
import os
import csv
from collections import defaultdict
cleaner = pd.read_csv('attendance-system.csv') #PROSES PERBAIKAN RAW DATA
cleaner.drop(cleaner.index, inplace=True)
cleaner.to_csv('attendance-system.csv', index=False)
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--detector", required=True,
help="path to OpenCV's deep learning face detector")
ap.add_argument("-m", "--embedding-model", required=True,
help="path to OpenCV's deep learning face embedding model")
ap.add_argument("-r", "--recognizer", required=True,
help="path to model trained to recognize faces")
ap.add_argument("-l", "--le", required=True,
help="path to label encoder")
ap.add_argument("-c", "--confidence", type=float, default=0.5,
help="minimum probability to filter weak detections")
args = vars(ap.parse_args())
# load our serialized face detector from disk
print("[INFO] loading face detector...")
protoPath = os.path.sep.join([args["detector"], "deploy.prototxt"])
modelPath = os.path.sep.join([args["detector"],
"res10_300x300_ssd_iter_140000.caffemodel"])
detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath)
# load our serialized face embedding model from disk
print("[INFO] loading face recognizer...")
embedder = cv2.dnn.readNetFromTorch(args["embedding_model"])
# load the actual face recognition model along with the label encoder
recognizer = pickle.loads(open(args["recognizer"], "rb").read())
le = pickle.loads(open(args["le"], "rb").read())
# initialize the video stream, then allow the camera sensor to warm up
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
time.sleep(2.0)
# start the FPS throughput estimator
fps = FPS().start()
name_list=[]
proba_list=[]
proba = 0
count=0
now = datetime.now()
dictionaryin={}
dictionaryout={}
# loop over frames from the video file stream
while True:
# grab the frame from the threaded video stream
frame = vs.read()
# resize the frame to have a width of 600 pixels (while
# maintaining the aspect ratio), and then grab the image
# dimensions
frame = imutils.resize(frame, width=600)
(h, w) = frame.shape[:2]
dt_string = now.strftime("%d/%m/%Y")
hr_string = strftime("%H:%M:%S", localtime())
# construct a blob from the image
imageBlob = cv2.dnn.blobFromImage(
cv2.resize(frame, (300, 300)), 1.0, (300, 300),
(104.0, 177.0, 123.0), swapRB=False, crop=False)
# apply OpenCV's deep learning-based face detector to localize
# faces in the input image
detector.setInput(imageBlob)
detections = detector.forward()
# loop over the detections
for i in range(0, detections.shape[2]):
# extract the confidence (i.e., probability) associated with
# the prediction
confidence = detections[0, 0, i, 2]
# filter out weak detections
if confidence > args["confidence"]:
# compute the (x, y)-coordinates of the bounding box for
# the face
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
# extract the face ROI
face = frame[startY:endY, startX:endX]
(fH, fW) = face.shape[:2]
# ensure the face width and height are sufficiently large
if fW < 20 or fH < 20:
continue
# construct a blob for the face ROI, then pass the blob
# through our face embedding model to obtain the 128-d
# quantification of the face
faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255,
(96, 96), (0, 0, 0), swapRB=True, crop=False)
embedder.setInput(faceBlob)
vec = embedder.forward()
# perform classification to recognize the face
preds = recognizer.predict_proba(vec)[0]
j = np.argmax(preds)
proba = preds[j]
name = le.classes_[j]
# draw the bounding box of the face along with the
# associated probability
text = "{}: {:.2f}%".format(name, proba * 100)
y = startY - 10 if startY - 10 > 10 else startY + 10
cv2.rectangle(frame, (startX, startY), (endX, endY),
(0, 0, 255), 2)
cv2.putText(frame, text, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
cv2.putText(frame, "Sign In Status", (10, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 150, 255), 2)
cv2.putText(frame, "Sign Out Status", (10, 270),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 150, 255), 2)
countitem=0
for item in le.classes_:
coordsy1=50+countitem*30
countitem=countitem+1
if item != 'unknown':
if item in dictionaryin.keys():
cv2.putText(frame,str(item), (10, coordsy1),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)
#os.system('play -nq -t alsa synth {} sine {}'.format(0.1, 500))
else:
cv2.putText(frame,str(item), (10, coordsy1),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 220, 255), 2)
countitem2=0
for item2 in dictionaryin.keys():
coordsy2=300+countitem2*30
countitem2=countitem2+1
if item2 != 'unknown':
if item2 in dictionaryout.keys():
cv2.putText(frame,str(item2), (10, coordsy2),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
#os.system('play -nq -t alsa synth {} sine {}'.format(0.1, 500))
else:
cv2.putText(frame,str(item2), (10, coordsy2),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)
#print(le.classes_)
if proba >=0.70:
name_list.append(name)
proba_list.append(proba)
count=count+1
if count==20:
d = defaultdict(list)
for key, value in zip(name_list, proba_list):
d[key].append(value)
occurence=dict(d)
thisset=set(occurence)
for x in thisset:
occurance_individual=len(occurence[x])
occurence[x]=sum(item for item in occurence[x])
a=sum(occurence.values())
for x in thisset:
occurence[x]=occurence[x]/a
attendance = {word for word, prob in occurence.items() if prob >= 0.3}
#students = max(occurence, key=occurence.get)
students = list(attendance)
headers = ['Date','Name', 'Time Sign In','Time Sign Out']
def write_csv(data):
with open('attendance-system.csv', 'a') as outfile:
outfile.truncate()
file_is_empty = os.stat('attendance-system.csv').st_size == 0
writer = csv.writer(outfile, lineterminator='\n',)
if file_is_empty:
writer.writerow(headers)
writer.writerow(data)
#time.sleep(1)
current_hour = datetime.now().second
fps.stop()
waktu=fps.elapsed()
if waktu >= 0 and waktu <= 15 :
print('Attendance system Open for sign in')
for a in students:
write_csv([dt_string,a,hr_string,''])
records = pd.read_csv('attendance-system.csv') #Records dictionaryin for notification
deduped = records.drop_duplicates(['Name'], keep='first')
deduped =deduped.drop(columns=['Time Sign Out'])
dictionaryin=deduped.set_index('Name').T.to_dict('list')
elif waktu >=30 and waktu <=45:
for a in students:
write_csv([dt_string,a,'',hr_string])
print('Attendance system Open for sign out')
records = pd.read_csv('attendance-system.csv') #Records dictionaryout for notification
signed_out=records.loc[records['Time Sign Out'].notna()]
deduped_out = signed_out.drop_duplicates(['Name'], keep='first')
deduped_out =deduped_out.drop(columns=['Time Sign In'])
dictionaryout=deduped_out.set_index('Name').T.to_dict('list')
else:
print('Attendance system close until Next Course')
print(dt_string,hr_string, students)
name_list.clear()
proba_list.clear()
count=0
# update the FPS counter
fps.update()
# show the output frame
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
# stop the timer and display FPS information
fps.stop()
records = pd.read_csv('attendance-system.csv') #PROSES PERBAIKAN RAW DATA
# deduped = records.drop_duplicates(['Name'], keep='first')
deduped=records.drop(columns=['Time Sign Out'])
signed_out=records.loc[records['Time Sign Out'].notna()]
deduped_out = signed_out.drop_duplicates(['Name'], keep='first')
deduped_out =deduped_out.drop(columns=['Time Sign In'])
mergedStuff = pd.merge(deduped, deduped_out, on=['Name'],suffixes=(' Sign In', ' Sign Out'))
attend_data = mergedStuff[mergedStuff.Name != 'unknown']
attend_data.to_csv('attendance-data.csv', index=False)
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()