-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauto_logger_raspi.py
399 lines (304 loc) · 15.5 KB
/
auto_logger_raspi.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 24 19:59:49 2019
@author: spidey, mtc-20, quangnhat185
TODO: [x] add LD_PRELOAD to .bashrc
TODO: [x] fix lcoation of imshow windows
TODO: [ ] reduce false positives of hand recognition
TODO: [x] automate new user creation; need to reload database on successful registration
TODO: [ ] improve model loading by using encoding only
@todo check me
"""
#LD_PRELOAD='/usr/lib/arm-linux-gnueabihf/libatomic.so.1.2.0 python3'
import face_recognition
import cv2
import numpy as np
import os
import glob
from datetime import datetime
import math
import time
from auto_user_reg import *
from auto_user_reg import users as known_face_names
#print("List of existing users: ", users)
#faces=glob.glob("faces/*.jpg")
##print(faces)
##print(faces[0].find('.'))
##print(faces[0][5+1:10])
## This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
## other example, but it includes some basic performance tweaks to make things run a lot faster:
## 1. Process each video frame at 1/4 resolution (though still display it at full resolution)
## 2. Only detect faces in every other frame of video.
#
## PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
## OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
## specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
#
## Get a reference to webcam #0 (the default one)
#known_face_encodings=[]
#known_face_names=[]
print("[INFO] Loading user database...")
print(known_face_names)
#for face in faces:
# image=face_recognition.load_image_file(face)
# encoding=face_recognition.face_encodings(image)[0]
# known_face_encodings.append(encoding)
# slash_ind=face.find('/')
# dot_index=face.find('.')
# known_face_names.append(face[slash_ind+1:dot_index])
#with open('users.txt', 'wb') as f:
# pickle.dump(known_face_names, f)
#
#with open('encodings.txt', 'wb') as f:
# pickle.dump(known_face_encodings, f)
# Assumes names and encodings have already been written to file
#with open('users.txt', 'rb') as f:
# known_face_names = pickle.load(f)
#print(known_face_names)
# Load a sample picture and learn how to recognize it.
'''
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
# Load a second sample picture and learn how to recognize it.
abir_image = face_recognition.load_image_file("abir.jpg")
abir_face_encoding = face_recognition.face_encodings(abir_image)[0]
# Create arrays of known face encodings and their names
known_face_encodings = [
obama_face_encoding,
abir_face_encoding
]
known_face_names = [
"Barack Obama",
"Abir"
]
# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
'''
#cv2.namedWindow('Video', cv2.WINDOW_NORMAL)
#cv2.moveWindow('Video', 40, 40)
while True:
process_this_frame = True
count=0
# workers=['Abir','Quang', 'Thomas', 'Prof Hartanto']
name='dummy'
user_input=input('\n Press u to create new user or \n Press y then [ENTER] to use me: \n')
if user_input.lower() == 'u':
add_new_user()
elif user_input =='y':
video_capture = cv2.VideoCapture(0)
video_capture.set(cv2.cv2.CAP_PROP_FPS, 1)
while True:
# Grab a single frame of video
ret, frame = video_capture.read()
# Resize frame of video to 1/4 size for faster face recognition processing
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_small_frame = small_frame[:, :, ::-1]
# Only process every other frame of video to save time
if process_this_frame:
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
# # If a match was found in known_face_encodings, just use the first one.
# if True in matches:
# first_match_index = matches.index(True)
# name = known_face_names[first_match_index]
# Or instead, use the known face with the smallest distance to the new face
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face_names.append(name)
process_this_frame = not process_this_frame
# Display the results
for (top, right, bottom, left), name in zip(face_locations, face_names):
# Scale back up face locations since the frame we detected in was scaled to 1/4 size
top *= 4
right *= 4
bottom *= 4
left *= 4
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
if name in known_face_names:
count+=1
if count==5:
break
# Display the resulting image
cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
#Putting coming in and going out logic
time.sleep(2)
cap = cv2.VideoCapture(0)
cap.set(cv2.cv2.CAP_PROP_FPS, 24)
entry=''
count_in=0
count_out=0
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
cv2.moveWindow('frame', 30, 40)
while(True):
try:
ret, frame = cap.read()
frame=cv2.flip(frame,1)
kernel = np.ones((3,3),np.uint8)
#define region of interest
roi=frame[100:300, 100:300]
cv2.rectangle(frame,(100,100),(300,300),(0,255,0),0)
hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
# define range of skin color in HSV
lower_skin = np.array([0,20,70], dtype=np.uint8)
upper_skin = np.array([20,255,255], dtype=np.uint8)
#extract skin colur imagw
mask = cv2.inRange(hsv, lower_skin, upper_skin)
#extrapolate the hand to fill dark spots within
mask = cv2.dilate(mask,kernel,iterations = 4)
#blur the image
mask = cv2.GaussianBlur(mask,(5,5),100)
#find contours
contours,hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#find contour of max area(hand)
cnt = max(contours, key = lambda x: cv2.contourArea(x))
#approx the contour a little
epsilon = 0.0005*cv2.arcLength(cnt,True)
approx= cv2.approxPolyDP(cnt,epsilon,True)
#make convex hull around hand
hull = cv2.convexHull(cnt)
#define area of hull and area of hand
areahull = cv2.contourArea(hull)
areacnt = cv2.contourArea(cnt)
#find the percentage of area not covered by hand in convex hull
arearatio=((areahull-areacnt)/areacnt)*100
#find the defects in convex hull with respect to hand
hull = cv2.convexHull(approx, returnPoints=False)
defects = cv2.convexityDefects(approx, hull)
# l = no. of defects
l=0
#code for finding no. of defects due to fingers
for i in range(defects.shape[0]):
s,e,f,d = defects[i,0]
start = tuple(approx[s][0])
end = tuple(approx[e][0])
far = tuple(approx[f][0])
pt= (100,180)
# find length of all sides of triangle
a = math.sqrt((end[0] - start[0])**2 + (end[1] - start[1])**2)
b = math.sqrt((far[0] - start[0])**2 + (far[1] - start[1])**2)
c = math.sqrt((end[0] - far[0])**2 + (end[1] - far[1])**2)
s = (a+b+c)/2
ar = math.sqrt(s*(s-a)*(s-b)*(s-c))
#distance between point and convex hull
d=(2*ar)/a
# apply cosine rule here
angle = math.acos((b**2 + c**2 - a**2)/(2*b*c)) * 57
# ignore angles > 90 and ignore points very close to convex hull(they generally come due to noise)
if angle <= 90 and d>30:
l += 1
cv2.circle(roi, far, 3, [255,0,0], -1)
#draw lines around hand
cv2.line(roi,start, end, [0,255,0], 2)
l+=1
#print corresponding gestures which are in their ranges
font = cv2.FONT_HERSHEY_SIMPLEX
if l==1:
if areacnt<2000:
cv2.putText(frame,'Put rock or best of luck for coming in and any other for going out inside the box',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA)
else:
if arearatio<12:
cv2.putText(frame,'Log in please wait...',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA)
count_in+=1
elif arearatio<17.5:
cv2.putText(frame,'Log in registering...',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA)
count_in+=1
else:
cv2.putText(frame,'Could not recognize, try again',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA)
elif l==2:
cv2.putText(frame,'Could not recognize try again',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA)
elif l==3:
if arearatio<27:
cv2.putText(frame,'Could not recognize try again',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA)
else:
cv2.putText(frame,'Could not recognize try again',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA)
elif l==4:
cv2.putText(frame,'Going out registered',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA)
count_out+=1
elif l==5:
cv2.putText(frame,'Going out registered',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA)
count_out+=1
elif l==6:
cv2.putText(frame,'reposition',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA)
else :
cv2.putText(frame,'reposition',(10,50), font, 2, (0,0,255), 3, cv2.LINE_AA)
if count_in==50:
entry+='in'
break
elif count_out==20:
entry+='out'
break
#show the windows
cv2.imshow('mask',mask)
cv2.imshow('frame',frame)
except:pass
if cv2.waitKey(1) & 0xFF == ord('q'):break
cv2.destroyAllWindows()
cap.release()
# Data Logger
# TODO: should a person be allowed to log-in multiple times???
#
now=datetime.now()
log=os.listdir('logbook/')
month=now.strftime("%B")
year=str(now.year)
day=str(now.day)
hour=str(now.hour)
minute=str(now.minute)
print(log)
if month+' '+year not in log:
os.mkdir('logbook/'+(now.strftime("%B")+' '+year))
day_glob=os.listdir("logbook/"+month+' '+year)
print(day_glob)
file_path="logbook/"+(now.strftime("%B")+' '+year)
file_name=day+'-'+month+'-'+year+'.txt'
full_path=os.path.join(file_path, file_name)
if day+'-'+month+'-'+year+'.txt' not in day_glob: # create new file? Then check out shouldn't happen
f=open(full_path,"a+")
if 'in' in entry:
f.write('[Check In++] Username: '+name+' '+day+'-'+ str(now.month) +'-'+year+' '+ str(now.strftime("%H:%M"))+'\n')
else:
f=open(full_path,"r") #
a=f.read()
#f=open(full_path,"a+")
if 'in' in entry:
if '[Check In++] Username: '+name in a and '[--Check Out] Username: '+name not in a:
print('You are already in. Logging you out now')
g=open(full_path,"a+")
g.write('[--Check Out] Username: '+name+' '+day+'-'+ str(now.month) +'-'+year+' '+str(now.strftime("%H:%M"))+'\n')
g.close()
else:
g=open(full_path,"a+")
g.write('[Check In++] Username: '+name+' '+day+'-'+ str(now.month) +'-'+year+' '+ str(now.strftime("%H:%M"))+'\n')
g.close()
else:
if '[--Check Out] Username: '+name in a:
print('You are already out. Try again with fist bump to come in')
else:
g=open(full_path,"a+")
g.write('[--Check Out] Username: '+name+' '+day+'-'+ str(now.month) +'-'+year+' '+str(now.strftime("%H:%M"))+'\n')
g.close()
f.close()
else:
pass