-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive face rec multicam.py
307 lines (281 loc) · 14.2 KB
/
live face rec multicam.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
"""
Live webcam face recognition; By Jo-dan
"""
import time
import csv
import numpy as np
import cv2
from PIL import Image
#my functions
import faceframes
import getlab
import my_IFTTT
#webcam number
Camera_Number1 = 1
Camera_Number2 = 0
vc = cv2.VideoCapture(Camera_Number1)
vc2 = cv2.VideoCapture(Camera_Number2)
#paths
face_database = 'yalefaces'
cascadepath = "haarcascade_frontalface_default.xml"
facecascade = cv2.CascadeClassifier(cascadepath)
log_file = open('live_log.txt', 'a')
#colours
admin_colour = (255, 000, 000)
user_colour = (58, 238, 247)
unknown_colour = (000, 000, 255)
threat_colour = (000, 000, 255)
back_colour = (255, 255, 255)
#font of text on video
font = cv2.FONT_HERSHEY_SIMPLEX
recognizer = cv2.face.createLBPHFaceRecognizer()
#load subject database
timenow = time.strftime("%d/%m/%Y") + ' - ' + time.strftime("%I:%M:%S")
log_file.write('\nInitialised at {}. \n'.format(timenow))
with open('subjects.csv', "rb") as subjects:
reader = csv.reader(subjects)
subject_name = []
subject_type = []
for row in reader:
subject_name.append(row[1])
if len(row[2]) == 0:
subject_type.append('UNKNOWN')
else:
subject_type.append(row[2])
subject_name = [x.upper() for x in subject_name]
subject_type = [x.upper() for x in subject_type]
log_file.write(' CSV Read. \n')
# webcam; change this number 0~5 if webcam not working
def normal_subject_path(a):
normface = face_database + "/subject{}.normal".format(str(a).zfill(2))
normopen = Image.open(normface)
normnp = np.array(normopen, 'uint8')
return normnp
def load_data():
database = raw_input("Would you like to (r)ebuild, or (l)oad the database?")
if str(database) == 'r':
images, labels = getlab.get_images_and_labels(face_database)
cv2.destroyAllWindows()
recognizer.train(images, np.array(labels))
recognizer.save('trainingsaved')
log_file.write(' Recognizer Retrained \n')
elif str(database) == 'l':
recognizer.load('trainingsaved')
log_file.write(' Recogniser Training Loaded \n')
else:
load_data()
cv2.destroyAllWindows()
shape_type = raw_input("(b)oxes, (c)circles, (p)oi or (s)amaritan?")
log_file.write(' Run in "{}" Mode. \n'.format(shape_type))
load_data()
nbr_old = 0
nbr_old2 = 0
nbr_predicted = 0
if shape_type == 'p':
admin_colour = (58, 238, 247)
analog_colour = (58, 238, 247)
user_colour = (243, 124, 13)
unknown_colour = (255, 255, 255)
threat_colour = (000, 000, 255)
back_colour = (000, 000, 000)
while True:
#read frame by frame
ret, frame = vc.read()
#cv2.imshow('stream', frame)
admin_present = False
user_present = False
unknown_present = False
threat_present = False
analog_present = False
grey_predict = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
predict_image = np.array(grey_predict, 'uint8')
faces = facecascade.detectMultiScale(predict_image, 1.03, 5, 0, (150, 150))
for (x, y, h, w) in faces:
nbr_predicted = recognizer.predict(predict_image[y:y+h, x:x+w])
#strings for stream
subtxt = "Subject: {}".format(nbr_predicted)
nametxt = "Name: {}".format(subject_name[nbr_predicted])
typetxt = "Type: {}".format(subject_type[nbr_predicted])
#Text on stream
if subject_type[nbr_predicted] == 'ADMIN':
all_colour = admin_colour
admin_present = True
elif subject_type[nbr_predicted] == 'USER':
all_colour = user_colour
user_present = True
elif subject_type[nbr_predicted] == 'UNKNOWN':
all_colour = unknown_colour
unknown_present = True
elif subject_type[nbr_predicted] == "THREAT":
all_colour = threat_colour
threat_present = True
elif subject_type[nbr_predicted] == "ANALOG":
all_colour = threat_colour
analog_present = True
if shape_type == 'c':
cv2.circle(frame, (x+int(round(.5*w)), y+int(round(.5*h))), int(round(.6*h)), all_colour, 4)
subco = (x+w+30, y+int(round(.5*h))-25)
nameco = (x+w+30, y+int(round(.5*h)))
typeco = (x+w+30, y+int(round(.5*h))+25)
elif shape_type == 'p':
faceframes.poi_box(frame, x, y, w, h, subject_type[nbr_predicted])
subco = (x, y+h+25)
nameco = (x, y+h+50)
typeco = (x, y+h+75)
elif shape_type == 's':
faceframes.sam_circle(frame, x, y, w, h, subject_type[nbr_predicted])
subco = (x+w+30, y+int(round(.5*h))-25)
nameco = (x+w+30, y+int(round(.5*h)))
typeco = (x+w+30, y+int(round(.5*h))+25)
else:
cv2.rectangle(frame, (x, y), (x+w, y+h), all_colour, 2)
txt_x = x
subco = (x, y+h+25)
nameco = (x, y+h+50)
typeco = (x, y+h+75)
cv2.putText(frame, subtxt, subco, font, .7, back_colour, 3)
cv2.putText(frame, subtxt, subco, font, .7, all_colour, 2)
cv2.putText(frame, nametxt, nameco, font, .7, back_colour, 3)
cv2.putText(frame, nametxt, nameco, font, .7, all_colour, 2)
cv2.putText(frame, typetxt, typeco, font, .7, back_colour, 3)
cv2.putText(frame, typetxt, typeco, font, .7, all_colour, 2)
cv2.imshow('stream', frame)
if nbr_predicted != nbr_old:
print "Recognized as {} @ Camera 1".format(nbr_predicted)
#my_IFTTT.IFTTT('Face_Detected', subject_type[nbr_predicted], subject_name[nbr_predicted], str(Camera_Number1))
log_file.write(' Subject {} recognised: {} @ Camera 1\n'.format(nbr_predicted, subject_type[nbr_predicted]))
#recognp = normal_subject_path(nbr_predicted)
#cv2.imshow("Recognised as...", recognp)
#oldnp = normal_subject_path(nbr_old)
#cv2.imshow("Previous", oldnp)
nbr_old = nbr_predicted
if threat_present == True:
cv2.putText(frame, 'THREAT DETECTED', (0, 25), font, 1, back_colour, 5)
cv2.putText(frame, 'ACCESS: DENIED', (0, 50), font, 1, back_colour, 5)
cv2.putText(frame, 'THREAT DETECTED', (0, 25), font, 1, threat_colour, 2)
cv2.putText(frame, 'ACCESS: DENIED', (0, 50), font, 1, threat_colour, 2)
elif analog_present == True:
cv2.putText(frame, 'ANALOG INTERFACE DETECTED', (0, 25), font, 1, back_colour, 5)
cv2.putText(frame, 'ACCESS: GRANTED', (0, 50), font, 1, back_colour, 5)
cv2.putText(frame, 'ANALOG INTERFACE DETECTED', (0, 25), font, 1, threat_colour, 2)
cv2.putText(frame, 'ACCESS: GRANTED', (0, 50), font, 1, threat_colour, 2)
elif admin_present == True:
cv2.putText(frame, 'ADMIN DETECTED', (0, 25), font, 1, back_colour, 5)
cv2.putText(frame, 'ACCESS: GRANTED', (0, 50), font, 1, back_colour, 5)
cv2.putText(frame, 'ADMIN DETECTED', (0, 25), font, 1, admin_colour, 2)
cv2.putText(frame, 'ACCESS: GRANTED', (0, 50), font, 1, admin_colour, 2)
elif user_present == True:
cv2.putText(frame, 'USER DETECTED', (0, 25), font, 1, back_colour, 5)
cv2.putText(frame, 'ACCESS: RESTRICTED', (0, 50), font, 1, back_colour, 5)
cv2.putText(frame, 'USER DETECTED', (0, 25), font, 1, user_colour, 2)
cv2.putText(frame, 'ACCESS: RESTRICTED', (0, 50), font, 1, user_colour, 2)
elif unknown_present == True:
cv2.putText(frame, 'UNKNOWN USER', (0, 25), font, 1, back_colour, 5)
cv2.putText(frame, 'ACCESS: DENIED', (0, 50), font, 1, back_colour, 5)
cv2.putText(frame, 'UNKNOWN USER', (0, 25), font, 1, unknown_colour, 2)
cv2.putText(frame, 'ACCESS: DENIED', (0, 50), font, 1, unknown_colour, 2)
cv2.imshow('stream', frame)
#camera 2
#read frame2 by frame2
ret2, frame2 = vc2.read()
#cv2.imshow('stream', frame2)
admin_present = False
user_present = False
unknown_present = False
threat_present = False
analog_present = False
grey_predict = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
predict_image = np.array(grey_predict, 'uint8')
faces2 = facecascade.detectMultiScale(predict_image, 1.03, 5, 0, (150, 150))
for (x, y, h, w) in faces2:
nbr_predicted2 = recognizer.predict(predict_image[y:y+h, x:x+w])
#strings for stream
subtxt = "Subject: {}".format(nbr_predicted2)
nametxt = "Name: {}".format(subject_name[nbr_predicted2])
typetxt = "Type: {}".format(subject_type[nbr_predicted2])
#Text on stream
if subject_type[nbr_predicted2] == 'ADMIN':
all_colour = admin_colour
admin_present = True
elif subject_type[nbr_predicted2] == 'USER':
all_colour = user_colour
user_present = True
elif subject_type[nbr_predicted2] == 'UNKNOWN':
all_colour = unknown_colour
unknown_present = True
elif subject_type[nbr_predicted2] == "THREAT":
all_colour = threat_colour
threat_present = True
elif subject_type[nbr_predicted2] == "ANALOG":
all_colour = threat_colour
analog_present = True
if shape_type == 'c':
cv2.circle(frame2, (x+int(round(.5*w)), y+int(round(.5*h))), int(round(.6*h)), all_colour, 4)
subco = (x+w+30, y+int(round(.5*h))-25)
nameco = (x+w+30, y+int(round(.5*h)))
typeco = (x+w+30, y+int(round(.5*h))+25)
elif shape_type == 'p':
faceframes.poi_box(frame2, x, y, w, h, subject_type[nbr_predicted2])
subco = (x, y+h+25)
nameco = (x, y+h+50)
typeco = (x, y+h+75)
elif shape_type == 's':
faceframes.sam_circle(frame2, x, y, w, h, subject_type[nbr_predicted2])
subco = (x+w+30, y+int(round(.5*h))-25)
nameco = (x+w+30, y+int(round(.5*h)))
typeco = (x+w+30, y+int(round(.5*h))+25)
else:
cv2.rectangle(frame2, (x, y), (x+w, y+h), all_colour, 2)
txt_x = x
subco = (x, y+h+25)
nameco = (x, y+h+50)
typeco = (x, y+h+75)
cv2.putText(frame2, subtxt, subco, font, .7, back_colour, 3)
cv2.putText(frame2, subtxt, subco, font, .7, all_colour, 2)
cv2.putText(frame2, nametxt, nameco, font, .7, back_colour, 3)
cv2.putText(frame2, nametxt, nameco, font, .7, all_colour, 2)
cv2.putText(frame2, typetxt, typeco, font, .7, back_colour, 3)
cv2.putText(frame2, typetxt, typeco, font, .7, all_colour, 2)
cv2.imshow('stream 2', frame2)
if nbr_predicted2 != nbr_old2:
print "Recognized as {} @ Camera 2".format(nbr_predicted2)
#my_IFTTT.IFTTT('Face_Detected', subject_type[nbr_predicted2], subject_name[nbr_predicted2], str(Camera_Number2))
log_file.write(' Subject {} recognised: {} @ Camera 2\n'.format(nbr_predicted2, subject_type[nbr_predicted2]))
#recognp = normal_subject_path(nbr_predicted2)
#cv2.imshow("Cam 2 - Recognised As", recognp)
#oldnp2 = normal_subject_path(nbr_old2)
#cv2.imshow("Cam 2 - Previous", oldnp2)
nbr_old2 = nbr_predicted2
if threat_present == True:
cv2.putText(frame2, 'THREAT DETECTED', (0, 25), font, 1, back_colour, 5)
cv2.putText(frame2, 'ACCESS: DENIED', (0, 50), font, 1, back_colour, 5)
cv2.putText(frame2, 'THREAT DETECTED', (0, 25), font, 1, threat_colour, 2)
cv2.putText(frame2, 'ACCESS: DENIED', (0, 50), font, 1, threat_colour, 2)
elif analog_present == True:
cv2.putText(frame, 'ANALOG INTERFACE DETECTED', (0, 25), font, 1, back_colour, 5)
cv2.putText(frame, 'ACCESS: GRANTED', (0, 50), font, 1, back_colour, 5)
cv2.putText(frame, 'ANALOG INTERFACE DETECTED', (0, 25), font, 1, threat_colour, 2)
cv2.putText(frame, 'ACCESS: GRANTED', (0, 50), font, 1, threat_colour, 2)
elif admin_present == True:
cv2.putText(frame2, 'ADMIN DETECTED', (0, 25), font, 1, back_colour, 5)
cv2.putText(frame2, 'ACCESS: GRANTED', (0, 50), font, 1, back_colour, 5)
cv2.putText(frame2, 'ADMIN DETECTED', (0, 25), font, 1, admin_colour, 2)
cv2.putText(frame2, 'ACCESS: GRANTED', (0, 50), font, 1, admin_colour, 2)
elif user_present == True:
cv2.putText(frame2, 'USER DETECTED', (0, 25), font, 1, back_colour, 5)
cv2.putText(frame2, 'ACCESS: RESTRICTED', (0, 50), font, 1, back_colour, 5)
cv2.putText(frame2, 'USER DETECTED', (0, 25), font, 1, user_colour, 2)
cv2.putText(frame2, 'ACCESS: RESTRICTED', (0, 50), font, 1, user_colour, 2)
elif unknown_present == True:
cv2.putText(frame2, 'UNKNOWN USER', (0, 25), font, 1, back_colour, 5)
cv2.putText(frame2, 'ACCESS: DENIED', (0, 50), font, 1, back_colour, 5)
cv2.putText(frame2, 'UNKNOWN USER', (0, 25), font, 1, unknown_colour, 2)
cv2.putText(frame2, 'ACCESS: DENIED', (0, 50), font, 1, unknown_colour, 2)
cv2.imshow('stream 2', frame2)
if cv2.waitKey(1) & 0xFF == 27:
break
vc.release()
cv2.destroyAllWindows()
timenow = time.strftime("%d/%m/%Y") + ' - ' + time.strftime("%I:%M:%S")
log_file.write('Program Terminated at {}. \n'.format(timenow))
print '.......\nGoodbye \n.......'