-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.py
457 lines (421 loc) · 12.4 KB
/
read.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
""" Anusha Nambiar (aan29), Alisha Kochar (ak225)
ECE 5725 Final
Spring 2022
"""
"""
Main code for smart lock
"""
import sys
sys.path.append("/usr/lib/python3/dist-packages")
import picamera
import cv2
import time
import RPi.GPIO as GPIO
from pirc522 import RFID
import pygame
from pygame.locals import *
import os
import subprocess
import io
import numpy
from datetime import datetime
import face_recog as fr
import requests
import take_photos
import shutil
import pickle
# Function to display text on piTFT display.
def disp(my_buttons, font):
screen.fill(BLACK)
for my_text, text_pos in my_buttons.items():
text_surface = font.render(my_text, True, WHITE)
rect = text_surface.get_rect(center=text_pos)
screen.blit(text_surface, rect)
pygame.display.flip()
# Function to display text on piTFT display.
# Takes a list of tuples (not a dict) as an input
def disp_tup(blist, font):
screen.fill(BLACK)
for my_text,text_pos in blist:
text_surface = font.render(my_text, True, WHITE)
rect = text_surface.get_rect(center=text_pos)
screen.blit(text_surface, rect)
pygame.display.flip()
# Physical quit button.
def GPIO13_callback(channel):
quit()
# Physical "RFID tag" button -- UNUSED
def GPIO16_callback(channel):
global tag_recieved
global first
tag_received = True
first = True
# Function to lock door (ie retract linear actuator).
def lock():
p.ChangeDutyCycle(5) # retract
time.sleep(1)
p.ChangeDutyCycle(0) # stop
# Function to unlock door (ie extend linear actuator).
def unlock():
p.ChangeDutyCycle(10) # extend
time.sleep(1)
p.ChangeDutyCycle(0) # stop
# Function to store the user login history
def storeHist(user, match):
x = datetime.now()
data = [x.year%2000, x.month, x.day, x.hour, x.minute, x.second, match]
try:
history = pickle.load(open("histories", "rb"))
except FileNotFoundError:
history = {}
if user not in history:
history[user] = []
if len(history[user]) > 2:
history[user] = history[user][:-1]
history[user] = [data] + history[user]
with open("histories", "wb") as hh:
pickle.dump(history, hh)
# Function to display the user history on screen
def showHist(user):
history = pickle.load(open("histories", "rb"))
mhist = [("Date", (50, 20)), ("Time", (110, 20)), ("Access Granted", (200, 20)), ("Back", (280, 220))]
ypos = 50
for hitem in history[user]:
date = str(hitem[1])+"/"+str(hitem[2])+"/"+str(hitem[0])
time = str(hitem[3])+":"+str(hitem[4])+":"+str(hitem[5])
if (hitem[6]):
grant = "Granted"
else:
grant = "Denied"
mhist.append((date, (50,ypos)))
mhist.append((time, (110,ypos)))
mhist.append((grant, (200,ypos)))
ypos += 30
disp_tup(mhist, my_small_font)
# RFID function to store history
def RFID_store_hist(uid, usr_num, match):
util.set_tag(uid)
x = datetime.now()
data = [x.year%2000, x.month, x.day, x.hour, x.minute, x.second, match]
top = usr_num * 4
util.read_out(top)
middle = rdr.read(top)[1]
oldest = rdr.read(top + 1)[1]
util.rewrite(top+1, middle)
util.rewrite(top+2, oldest)
util.rewrite(top, data)
util.read_out(top)
# RFID function to show history
def RFID_showHist():
hist = [(usr_num * 4), (usr_num * 4) + 1, (usr_num * 4) + 2]
hist1 = rdr.read(hist[0])[1]
date1 = str(hist1[1])+"/"+str(hist1[2])+"/"+str(hist1[0])
time1 = str(hist1[3])+":"+str(hist1[4])+":"+str(hist1[5])
if (hist1[6]):
grants1 = "Granted"
else:
grants1 = "Denied"
hist2 = rdr.read(hist[1])[1]
date2 = str(hist2[1])+"/"+str(hist2[2])+"/"+str(hist2[0])
time2 = str(hist2[3])+":"+str(hist2[4])+":"+str(hist2[5])
if (hist2[6]):
grants2 = "Granted"
else:
grants2 = "Denied"
hist3 = rdr.read(hist[2])[1]
date3 = str(hist3[1])+"/"+str(hist3[2])+"/"+str(hist3[0])
time3 = str(hist3[3])+":"+str(hist3[4])+":"+str(hist3[5])
if (hist3[6]):
grants3 = "Granted"
else:
grants3 = "Denied"
mhist = [("Date", (50, 20)), ("Time", (110, 20)), ("Access Granted", (200, 20)),("Back", (280, 220))]
disp_tup(mhist, my_small_font)
# Function to add a user
def addUser():
add_initial_buttons = {"Scan your card": (160, 120)}
disp(add_initial_buttons, my_font)
pressed = False
while(not pressed):
# add the rfid tag to list of users
# take 20 pictures of them
if not GPIO.input(11):
user = 1
pressed = True
elif not GPIO.input(15):
user = 2
pressed = True
elif not GPIO.input(16):
user = 3
pressed = True
add_picture_buttons = {"Please stand in front of camera": (160, 120)}
disp(add_picture_buttons, my_font)
time.sleep(3)
add_picture_buttons = {"3": (160, 120)}
disp(add_picture_buttons, my_font)
time.sleep(10)
add_picture_buttons = {"3 2": (160, 120)}
disp(add_picture_buttons, my_font)
time.sleep(10)
add_picture_buttons = {"3 2 1": (160, 120)}
disp(add_picture_buttons, my_font)
time.sleep(10)
take_photos.take_pics(user)
add_picture_buttons = {"Please stand by": (160, 120)}
disp(add_picture_buttons, my_font)
try:
fr.get_encodings("user"+str(user))
global users
users = os.listdir("./pictures")
success = "User "+str(user) + " successfully added"
add_picture_buttons = {success: (160, 120), "Back": (280, 220)}
disp(add_picture_buttons, my_font)
except IndexError:
fail = "Unable to add user."
add_fail = {fail: (160, 120), "Back": (280, 220)}
disp(add_fail, my_font)
# Function to remove a user
def remUser():
removing_initial_buttons = {"Please select the user to remove": (160, 120)}
disp(removing_initial_buttons, my_font)
pressed = False
while(not pressed):
if not GPIO.input(11):
user = 1
pressed = True
elif not GPIO.input(15):
user = 2
pressed = True
elif not GPIO.input(16):
user = 3
pressed = True
global users
rem_picture_buttons = {"Please stand by": (160, 120)}
disp(rem_picture_buttons, my_font)
try:
shutil.rmtree("./pictures/user"+ str(user))
os.remove("encodings")
os.remove("names")
users = os.listdir("./pictures")
for usr in users:
fr.get_encodings(usr)
success = "User "+str(user) + " successfully removed"
removing_initial_buttons = {success: (160, 120), "Back": (280, 220)}
disp(removing_initial_buttons, my_font)
history = pickle.load(open("histories", "rb"))
history.pop(user)
with open("histories", "wb") as hh:
pickle.dump(history, hh)
except:
fail = "User "+str(user) + " does not exist"
removing_initial_buttons = {fail: (160, 120), "Back": (280, 220)}
disp(removing_initial_buttons, my_font)
# Setup piTFT & touchscreen
os.putenv("SDL_VIDEODRIVER","fbcon")
os.putenv("SDL_FBDEV", "/dev/fb1")
os.putenv("SDL_MOUSEDRV", "TSLIB")
os.putenv("SDL_MOUSEDEV", "/dev/input/touchscreen")
# Initializing pygame
pygame.init()
WHITE = 255, 255, 255
BLACK = 0,0,0
RED = 255, 0, 0
GREEN = 0, 255, 0
screen = pygame.display.set_mode((320, 240))
my_font = pygame.font.Font(None, 30)
my_small_font = pygame.font.Font(None, 20)
pygame.mouse.set_visible(True)
# Setting up RFID and GPIO pins
rdr = RFID(1, 0, 1000000, 31, 37, 29)
util = rdr.util()
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(13, GPIO.FALLING, callback=GPIO13_callback, bouncetime=300)
# Some associated RFID tags
anusha = [194, 238, 139, 27, 188]
alisha = [51, 8, 135, 33, 157]
uids = {"[194, 238, 139, 27, 188]": 1, "[51, 8, 135, 33, 157]": 2}
# Setting up Linear Actuator
GPIO.setmode(GPIO.BOARD)
GPIO.setup(36, GPIO.OUT)
p = GPIO.PWM(36, 50)
p.start(0)
# Some control variables
running = True
tag_received = False
first = False
match = False
start_buttons = {"Scan your card": (160, 120)}
superusers = [1, 2]
showing_hist = 0
adding_user = 0
removing_user = 0
global users
searching = False
users = os.listdir("./pictures")
# Main
while (running):
# waiting for tag/button press
if (not tag_received):
# User 1 detected
if not GPIO.input(11):
first = True
user = 1
searching = True
if "user"+str(user) in users:
tag_received = True
else:
fail = "User "+str(user) + " doesn't exist"
fail_buttons = {fail: (160, 120)}
disp(fail_buttons, my_font)
time.sleep(2)
# User 2 detected
elif not GPIO.input(15):
first = True
user = 2
searching = True
if "user"+str(user) in users:
tag_received = True
else:
fail = "User "+str(user) + " doesn't exist"
fail_buttons = {fail: (160, 120)}
disp(fail_buttons, my_font)
time.sleep(2)
# User 3 detected
elif not GPIO.input(16):
first = True
user = 3
if "user"+str(user) in users:
tag_received = True
else:
fail = "User "+str(user) + " doesn't exist"
fail_buttons = {fail: (160, 120)}
disp(fail_buttons, my_font)
time.sleep(2)
# Display start menu
disp(start_buttons, my_font)
# If a tag was recieved
if (tag_received and first):
# Display "Verifying User" screen, take photo of user
# Verify user's identity
my_buttons = {"Verifying User "+str(user):(160, 120)}
disp(my_buttons, my_font)
first = False
camera = picamera.PiCamera()
img = camera.capture("compare.jpg")
name = "user" + str(user)
match = fr.test_recog("compare.jpg", name)
camera.close()
storeHist(user, match)
# If photo matches user:
if (match):
# User is a superuser
if (user in superusers):
screen.fill(BLACK)
# Display superuser menu
options = {"Add User": (160, 30), "Remove User": (160, 70), "Lock": (160, 110), "Unlock": (160, 150), "History": (160, 190), "EXIT": (280, 220)}
if ( not showing_hist and not adding_user and not removing_user):
disp(options, my_font)
# Menu button control
for event in pygame.event.get():
if (event.type is MOUSEBUTTONDOWN):
pos = pygame.mouse.get_pos()
elif(event.type is MOUSEBUTTONUP):
pos = pygame.mouse.get_pos()
x,y = pos
if x > 100 and x < 200:
if y > 20 and y < 40:
adding_user = 1
addUser()
elif y > 60 and y < 80:
removing_user = 1
remUser()
elif y > 100 and y < 120:
lock()
elif y > 140 and y < 160:
unlock()
elif y > 180 and y < 200:
showing_hist = 1
showHist(user)
elif x > 250:
if y > 200 and y < 240:
tag_received = 0
match = False
elif showing_hist:
for event in pygame.event.get():
if (event.type is MOUSEBUTTONDOWN):
pos = pygame.mouse.get_pos()
elif(event.type is MOUSEBUTTONUP):
pos = pygame.mouse.get_pos()
x,y = pos
if x > 250:
if y > 200:
showing_hist = False
elif adding_user:
for event in pygame.event.get():
if (event.type is MOUSEBUTTONDOWN):
pos = pygame.mouse.get_pos()
elif(event.type is MOUSEBUTTONUP):
pos = pygame.mouse.get_pos()
x,y = pos
if x > 250:
if y > 200:
adding_user = False
elif removing_user:
for event in pygame.event.get():
if (event.type is MOUSEBUTTONDOWN):
pos = pygame.mouse.get_pos()
elif(event.type is MOUSEBUTTONUP):
pos = pygame.mouse.get_pos()
x,y = pos
if x > 250:
if y > 200:
removing_user = False
# User is not a superuser
else:
# Display regular user menu
screen.fill(BLACK)
options = {"Lock": (160, 60), "Unlock": (160, 120), "History": (160, 180), "EXIT": (280, 220)}
if ( not showing_hist ):
# User menu controls
disp(options, my_font)
for event in pygame.event.get():
if (event.type is MOUSEBUTTONDOWN):
pos = pygame.mouse.get_pos()
elif(event.type is MOUSEBUTTONUP):
pos = pygame.mouse.get_pos()
x,y = pos
if x > 100 and x < 200:
if y < 90:
lock()
elif y > 100 and y < 150:
unlock()
elif y > 160 and y < 210:
showHist(user)
showing_hist = True
elif x > 250:
if y > 200 and y < 240:
tag_received = 0
match = False
elif showing_hist:
for event in pygame.event.get():
if (event.type is MOUSEBUTTONDOWN):
pos = pygame.mouse.get_pos()
elif(event.type is MOUSEBUTTONUP):
pos = pygame.mouse.get_pos()
x,y = pos
if x > 250:
if y > 200:
showing_hist = False
# Identity not verified
elif tag_received and (not match):
# "Call cops," show "acess denied" message, and let the user try again
# after 2 seconds
requests.post("https://maker.ifttt.com/trigger/nonuser_detected/with/key/bZN58Z9OZzwAcmjGQ20Y5C?value1=user1")
message = {"Access denied." : (160, 120)}
disp(message, my_font)
time.sleep(2)
tag_received = 0
match = False