-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathback.py
More file actions
245 lines (172 loc) · 6.92 KB
/
Copy pathback.py
File metadata and controls
245 lines (172 loc) · 6.92 KB
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
import cv2
import numpy as np
import os
import xlwt
import time
from PIL import Image
t = time.strftime("Attendence on %d_%m_%Y at %I_%M %p") + '.xls'
book = xlwt.Workbook()
sheet = book.add_sheet("Sheet 1", cell_overwrite_ok=True)
def entry(id, r):
sheet.write(0, 0, "NAME")
sheet.write(0, 1, "ATTENDANCE")
sheet.write(r + 1, 0, id)
sheet.write(r + 1, 1, 'Present')
book.save(t)
def attendance():
r = 0
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer/trainer.yml')
cascadePath = "haarcascade.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)
font = cv2.FONT_HERSHEY_SIMPLEX
id = 0
flag = 0
id2 = 0
# names related to ids
names = ['None', 'Hari','Gayathri','Rahul','Prasanth']
# Initialize and start realtime video capture
cam = cv2.VideoCapture(0)
cam.set(3, 640) # set video widht
cam.set(4, 480) # set video height
# Define min window size to be recognized as a face
minW = 0.1 * cam.get(3)
minH = 0.1 * cam.get(4)
while True:
ret, img = cam.read()
img = cv2.flip(img, 1) # Flip vertically
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize=(int(minW), int(minH)),
)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
path = 'C:\\Users\\Hari\\Desktop\\REVIEW\\'
cv2.imwrite(os.path.join(path, str(id) + '.jpg'), img)
id, confidence = recognizer.predict(gray[y:y + h, x:x + w])
id2 = id
# Check if confidence is less them 100 ==> "0" is perfect match
if (confidence < 100):
id = names[id]
confidence = " {0}%".format(round(100 - confidence))
else:
id = "Unknown Face"
confidence = " {0}%".format(round(100 - confidence))
cv2.putText(img, str(id), (x + 5, y - 5), font, 1, (0, 0, 255), 2)
cv2.putText(img, str(confidence), (x + w, y), font, 0.5, (0, 0, 255), 1)
for i in range(1, 60):
if (id2 == i):
entry(id, i)
cv2.imshow('camera', img)
k = cv2.waitKey(10) & 0xff # Press 'ESC' for exiting video
if k == 27:
cam.release()
cv2.destroyAllWindows()
break
def snap():
r = 0
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer/trainer.yml')
cascadePath = "haarcascade.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)
font = cv2.FONT_HERSHEY_SIMPLEX
id = 0
flag = 0
id2 = 0
# names related to ids
names = ['None', 'Hari','Gayathri','Rahul','Prasanth']
# Initialize and start realtime video capture
cam = cv2.VideoCapture(0)
cam.set(3, 640) # set video widht
cam.set(4, 480) # set video height
r, img = cam.read()
img = cv2.flip(img, 1) # Flip vertically
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Define min window size to be recognized as a face
minW = 0.1 * cam.get(3)
minH = 0.1 * cam.get(4)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize=(int(minW), int(minH)),
)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
path = 'C:\\Users\\Hari\\Desktop\\REVIEW\\Data'
cv2.imwrite(os.path.join(path, str(id) + '.jpg'), img)
cv2.imwrite("new.jpg", img)
id, confidence = recognizer.predict(gray[y:y + h, x:x + w])
id2 = id
# Check if confidence is less them 100 ==> "0" is perfect match
if (confidence < 100):
id = names[id]
confidence = " {0}%".format(round(100 - confidence))
else:
id = "Unknown Face"
confidence = " {0}%".format(round(100 - confidence))
cv2.putText(img, str(id), (x + 5, y - 5), font, 1, (0, 0, 255), 2)
cv2.putText(img, str(confidence), (x + w, y), font, 0.5, (0, 0, 255), 1)
for i in range(1, 60):
if (id2 == i):
entry(id, i)
cam.release()
def report():
file = "C:\\Users\\Hari\\Desktop\\REVIEW\\" + str(t)
os.startfile(file)
def trainer(x):
cam = cv2.VideoCapture(0)
cam.set(3, 640) # set video width1
cam.set(4, 480) # set video height
face_detector = cv2.CascadeClassifier('haarcascade.xml')
# For each person, enter one numeric face id
face_id = x
count = 0
while (True):
ret, img = cam.read()
img = cv2.flip(img, 1) # flip video image vertically
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
count += 1
# Save the captured image into the datasets folder
cv2.imwrite("dataset/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y + h, x:x + w])
cv2.imshow('image', img)
k = cv2.waitKey(100) & 0xff # Press 'ESC' for exiting video
if k == 27:
cam.release()
cv2.destroyAllWindows()
break
elif count >= 100:
cam.release()
cv2.destroyAllWindows()
break
# Path for face image database
path = 'dataset'
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier("haarcascade.xml");
# function to get the images and label data
def getImagesAndLabels(path):
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
faceSamples = []
ids = []
for imagePath in imagePaths:
PIL_img = Image.open(imagePath).convert('L') # convert it to grayscale
img_numpy = np.array(PIL_img, 'uint8')
id = int(os.path.split(imagePath)[-1].split(".")[1])
faces = detector.detectMultiScale(img_numpy)
for (x, y, w, h) in faces:
faceSamples.append(img_numpy[y:y + h, x:x + w])
ids.append(id)
return faceSamples, ids
print("\n [INFO] Training faces. It will take a few seconds. Wait ...")
faces, ids = getImagesAndLabels(path)
recognizer.train(faces, np.array(ids))
# Save the model into trainer/trainer.yml
recognizer.write('trainer/trainer.yml') # recognizer.save() worked on Mac, but not on Pi
# Print the number of faces trained and end program
print("\n [INFO] {0} faces trained. Exiting Program".format(len(np.unique(ids))))