-
Notifications
You must be signed in to change notification settings - Fork 0
/
capturer.py
70 lines (54 loc) · 1.98 KB
/
capturer.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
62
63
64
65
66
67
68
69
# include standard modules
import argparse
import cv2
import time, os
face_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_alt.xml')
cap = cv2.VideoCapture(0)
width = cap.get(3)
height = cap.get(4)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
try:
os.mkdir("images")
except FileExistsError:
pass
img_dir = os.path.join(BASE_DIR, "images")
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--name", help="set person name")
parser.add_argument("-c", "--captures", help="set number of captures")
args = parser.parse_args()
person = args.name
captures = int(args.captures)
if (person and captures):
print("Person name : %s" % person)
i = 1
while(i<=captures):
ret,frame = cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray,scaleFactor=1.3, minNeighbors=3)
#eyes = eye_cascade.detectMultiScale(gray,scaleFactor=1.3, minNeighbors=10)
for(xf, yf, wf, hf) in faces:
i += 1
roi_gray = gray[yf:yf+hf, xf:xf+wf]
img_item = str(int(time.time()*1000)) + ".png"
try:
os.mkdir(img_dir + "/" + person)
except FileExistsError:
pass
cv2.imwrite(img_dir + "/" + person + "/" + img_item, frame)
time.sleep(0.25)
fcolor = (255,0,0) #BGR
stroke = 2
cv2.rectangle(frame,(xf,yf),(xf+wf,yf+hf), fcolor, stroke)
font = cv2.FONT_HERSHEY_DUPLEX
name = "Capturing data for " + person
color = (0,0,255)
stroke = 1
cv2.putText(frame, name, (int(width/3),yf), font, 0.5, color, stroke, cv2.LINE_AA)
cv2.imshow('Face Recognition',frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
else:
print("Please Enter person name using \"-n <your name> -c <sample size>\" parameters")
exit(0)