-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcollector.py
61 lines (42 loc) · 1.23 KB
/
collector.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
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
count = 0
classifier = cv2.CascadeClassifier("haar.xml")
name = input("Enter your name : ")
images = []
while True:
ret, frame = cap.read()
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = classifier.detectMultiScale(gray)
if len(faces):
index = np.array(faces)[:, 2:].prod(axis=1).argmax()
face = faces[index]
x, y, w, h = face
crop = gray[y:y+h, x:x+w]
crop = cv2.resize(crop, (100, 100))
cv2.imshow("Crop", crop)
cv2.imshow("Window", frame)
key = cv2.waitKey(1)
if ord('q') == 0xff & key:
break
if ord('c') == 0xff & key:
if len(faces):
images.append(crop.flatten())
count += 1
print("Captured : " + str(count))
if count >= 10:
break
X = np.array(images)
y = np.full((X.shape[0], 1), name)
data = np.hstack([y, X])
try:
old = np.load("data.npy")
except FileNotFoundError as e:
old = np.zeros([0, 10001], dtype=int)
result = np.vstack([old, data])
np.save("data.npy", result)
print(result.shape)
cap.release()
cv2.destroyAllWindows()