-
Notifications
You must be signed in to change notification settings - Fork 0
/
samplegenerator.py
38 lines (29 loc) · 1008 Bytes
/
samplegenerator.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
import cv2
import os
# Create a directory named 'samples' if it doesn't exist
if not os.path.exists('samples'):
os.makedirs('samples')
cam = cv2.VideoCapture(0, cv2.CAP_DSHOW)
cam.set(3, 640)
cam.set(4, 480)
detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
face_id = input("Enter a Numeric user ID here: ")
print("Taking samples, look at the camera ....... ")
count = 0
while True:
ret, img = cam.read()
converted_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(converted_image, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
count += 1
cv2.imwrite("samples/face." + str(face_id) + '.' + str(count) + ".jpg", converted_image[y:y+h, x:x+w])
cv2.imshow('image', img)
k = cv2.waitKey(100) & 0xff
if k == 27:
break
elif count >= 10:
break
print("Samples taken. Closing the program....")
cam.release()
cv2.destroyAllWindows()