-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd_faces.py
More file actions
55 lines (48 loc) · 1.63 KB
/
Copy pathadd_faces.py
File metadata and controls
55 lines (48 loc) · 1.63 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
import cv2
import numpy as np
import pickle as pkl
import os
video = cv2.VideoCapture(0)
facedetect = cv2.CascadeClassifier('./data/haarcascades/haarcascade_frontalface_default.xml')
faces_data = []
i = 0
name = input("Enter Your Name: ")
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) <= 100 and i%10 == 0:
faces_data.append(resized_img)
i = i+1
cv2.putText(frame, str(len(faces_data)), (50,50), cv2.FONT_HERSHEY_COMPLEX, 1.5, (180,0,0), 3)
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 1)
cv2.imshow("Frame", frame)
k = cv2.waitKey(1)
if k == ord('a') or len(faces_data)==100:
break
video.release()
cv2.destroyAllWindows()
faces_data = np.asarray(faces_data)
faces_data = faces_data.reshape(100, -1)
if 'names.pkl' not in os.listdir('data/'):
names = [name]*100
with open('data/names.pkl', 'wb') as f:
pkl.dump(names, f)
else:
with open('data/names.pkl', 'rb') as f:
names = pkl.load(f)
names = names + [name]*100
with open('data/names.pkl', 'wb') as f:
pkl.dump(names, f)
if 'faces_data.pkl' not in os.listdir('data/'):
with open('data/faces_data.pkl', 'wb') as f:
pkl.dump(faces_data, f)
else:
with open('data/faces_data.pkl', 'rb') as f:
faces = pkl.load(f)
faces = np.append(faces, faces_data, axis = 0)
with open('data/faces_data.pkl', 'wb') as f:
pkl.dump(names, f)