forked from STU-IT/elevfremm0deautomat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tjekIndV1.py
272 lines (207 loc) · 7.23 KB
/
tjekIndV1.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
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import os
import numpy as np
from guizero import App, Text, PushButton, TextBox, Box
def getSub():
sub = open("subjects.csv")
subjects = sub.read().split(",")
subjects[len(subjects)- 1] = subjects[len(subjects) - 1].strip("")
sub.close()
if(len(subjects) == 1):
subjects = [""]
else:
tempSubjects = [""]
for i in range(len(subjects) - 1):
print(i)
tempSubjects.append(subjects[i])
subjects = tempSubjects
print(subjects)
return subjects
'''
print(len(subjects))
print(subjects)
'''
def takeImg():
global dirs
global app
count = 0
app.destroy()
if(len(dirs) == 0):
os.makedirs("person/s1")
else:
os.makedirs("person/s" + str(len(dirs) + 1))
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor = 1.1,
minNeighbors = 5,
minSize = (30, 30)
)
for(x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# show the frame
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the `q` key was pressed, break from the loop
if key == ord("q"):
dirs = os.listdir("person")
images = os.listdir("person/" + dirs[len(dirs) - 1])
camera.capture("person/" + dirs[len(dirs) - 1] + "/" + str(len(images) + 1) + ".jpg")
count = count + 1
if(count == 5):
break
else:
continue
def finsh():
global subjects
print(subjects)
sub = open("subjects.csv", "a")#append and read
sub.write(tbox.get() + ",")
sub.close()
subjects = getSub()
print(subjects)
sub.close()
app.destroy()
cv2.destroyAllWindows()
app = App("tjek ind V1")
Text(app, "please enter your name")
tbox = TextBox(app)
PushButton(app, finsh, text="Save name")
app.display()
def trainer():
def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor = 1.1,
minNeighbors = 5,
minSize = (30, 30),
#flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
if(len(faces) == 0):
return None, None
(x, y, w, h) = faces[0]
return gray[y:y+w, x:x+h], faces[0]
def prepare_training_data(data_folder_path):
dirs = os.listdir(data_folder_path)
faces = []
labels = []
#folder starts with 's'
for dir_name in dirs:
if(not dir_name.startswith("s")):
continue
#remove the 's' give us the lable
label = int(dir_name.replace("s", ""))
subject_dir_path = data_folder_path + "/" + dir_name
subject_images_names = os.listdir(subject_dir_path)
for image_name in subject_images_names:
if(image_name.startswith(".")):
continue
image_path = subject_dir_path + "/" + image_name
print(image_name)
image = cv2.imread(image_path)
#view image
#cv2.imshow("Training in image...", image)
#cv2.waitKey(100)
face, rect = detect_face(image)
if(face is not None):
faces.append(face)
labels.append(label)
#cv2.destroyAllWindows()
#cv2.waitKey(1)
#cv2.destroyAllWindows()
return faces, labels
print("Preparing data...")
faces, labels = prepare_training_data("person")
print("Data prepared")
print("Total faces: ", len(faces))
print("Total labels: ", len(labels))
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
face_recognizer.train(faces, np.array(labels))
face_recognizer.write("recData.xml")
def recognition():
global subjects
trainer()
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
face_recognizer.read("recData.xml")
count = 0
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor = 1.2,
minNeighbors = 5,
minSize = (30, 30)
)
print(faces)
if(faces == ()):
count = 0
for(x, y, w, h) in faces:
face = gray[y:y+w, x:x+h]
label = face_recognizer.predict(face)
print(label)
label_text = subjects[label[0]]
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
if(label[1] < 110):
cv2.putText(image, label_text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 0), 2)
cv2.destroyAllWindows()
return label[0]
else:
#if 5 sec or more is unknown. ask you to registrant
count = count + 1
print(count)
if(count >= 20):
return "Unknown"
cv2.putText(image, "Unknown", (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 0), 2)
# show the frame
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
cv2.destroyAllWindows()
def noToRegBefore():
print("")
#-------------------------------------------------
#code stuff
dirs = os.listdir("person")
subjects = getSub()
cascPath = "../haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
camera = PiCamera()
camera.resolution = (640, 480) #be Full screen
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(640, 480)) #be Full screen
app = App("tjek ind V1")
if(len(subjects) == 1):
Text(app, "be the first")
PushButton(app, takeImg ,text="Take picture")
app.display()
else:
#check if face is known
print("")
rec = recognition()
if(rec == "Unknown"):
Text(app, "have registratet before?")
box = Box(app, layout="grid")
PushButton(box, takeImg ,text="yes", grid=[0,1])
PushButton(box, takeImg ,text="no", grid=[0,0])
app.display()
else:
name = subjects[rec]
Text(app, "are you " + name)
app.display()