-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-data-set.py
executable file
·47 lines (34 loc) · 1 KB
/
create-data-set.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
#!/usr/bin/env python
import sys
import cv2
if len(sys.argv) < 3:
print sys.argv[0] + ' <user-id> <data-set-dir-path>'
exit(1)
CASCADE_PATH = "haarcascade_frontalface_default.xml"
N_OF_SAMPLES = 500
user_id = sys.argv[1]
data_set_path = sys.argv[2]
cam = cv2.VideoCapture(1)
detector = cv2.CascadeClassifier(CASCADE_PATH)
sample_num = 0
while(True):
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = 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)
# incrementing sample number
sample_num = sample_num + 1
# saving the captured face in the dataset folder
cv2.imwrite(data_set_path + '/user.' + user_id + '.' + str(sample_num) +
'.jpg', gray[y:y+h, x:x+w])
cv2.imshow('frame', img)
#wait for 100 miliseconds
if cv2.waitKey(100) & 0xFF == ord('q'):
break
# break if the sample number is morethan 20
elif sample_num > N_OF_SAMPLES:
break
cam.release()
cv2.destroyAllWindows()
print 'Finish'