-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard_main.py
453 lines (375 loc) · 13 KB
/
board_main.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
#!/usr/bin/python
import cv2
import sys
import os
import time
import numpy as np
from PIL import Image
import logging as log
import datetime as dt
from time import sleep
Tstart = time.time()
index= 0
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
log.basicConfig(filename='webcam.log',level=log.INFO)
video_capture = cv2.VideoCapture(0)
# anterior = 0
subjects = ["", "1111", "2222", "3333", "4444", "5555", "6666" ]
# 1111 = Anna, 2222 = Chris, 3333 = Marsh, 4444 = Kelly, 5555 = Alex, 6666 = Jerry
import mraa
import time
# Refer to the pin-out diagram for the GPIO number to silk print mapping
# in this example the number 2 maps to P10 on LinkIt Smart 7688 board
#from mraa import Result as r
rowVal = 0
colVal = 0
value = ""
pin1 = mraa.Gpio(24)
pin2 = mraa.Gpio(26)
pin3 = mraa.Gpio(30)
pin4 = mraa.Gpio(32)
pin5 = mraa.Gpio(34)
pin6 = mraa.Gpio(23)
pin7 = mraa.Gpio(25)
pin8 = mraa.Gpio(27)
pin1.dir(mraa.DIR_IN)
pin2.dir(mraa.DIR_IN)
pin3.dir(mraa.DIR_IN)
pin4.dir(mraa.DIR_IN)
pin5.dir(mraa.DIR_OUT)
pin6.dir(mraa.DIR_OUT)
pin7.dir(mraa.DIR_OUT)
pin8.dir(mraa.DIR_OUT)
if(pin1 == None):
print('Error')
#ret = pin1.dir(mraa.DIR_IN)
#if(ret!=mraa.SUCCESS):
# print('s')
#pin8.write(1)
#pin1.write(1)
pin8.write(1)
pin7.write(1)
pin6.write(1)
pin5.write(1)
pin4.write(0)
pin3.write(0)
pin2.write(0)
pin1.write(0)
def getID():
result = ""
while True:
if(pin1.read() == 1):
rowVal=1
if(pin2.read() == 1):
rowVal=2
if(pin3.read() == 1):
rowVal=3
if(pin4.read() == 1):
rowVal=4
if not rowVal == 0:
#num = pin.read()
pin5.dir(mraa.DIR_IN)
pin6.dir(mraa.DIR_IN)
pin7.dir(mraa.DIR_IN)
pin8.dir(mraa.DIR_IN)
pin5.write(0)
pin6.write(0)
pin7.write(0)
pin8.write(0)
if rowVal==1:
pin1.dir(mraa.DIR_OUT)
if(pin3.read() == 1):
rowVal=3
if(pin4.read() == 1):
rowVal=4
if not rowVal == 0:
#num = pin.read()
pin5.dir(mraa.DIR_IN)
pin6.dir(mraa.DIR_IN)
pin7.dir(mraa.DIR_IN)
pin8.dir(mraa.DIR_IN)
pin5.write(0)
pin6.write(0)
pin7.write(0)
pin8.write(0)
if rowVal==1:
pin1.dir(mraa.DIR_OUT)
pin1.write(1)
if rowVal==2:
pin2.dir(mraa.DIR_OUT)
pin2.write(1)
if rowVal==3:
pin3.dir(mraa.DIR_OUT)
pin3.write(1)
if rowVal==4:
pin4.dir(mraa.DIR_OUT)
pin4.write(1)
if(pin5.read()==1):
colVal = 4
if(pin6.read()==1):
colVal = 3
if(pin7.read()==1):
colVal = 2
if(pin8.read()==1):
colVal = 1
#print (str(rowVal) + " " +str(colVal))
pin1.dir(mraa.DIR_IN)
pin2.dir(mraa.DIR_IN)
pin3.dir(mraa.DIR_IN)
pin4.dir(mraa.DIR_IN)
pin5.dir(mraa.DIR_OUT)
pin6.dir(mraa.DIR_OUT)
pin7.dir(mraa.DIR_OUT)
pin8.dir(mraa.DIR_OUT)
pin8.write(1)
pin7.write(1)
pin6.write(1)
pin5.write(1)
pin4.write(0)
pin3.write(0)
pin2.write(0)
pin1.write(0)
if(rowVal==1 and colVal ==1):
value = "A"
if(rowVal==2 and colVal ==1):
value = "3"
if(rowVal==3 and colVal ==1):
value = "2"
if(rowVal==4 and colVal ==1):
value = "1"
if(rowVal==1 and colVal ==2):
value = "B"
if(rowVal==2 and colVal ==2):
value = "6"
if(rowVal==3 and colVal ==2):
value = "5"
if(rowVal==4 and colVal ==2):
value = "4"
if(rowVal==1 and colVal ==3):
value = "C"
if(rowVal==2 and colVal ==3):
value = "9"
if(rowVal==3 and colVal ==3):
value = "8"
if(rowVal==4 and colVal ==3):
value = "7"
if(rowVal==1 and colVal ==4):
value = "D"
if(rowVal==2 and colVal ==4):
value = "#"
if(rowVal==3 and colVal ==4):
value = "0"
if(rowVal==4 and colVal ==4):
value = "*"
break
if not value == "":
print(value)
result+=value
time.sleep(0.3)
colVal=0
rowVal=0
value=""
return result
def detect_face(img):
#convert the test image to gray image as opencv face detector expects gray images
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#load OpenCV face detector, I am using LBP which is fast
#there is also a more accurate but slow Haar classifier
face_cascade = cv2.CascadeClassifier('lbpcascade_frontalface.xml')
#let's detect multiscale (some images may be closer to camera than others) images
#result is a list of faces
faces = face_cascade.detectMultiScale(gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30));
#if no faces are detected then return original img
if (len(faces) == 0):
return None, None
#under the assumption that there will be only one face,
#extract the face area
(x, y, w, h) = faces[0]
#return only the face part of the image
return gray[y:y+w, x:x+h], faces[0]
def prepare_training_data(data_folder_path):
#------STEP-1--------
#get the directories (one directory for each subject) in data folder
dirs = os.listdir(data_folder_path)
#list to hold all subject faces
faces = []
#list to hold labels for all subjects
labels = []
#let's go through each directory and read images within it
for dir_name in dirs:
#our subject directories start with letter 's' so
#ignore any non-relevant directories if any
if not dir_name.startswith("s"):
continue;
#------STEP-2--------
#extract label number of subject from dir_name
#format of dir name = slabel
#, so removing letter 's' from dir_name will give us label
label = int(dir_name.replace("s", ""))
#build path of directory containin images for current subject subject
#sample subject_dir_path = "training-data/s1"
subject_dir_path = data_folder_path + "/" + dir_name
#get the images names that are inside the given subject directory
subject_images_names = os.listdir(subject_dir_path)
#------STEP-3--------
#go through each image name, read image,
#detect face and add face to list of faces
for image_name in subject_images_names:
#ignore system files like .DS_Store
if image_name.startswith("."):
continue
#build image path
#sample image path = training-data/s1/1.pgm
image_path = subject_dir_path + "/" + image_name
#read image
image = cv2.imread(image_path)
#display an image window to show the image
#cv2.imshow("Training on image...", cv2.resize(image, (400, 500)))
cv2.waitKey(100)
#detect face
face, rect = detect_face(image)
#------STEP-4--------
#for the purpose of this tutorial
#we will ignore faces that are not detected
if face is not None:
#add face to list of faces
faces.append(face)
#add label for this face
labels.append(label)
cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.destroyAllWindows()
return faces, labels
#data will be in two lists of same size
#one list will contain all the faces
#and the other list will contain respective labels for each face
print("Preparing data...")
faces, labels = prepare_training_data("training-data")
print(faces)
print(labels)
print("Data prepared")
#print total faces and labels
print("Total faces: ", len(faces))
print("Total labels: ", len(labels))
face_recognizer = cv2.createLBPHFaceRecognizer()
face_recognizer.train(faces, np.array(labels))
def draw_rectangle(img, rect):
(x, y, w, h) = rect
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
def draw_text(img, text, x, y):
cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 0), 2)
def predict(test_img):
#make a copy of the image as we don't want to change original image
img = test_img.copy()
#detect face from the image
face, rect = detect_face(img)
#predict the image using our face recognizer
label, confidence = face_recognizer.predict(face)
print("Confidence", confidence) #confirm with marshall later what confidence means
#get name of respective label returned by face recognizer
label_text = subjects[label]
#draw a rectangle around face detected
draw_rectangle(img, rect)
#draw name of predicted person if confidence > 50
if confidence < 70:
draw_text(img, label_text, rect[0], rect[1]-5)
return img, label_text, confidence
exists = False
while True:
while not exists:
IDNum = getID()
dirs = os.listdir("Faces")
for face in dirs:
if IDNum in face:
exists =True
if not exists:
print("Invalid ID Number")
try:
if not video_capture.isOpened():
print('Unable to load camera.')
sleep(5)
pass
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30)
)
if faces == ():
continue
x1=0
y1=0
x2=0
y2=0
if len(faces) > 1:
# If more than 1 face is detected in the frame, draw a blue rectangle
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
# if anterior != len(faces):
# anterior = len(faces)
# log.info("faces: "+str(len(faces))+" at "+str(dt.datetime.now()))
elif len(faces) == 0:
continue
else:
#If only 1 face is detected in the frame, draw a green rectangle around the face
for (x, y, w, h) in faces:
#cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
x1=x
y1=y
x2=x+w
y2=y+h
# if anterior != len(faces):
# anterior = len(faces)
# log.info("faces: "+str(len(faces))+" at "+str(dt.datetime.now()))
Ttemp = time.time()
if Ttemp - Tstart > 0.8:
cv2.imwrite("oripics\\"+str(index)+"s.jpg",frame)
box = (x1*0.97,y1*0.97,x2*1.03,y2*1.03)
im = Image.open("oripics\\"+str(index)+"s.jpg")
region = im.crop(box)
region.save("editpics\\"+str(index)+"c.png","PNG")
Tstart = Ttemp
print("Predicting images...")
#load comparing images
test_img1 = cv2.imread("oripics/"+str(index)+"s.jpg")
#perform a prediction
print("Test image", test_img1)
predicted_img1, name_of_person, confidence_num = predict(test_img1) #added label
print(predicted_img1, name_of_person)
# if(predict == 0):
# print("Prediction failed, retrying")
# continue
print("Prediction complete")
#display both images
#cv2.imshow("test",cv2.resize(predicted_img1, (400, 500)))
index = index + 1
# Display the resulting frame
#cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Display the resulting frame
#cv2.imshow('Video', frame)
if(confidence_num < 50):
if(name_of_person == IDNum):
print("Matched ID and Face")
exists = False
sleep(5)
continue
else:
print("ID and Face not matching")
exists = False
sleep(5)
continue
except cv2.error:
print("No match, try again")
#except OpenCV Error:
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()