Code Appendix

  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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# import libraries and packages for facial recognition and video
from imutils.video import VideoStream
from imutils.video import FPS
import face_recognition
import imutils
from imutils import paths
import pickle
import cv2
from picamera import PiCamera
from picamera.array import PiRGBArray

# import math libraries
import numpy as np
import math

# imports for pygame and display
import pygame
from pygame.locals import *
import pigame

# import for Raspberry Pi
import RPi.GPIO as GPIO
import os
import time
import sys
import subprocess

#==================================================================================================
# GPIOs
#==================================================================================================
# set GPIO numbering to Broadcom
GPIO.setmode(GPIO.BCM)

# setup GPIO for servos
GPIO.setup(26, GPIO.OUT)
GPIO.output(26, GPIO.HIGH)
pwm26=GPIO.PWM(26, 50)
pwm26.start(0)

GPIO.setup(19, GPIO.OUT)
GPIO.output(19, GPIO.HIGH)
pwm19=GPIO.PWM(19, 50)
pwm19.start(0)

# return to start
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
def GPIO17_callback(channel):   
	global screen
	screen = 1
GPIO.add_event_detect(17, GPIO.FALLING, callback=GPIO17_callback, bouncetime=300)

# submit directory name
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
def GPIO22_callback(channel):   
	global screen
	global pic_ins
	if (screen == 6):                   
		try:
			path = "/home/pi/ece5725-finalproject/final_demo/dataset/" + str(dir_name[0])
			os.mkdir(path)
		except: 
			pass
		screen = 7
		pic_ins = True
GPIO.add_event_detect(22, GPIO.FALLING, callback=GPIO22_callback, bouncetime=300)

# take photos
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
def GPIO23_callback(channel):   
	global take_pic
	if (screen == 7):                   
		take_pic = True
GPIO.add_event_detect(23, GPIO.FALLING, callback=GPIO23_callback, bouncetime=300)

# quit button
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def GPIO27_callback(channel):  
	global code_run
	code_run = False
GPIO.add_event_detect(27, GPIO.FALLING, callback=GPIO27_callback, bouncetime=300)

#==================================================================================================
# PiTFT/PyGame Initializations
#==================================================================================================
# set up environment variables for piTFT_display
os.putenv('SDL_VIDEODRIVER','fbcon')
os.putenv('SDL_FBDEV','/dev/fb0')

# Environment variables for touchscreen
os.putenv('SDL_MOUSEDRV','dummy')
os.putenv('SDL_MOUSEDEV','/dev/null')
os.putenv('DISPLAY','')

# colors
WHITE = (255,255,255)
BLACK = (0,0,0)
GREEN = (0,255,0)
TEAL = (174,253,255)

# initialize screen
pygame.init()
pitft = pigame.PiTft()
width = 320
height = 240
lcd = pygame.display.set_mode((width, height))
lcd.fill(BLACK)

# Mouse display
pygame.mouse.set_visible(True) 

# font sizes
font_small = pygame.font.Font(None, 25)
font_mid = pygame.font.Font(None, 30)
font_big = pygame.font.Font(None, 50)

# general circle
circenter = (160,120)
global x, y
x=0
y=0

#==================================================================================================
# PyGame State GUIs
#==================================================================================================
# 1: Init
start_surface = font_small.render('Click Button to Start', True, WHITE)
start_rect = start_surface.get_rect(center=(160,30))

# GREEN 'START' circle
start_circolor = GREEN
start_cirtext = 'START'
start_cir_surface = font_mid.render(start_cirtext, True, BLACK)
start_cir_rect = start_cir_surface.get_rect(center=circenter)

# 2: Smile
smile_surface = font_mid.render('Smile for the Camera!', True, WHITE)
smile_rect = smile_surface.get_rect(center=(160,120))

# 3: Dispensing Snack
dispense_surface = font_mid.render('Dispensing Snack', True, WHITE)
dispense_rect = dispense_surface.get_rect(center=(160,120))

open_surface = font_mid.render('Open Drawer Below', True, WHITE)
open_rect = open_surface.get_rect(center=(160,120))

thx_surface = font_mid.render('Thanks for stopping by!', True, WHITE)
thx_rect = thx_surface.get_rect(center=(160,120))

# 4: Unrecognized Select to Add to Library
add_face_surface = font_small.render('Click Button to be Recognized', True, WHITE)
add_face_rect = add_face_surface.get_rect(center=(160,30))

# TEAL 'Add Face' circle
add_face_circolor = TEAL
add_face_cirtext = 'Add Face'
add_face_cir_surface = font_mid.render(add_face_cirtext, True, BLACK)
add_face_cir_rect = add_face_cir_surface.get_rect(center=circenter)

# 5: Passcode
code_surface = font_small.render('Type Passcode', True, WHITE)
code_rect = code_surface.get_rect(center=(160,30))

attempt_code = 0	# 5 attempts to enter passcode
fail_code1_surface = font_mid.render('Unfortunately,', True, WHITE)
fail_code1_rect = fail_code1_surface.get_rect(center=(160,90))
fail_code2_surface = font_mid.render('you do not have access', True, WHITE)
fail_code2_rect = fail_code2_surface.get_rect(center=(160,120))
fail_code3_surface = font_mid.render(':(', True, WHITE)
fail_code3_rect = fail_code3_surface.get_rect(center=(160,150))

# 6: Type Directory Name Keyboard
dir_name = ['']
file_done = False
add_dir1_surface = font_small.render('Type FirstName_LastInitial', True, WHITE)
add_dir1_rect = add_dir1_surface.get_rect(center=(160,20))
add_dir2_surface = font_small.render('Click "Submit Name" Button', True, WHITE)
add_dir2_rect = add_dir2_surface.get_rect(center=(160,40))

# 7: Take Pics to Add to Library
img_counter = 0
take_pic1_surface = font_small.render('Take 25 pics at many angles ~1\' away', True, WHITE)
take_pic1_rect = take_pic1_surface.get_rect(center=(160,110))
take_pic2_surface = font_small.render('Click "Take Pic" Button', True, WHITE)
take_pic2_rect = take_pic2_surface.get_rect(center=(160,140))

# 8: Training
train_surface = font_mid.render('Training...', True, WHITE)
train_rect = train_surface.get_rect(center=(160,110))

train1_surface = font_small.render('This can take up to 10 minutes.', True, WHITE)
train1_rect = train1_surface.get_rect(center=(160,140))

train_done = False
train_done_surface = font_mid.render('Training Complete', True, WHITE)
train_done_rect = train_done_surface.get_rect(center=(160,120))

#==================================================================================================
# Facial Recognition Functions
#==================================================================================================
# function for PiCam facial recognition
def facial_recognition():	
	global currentname
	global dispense
	global screen 
	global frame
	global face

	### FACIAL RECOGNITION BOXING ###
	boxes = face_recognition.face_locations(frame)
	# compute the facial embeddings for each face bounding box
	encodings = face_recognition.face_encodings(frame, boxes)
	names = []
	# loop over the facial embeddings
	for encoding in encodings:
		# attempt to match each face in the input image to our known encodings
		matches = face_recognition.compare_faces(data["encodings"],
			encoding)
		name = "Unknown" #if face is not recognized, then print Unknown

		# check to see if we have found a match
		if True in matches:
			print("looking for match")
			# find the indexes of all matched faces then initialize a
			# dictionary to count the total number of times each face
			# was matched
			matchedIdxs = [i for (i, b) in enumerate(matches) if b]
			counts = {}

			# loop over the matched indexes and maintain a count for
			# each recognized face face
			for i in matchedIdxs:
				name = data["names"][i]
				counts[name] = counts.get(name, 0) + 1

			# determine the recognized face with the largest number
			# of votes (note: in the event of an unlikely tie Python
			# will select first entry in the dictionary)
			name = max(counts, key=counts.get)

			#If someone in your dataset is identified, print their name on the screen
			if currentname != name:
				print("matched!")
				currentname = name
				dispense = 1 # change to 1
				print("current name: " + str(currentname))
				
		# update the list of names
		names.append(name)

		# change to screen 2 when see a face
		if screen == 2:
			face = True

	# loop over the recognized faces
	for ((top, right, bottom, left), name) in zip(boxes, names):
		# draw the predicted face name on the image - color is in BGR
		cv2.rectangle(frame, (left, top), (right, bottom),
			(0, 255, 225), 2)
		y = top - 15 if top - 15 > 15 else top + 15
		cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,
			.8, (0, 255, 255), 2)
	
	frame = np.fliplr(frame)
	frame = np.rot90(frame)
	frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
	surf = pygame.surfarray.make_surface(frame)
	lcd.fill(BLACK)
	lcd.blit(surf,(0,0))
	pygame.display.update()
	pitft.update()

	# display camera stream at least for 1 sec before transitioning states
	if face:
		time.sleep(1)
		face = False
		screen = 3

# function to add face to library
def add_to_library():
	global take_pic
	global img_name
	global img_counter
	global screen 

	take_pic = False
	img_name = "dataset/"+ dir_name[0] +"/image_{}.jpg".format(img_counter)
	cv2.imwrite(img_name, frame)
	print("{} written!".format(img_name))
	img_counter += 1
	if img_counter == 25 :
		img_counter = 0
		screen = 8

# function to re-train facial recognition model
def train_model():
	global train_done

	# our images are located in the dataset folder
	print("[INFO] start processing faces...")
	imagePaths = list(paths.list_images("/home/pi/ece5725-finalproject/final_demo/dataset"))

	# initialize the list of known encodings and known names
	knownEncodings = []
	knownNames = []

	# loop over the image paths
	for (i, imagePath) in enumerate(imagePaths):
		# extract the person name from the image path
		print("[INFO] processing image {}/{}".format(i + 1,
			len(imagePaths)))
		name = imagePath.split(os.path.sep)[-2]

		# load the input image and convert it from RGB (OpenCV ordering)
		# to dlib ordering (RGB)
		image = cv2.imread(imagePath)
		rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

		# detect the (x, y)-coordinates of the bounding boxes
		# corresponding to each face in the input image
		boxes = face_recognition.face_locations(rgb,
			model="hog")

		# compute the facial embedding for the face
		encodings = face_recognition.face_encodings(rgb, boxes)

		# loop over the encodings
		for encoding in encodings:
			# add each encoding + name to our set of known names and
			# encodings
			knownEncodings.append(encoding)
			knownNames.append(name)

	# dump the facial encodings + names to disk
	print("[INFO] serializing encodings...")
	data = {"encodings": knownEncodings, "names": knownNames}
	f = open("/home/pi/ece5725-finalproject/final_demo/encodings.pickle", "wb")
	f.write(pickle.dumps(data))
	f.close()
	train_done = True

#==================================================================================================
# Servo Functions
#==================================================================================================
# function to dispense snack
def dispense_snack():
	pwm26.ChangeDutyCycle(8)  # CW
	time.sleep(0.6)
	pwm26.ChangeDutyCycle(0)  # STOP
	time.sleep(0.6)
	pwm26.ChangeDutyCycle(1) # CCW
	time.sleep(0.6)
	pwm26.ChangeDutyCycle(0)  # STOP

# function to dispense marble
def dispense_marble():
	pwm19.ChangeDutyCycle(8)  # CW
	time.sleep(0.5)
	pwm19.ChangeDutyCycle(0)  # STOP
	time.sleep(0.5)
	pwm19.ChangeDutyCycle(1) # CCW
	time.sleep(0.5)
	pwm19.ChangeDutyCycle(0)  # STOP

#==================================================================================================
# Passcode/Keyboard Functions
#==================================================================================================
# function to draw dashed line for passcode
def draw_dash():
    pygame.draw.line(lcd, WHITE, (125, 98), (141,98), 3)
    pygame.draw.line(lcd, WHITE, (144, 98), (160,98), 3)
    pygame.draw.line(lcd, WHITE, (163, 98), (179,98), 3)
    pygame.draw.line(lcd, WHITE, (182, 98), (198,98), 3)

# number button parameters
num_dim = (62, 40)
gap = 1
one_pos = (3,156)
six_pos = (3,198)
num_recs = {
    '1' : pygame.Rect(one_pos                                          , num_dim),
    '2' : pygame.Rect((one_pos[0] + (num_dim[0] + gap)    , one_pos[1]), num_dim),
    '3' : pygame.Rect((one_pos[0] + (num_dim[0] + gap) * 2, one_pos[1]), num_dim),
    '4' : pygame.Rect((one_pos[0] + (num_dim[0] + gap) * 3, one_pos[1]), num_dim),
    '5' : pygame.Rect((one_pos[0] + (num_dim[0] + gap) * 4, one_pos[1]), num_dim),
    '6' : pygame.Rect(six_pos                                          , num_dim),
    '7' : pygame.Rect((six_pos[0] + (num_dim[0] + gap)    , six_pos[1]), num_dim),
    '8' : pygame.Rect((six_pos[0] + (num_dim[0] + gap) * 2, six_pos[1]), num_dim),
    '9' : pygame.Rect((six_pos[0] + (num_dim[0] + gap) * 3, six_pos[1]), num_dim), 
    '0' : pygame.Rect((six_pos[0] + (num_dim[0] + gap) * 4, six_pos[1]), num_dim)
}
# function to draw number buttons
def draw_num_button():
    for key, rectangle in num_recs.items():
        pygame.draw.rect(lcd, WHITE, rectangle)
        text_surface = font_mid.render(key, True, BLACK)
        lcd.blit(text_surface, (rectangle[0]+25, rectangle[1]+10))

# passcode parameters
code_texts = ['']
code_texts_position = (125, 65)
code_cursor_position = (125, 65)
code_cursor_position = ( len( code_texts[len( code_texts ) - 1] ), len( code_texts ) - 1 )
# function to draw passcode
def draw_num_text():
	for text in code_texts:
		code_text_surface = font_big.render( text, True, WHITE )
		lcd.blit( code_text_surface, code_text_surface.get_rect(topleft = code_texts_position) )

# function to check if passcode is correct
def check_passcode():
	global attempt_code
	global screen
	
	if len(code_texts[code_cursor_position[1]]) >= 4:
		lcd.fill(BLACK)
		draw_num_text()
		draw_num_button()
		draw_dash()
		pygame.display.flip()
		pitft.update()
		if code_texts[code_cursor_position[1]] == '5725':
			attempt_code = 0
			screen = 6
		elif attempt_code == 4:
			time.sleep(1)
			attempt_code = 0

			# update piTFT with failed message before returning to init screen
			lcd.fill(BLACK) # erase workspace
			lcd.blit(fail_code1_surface, fail_code1_rect)
			lcd.blit(fail_code2_surface, fail_code2_rect)
			lcd.blit(fail_code3_surface, fail_code3_rect)
			pygame.display.update()
			pitft.update()
			time.sleep(2)

			screen = 1
		else:
			attempt_code += 1

# keyboard button parameters - horizontal
q_origin = ( 1, 146 )
a_origin = ( 16, 169 )
z_origin = ( 32, 192 )
space_origin = ( 119, 215 )
key_dimensions = ( 30, 21 )
space_dimensions = ( 50, 21 )
width_gap = 2
rectangles = {
    'q' : pygame.Rect( q_origin, key_dimensions ),
    'w' : pygame.Rect( ( q_origin[0] + ( key_dimensions[0] + width_gap ) * 1, q_origin[1] ), key_dimensions ),
    'e' : pygame.Rect( ( q_origin[0] + ( key_dimensions[0] + width_gap ) * 2, q_origin[1] ), key_dimensions ),
    'r' : pygame.Rect( ( q_origin[0] + ( key_dimensions[0] + width_gap ) * 3, q_origin[1] ), key_dimensions ),
    't' : pygame.Rect( ( q_origin[0] + ( key_dimensions[0] + width_gap ) * 4, q_origin[1] ), key_dimensions ),
    'y' : pygame.Rect( ( q_origin[0] + ( key_dimensions[0] + width_gap ) * 5, q_origin[1] ), key_dimensions ),
    'u' : pygame.Rect( ( q_origin[0] + ( key_dimensions[0] + width_gap ) * 6, q_origin[1] ), key_dimensions ),
    'i' : pygame.Rect( ( q_origin[0] + ( key_dimensions[0] + width_gap ) * 7, q_origin[1] ), key_dimensions ),
    'o' : pygame.Rect( ( q_origin[0] + ( key_dimensions[0] + width_gap ) * 8, q_origin[1] ), key_dimensions ),
    'p' : pygame.Rect( ( q_origin[0] + ( key_dimensions[0] + width_gap ) * 9, q_origin[1] ), key_dimensions ), 

    'a' : pygame.Rect( ( a_origin, key_dimensions ) ),
    's' : pygame.Rect( ( a_origin[0] + ( key_dimensions[0] + width_gap ) * 1, a_origin[1] ), key_dimensions ),
    'd' : pygame.Rect( ( a_origin[0] + ( key_dimensions[0] + width_gap ) * 2, a_origin[1] ), key_dimensions ),
    'f' : pygame.Rect( ( a_origin[0] + ( key_dimensions[0] + width_gap ) * 3, a_origin[1] ), key_dimensions ),
    'g' : pygame.Rect( ( a_origin[0] + ( key_dimensions[0] + width_gap ) * 4, a_origin[1] ), key_dimensions ),
    'h' : pygame.Rect( ( a_origin[0] + ( key_dimensions[0] + width_gap ) * 5, a_origin[1] ), key_dimensions ),
    'j' : pygame.Rect( ( a_origin[0] + ( key_dimensions[0] + width_gap ) * 6, a_origin[1] ), key_dimensions ),
    'k' : pygame.Rect( ( a_origin[0] + ( key_dimensions[0] + width_gap ) * 7, a_origin[1] ), key_dimensions ),
    'l' : pygame.Rect( ( a_origin[0] + ( key_dimensions[0] + width_gap ) * 8, a_origin[1] ), key_dimensions ),

    'z' : pygame.Rect( z_origin, key_dimensions ),
    'x' : pygame.Rect( ( z_origin[0] + ( key_dimensions[0] + width_gap ) * 1, z_origin[1] ), key_dimensions ),
    'c' : pygame.Rect( ( z_origin[0] + ( key_dimensions[0] + width_gap ) * 2, z_origin[1] ), key_dimensions ),
    'v' : pygame.Rect( ( z_origin[0] + ( key_dimensions[0] + width_gap ) * 3, z_origin[1] ), key_dimensions ),
    'b' : pygame.Rect( ( z_origin[0] + ( key_dimensions[0] + width_gap ) * 4, z_origin[1] ), key_dimensions ),
    'n' : pygame.Rect( ( z_origin[0] + ( key_dimensions[0] + width_gap ) * 5, z_origin[1] ), key_dimensions ),
    'm' : pygame.Rect( ( z_origin[0] + ( key_dimensions[0] + width_gap ) * 6, z_origin[1] ), key_dimensions ),
    '←' : pygame.Rect( ( z_origin[0] + ( key_dimensions[0] + width_gap ) * 7, z_origin[1] ), key_dimensions ),

    'Space' : pygame.Rect( space_origin, space_dimensions ),
    '_' : pygame.Rect( ( space_origin[0] + ( space_dimensions[0] + width_gap ) * 1, space_origin[1] ), key_dimensions )
}

# function to draw keyboard buttons
def draw_keyboard():
    # Loop through the rectangle directory
    for key, rectangle in rectangles.items():
        # Draw the rectangle to the screen
        pygame.draw.rect( lcd, WHITE, rectangle )
        # Draw the keyboard text
        if key == 'Space':
            key_text_surface = font_mid.render( '', True, BLACK ) # WHITE
        else:
            key_text_surface = font_mid.render( key, True, BLACK ) # WHITE

        lcd.blit(key_text_surface, (rectangle[0]+8, rectangle[1]))

# typed name parameters
key_texts_position = ( 160, 90 )
key_cursor_position = ( 0, 0 )
# function to draw typed name
def draw_text():
    # Keep track of the horizontal offset for the sake of new lines and text wrapping
    for text in dir_name:
        key_text_surface = font_big.render( text, True, WHITE )
        lcd.blit( key_text_surface, key_text_surface.get_rect(center = key_texts_position) )

#==================================================================================================
# Start up
#==================================================================================================
# time-out
time_limit = 3600
start_time= time.time()

# initialize flags
code_run = True
pic_ins = False
take_pic = False
face = False
dispense = 0	# 0=marble, 1=snack

# initialize screen state
screen = 1

# Facial Recognition Initialization
currentname = "unknown"
# Determine faces from encodings.pickle file model created from train_model.py
encodingsP = "/home/pi/ece5725-finalproject/final_demo/encodings.pickle"
# load the known faces and embeddings along with OpenCV's Haarcascade for face detection
print("[INFO] loading encodings + face detector...")
data = pickle.loads(open(encodingsP, "rb").read())

# initialize the video stream and allow the camera sensor to warm up
vs = VideoStream(usePiCamera=True).start()
time.sleep(2)
name = ""

# start the FPS counter
fps = FPS().start()

#==================================================================================================
# Main Loop
#==================================================================================================
while (code_run):
	lcd.fill(BLACK) # erase workspace

	### STATE LOGIC ###
	if (screen == 1): ### SCREEN 1: Start ###
		# resets for multiple rounds
		currentname = "unknown"
		dir_name = ['']
		key_texts_position = ( 160, 90 )

		lcd.blit(start_surface, start_rect)
		pygame.draw.circle(lcd, start_circolor, circenter, 60)
		lcd.blit(start_cir_surface, start_cir_rect)

	### SCREEN 2: Smile ###
	elif screen == 2:
		lcd.blit(smile_surface, smile_rect)

		frame = vs.read()
		frame = imutils.resize(frame, width=500)
		facial_recognition()
			
	elif (screen == 3): ### SCREEN 3: Dispensing ###
		# update piTFT as servos move
		lcd.blit(dispense_surface, dispense_rect)
		pygame.display.update()		
		pitft.update()

		# move servos
		if dispense == 1: # dispense snack
			dispense_snack()
		if dispense == 0: # dispense marble
			dispense_marble()

		# update piTFT before thx screen
		lcd.fill(BLACK) # erase workspace
		lcd.blit(open_surface, open_rect)
		pygame.display.update()		
		pitft.update()
		time.sleep(3)

		# different paths for recognized vs not
		if dispense:
			# update piTFT before returning to init screen
			lcd.fill(BLACK) # erase workspace
			lcd.blit(thx_surface, thx_rect)	
			pygame.display.update()		
			pitft.update()
			time.sleep(2)

			dispense = 0		# reset dispense flag
			screen = 1
		else:
			screen = 4
	
	elif (screen == 4):  ### SCREEN 4: Click to Add Face ###
		lcd.blit(add_face_surface, add_face_rect)
		pygame.draw.circle(lcd, add_face_circolor, circenter, 60)
		lcd.blit(add_face_cir_surface, add_face_cir_rect)

	elif (screen == 5):	 ### SCREEN 5: Passcode ###
		lcd.blit(code_surface, code_rect)
		draw_num_text()
		draw_num_button()
		draw_dash()

	elif (screen == 6):	### SCREEN 6: Keyboard ###
		lcd.blit(add_dir1_surface, add_dir1_rect)
		lcd.blit(add_dir2_surface, add_dir2_rect)
		draw_keyboard()
		draw_text()

	### SCREEN 7: Add Face ###
	elif screen == 7:
		if pic_ins:
			# display take pic instructions upon 1st entry
			lcd.fill(BLACK)
			lcd.blit(take_pic1_surface, take_pic1_rect)
			lcd.blit(take_pic2_surface, take_pic2_rect)
			pygame.display.update()
			pitft.update()
			time.sleep(2)

			pic_ins = False

		frame = vs.read()
		frame = imutils.resize(frame, width=500)

		# take pic only when button 23 pressed (screen = 7)
		if (take_pic):
			add_to_library()

		facial_recognition()

	elif (screen == 8): ### SCREEN 8: Training Model ###
		cv2.destroyAllWindows()

		# update piTFT before calling train_model()
		lcd.blit(train_surface, train_rect)
		lcd.blit(train1_surface, train1_rect)
		pygame.display.update()
		pitft.update()

		if train_done: 
			# update piTFT before returning to init screen 
			lcd.fill(BLACK)
			lcd.blit(train_done_surface, train_done_rect)
			pygame.display.update()
			pitft.update()
			time.sleep(2)

			# Facial Recognition Initialization (for after retraining)
			currentname = "unknown"
			# Determine faces from encodings.pickle file model created from train_model.py
			encodingsP = "/home/pi/ece5725-finalproject/final_demo/encodings.pickle"
			# load the known faces and embeddings along with OpenCV's Haarcascade for face detection
			print("[INFO] loading encodings + face detector...")
			data = pickle.loads(open(encodingsP, "rb").read())
			name = ""

			# reset flag
			train_done = False

			screen = 1
		else:
			train_model()

	### ADD TOUCHSCREEN EVENT TO PYGAME EVENT QUEUE ###
	for event in pygame.event.get():
		if(event.type is pygame.MOUSEBUTTONUP):
			x,y = pygame.mouse.get_pos()
			touch_pt=(x,y)
			
			# circle button boundary
			euc_dist = math.dist(circenter, touch_pt)
			if euc_dist <= 60: 
				if (screen == 1):	# start	
					screen = 2
				elif (screen == 4):	# add face
					screen = 5
			
			# touchscreen passcode
			elif screen == 5:
				for key, rectangle in num_recs.items():
					if(rectangle.collidepoint(touch_pt)):
						code_texts[code_cursor_position[1]] += key
						code_cursor_position = ( code_cursor_position[0] + 1, code_cursor_position[1] )
				check_passcode()

			# touchscreen keyboard
			elif screen == 6:
				for key, rectangle in rectangles.items():
					if(rectangle.collidepoint(touch_pt)):
						if( key == '←' ):
							dir_name[key_cursor_position[1]] = dir_name[key_cursor_position[1]][:len(dir_name[key_cursor_position[1]]) - 1]
							# We then move the cursor positon back one
							key_cursor_position = ( key_cursor_position[0] - 1, key_cursor_position[1] )
						elif( key == 'Space' ):
							# Append the space character
							dir_name[key_cursor_position[1]] += ' '
							# Bump the cursor position
							key_cursor_position = ( key_cursor_position[0] + 1, key_cursor_position[1] )
						else:
							dir_name[key_cursor_position[1]] += key
							key_cursor_position = ( key_cursor_position[0] + 1, key_cursor_position[1] )
					
						if len(dir_name[key_cursor_position[1]]) == 18:
							try:
								path = "/home/pi/ece5725-finalproject/final_demo/dataset/" + str(dir_name[0])
								os.mkdir(path)
							except: 
								pass
							screen = 7
							pic_ins = True

	# pause screen before resetting passcode
	if len(code_texts[code_cursor_position[1]]) == 4:
		time.sleep(0.7)
		code_texts = ['']

	# Display GUI on piTFT
	pygame.display.update()	
	pitft.update()

	# time-out
	now=time.time()
	elapsed_time=now-start_time
	if (elapsed_time>time_limit):
		code_run = False
	fps.update()
	
#==================================================================================================
# Exit Main (clean up)
#==================================================================================================
# stop the timer and display FPS information
cv2.destroyAllWindows()
fps.stop()
vs.stop()
pygame.quit()
GPIO.cleanup()
del(pitft)