-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-new-face.py
71 lines (51 loc) · 1.63 KB
/
add-new-face.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
import cv2
import numpy as np
import os
import pickle
face_data = []
i = 0
cam = cv2.VideoCapture(0)
facec = cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml')
name = input('Enter your name --> ')
ret = True
while(ret):
ret, frame = cam.read()
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face_coordinates = facec.detectMultiScale(gray, 1.3, 4)
for (x, y, w, h) in face_coordinates:
faces = frame[y:y+h, x:x+w, :]
resized_faces = cv2.resize(faces, (50, 50))
if i % 10 == 0 and len(face_data) < 10:
face_data.append(resized_faces)
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
i += 1
cv2.imshow('frames', frame)
if cv2.waitKey(1) == 27 or len(face_data) >= 10:
break
else:
print('error')
break
cv2.destroyAllWindows()
cam.release()
face_data = np.asarray(face_data)
face_data = face_data.reshape(10, -1)
if 'names.pkl' not in os.listdir('data/'):
names = [name]*10
with open('data/names.pkl', 'wb') as f:
pickle.dump(names, f)
else:
with open('data/names.pkl', 'rb') as f:
names = pickle.load(f)
names = names + [name]*10
with open('data/names.pkl', 'wb') as f:
pickle.dump(names, f)
if 'faces.pkl' not in os.listdir('data/'):
with open('data/faces.pkl', 'wb') as w:
pickle.dump(face_data, w)
else:
with open('data/faces.pkl', 'rb') as w:
faces = pickle.load(w)
faces = np.append(faces, face_data, axis=0)
with open('data/faces.pkl', 'wb') as w:
pickle.dump(faces, w)