-
Notifications
You must be signed in to change notification settings - Fork 1
/
add_faces.py
68 lines (54 loc) · 1.83 KB
/
add_faces.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
import cv2
import pickle
import numpy as np
import os
if not os.path.exists('data/'):
os.makedirs('data/')
video = cv2.VideoCapture(0)
facedetect= cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
faces_data = []
i=0
name = input("Enter your aadhar number: ")
framesTotal=51
captureAfterFrame=2
while True:
ret, frame = video.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces=facedetect.detectMultiScale(gray, 1.3 ,5)
for (x, y, w, h) in faces:
crop_img = frame[y:y+h, x:x+w]
resized_img = cv2.resize(crop_img, (50, 50))
if len(faces_data)<= framesTotal and i%captureAfterFrame==0:
faces_data.append(resized_img)
i=i+1
cv2.putText(frame, str(len(faces_data)),(50,50),cv2.FONT_HERSHEY_COMPLEX, 1, (50,50,255), 1 )
cv2.rectangle(frame, (x,y), (x+w, y+h), (50,50,255), 1)
cv2.imshow('frame', frame)
k=cv2.waitKey(1)
if k== ord('q') or len(faces_data) >= framesTotal:
break
video.release()
cv2.destroyAllWindows()
# print(len(faces_data))
faces_data = np.asarray(faces_data)
faces_data = faces_data.reshape((framesTotal, -1))
print(faces_data)
if 'names.pkl' not in os.listdir('data/'):
names=[name]*framesTotal
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]*framesTotal
with open('data/names.pkl', 'wb') as f:
pickle.dump(names, f)
if 'faces_data.pkl' not in os.listdir('data/'):
with open('data/faces_data.pkl', 'wb') as f:
pickle.dump(faces_data, f)
else:
with open('data/faces_data.pkl', 'rb') as f:
faces=pickle.load(f)
faces=np.append(faces, faces_data, axis=0)
with open('data/faces_data.pkl', 'wb') as f:
pickle.dump(faces, f)