-
Notifications
You must be signed in to change notification settings - Fork 0
/
Save_snaps.py
33 lines (29 loc) · 906 Bytes
/
Save_snaps.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
import cv2, os
haar_file = 'haarcascade_frontalface_default.xml'
datasets = 'dataset'
sub_data = 'Rhea'
path = os.path.join(datasets, sub_data)
if not os.path.isdir(path):
os.mkdir(path)
(width, height) = (400, 500)
face_cascade = cv2.CascadeClassifier(haar_file)
webcam = cv2.VideoCapture(0)
count = 1
while count < 11:
print(count)
(_, im) = webcam.read()
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 4)
for (x,y,w,h) in faces:
cv2.rectangle(im,(x,y),(x+w,y+h),(255,0,0),2)
face = gray[y:y + h, x:x + w]
face_resize = cv2.resize(face, (width, height))
cv2.imwrite('%s/%s.png' % (path,count), face_resize)
count += 1
cv2.imshow('OpenCV', im)
key = cv2.waitKey(20)
if key == 27:
break
print("Dataset obtained successfully")
webcam.release()
cv2.destroyAllWindows()