-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_detector.py
executable file
·58 lines (50 loc) · 1.67 KB
/
face_detector.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
#!/Users/albertolanaro/venv3/bin/python3
import os
import cv2
import numpy as np
import sys
import datetime
if len(sys.argv) != 3:
print("usage: ./face_detector.py <user_name> <save_face flag>")
sys.exit(1)
# -----PARAMS---------
USER = sys.argv[1]
SAVE_FACE = int(sys.argv[2])
FACES_FOLDER = 'Faces' # folder where to store the detected faces
# --------------------
if SAVE_FACE and not os.path.exists(FACES_FOLDER + '/' + USER):
os.makedirs(FACES_FOLDER + '/' + USER)
print('Detecting faces for user named', USER)
def process_frame(frame, face_classifier):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.6, 8)
return faces
def display_result(img, faces, save_results):
for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)
# if detected save face in .png
if save_results and len(faces)==1:
t_index = datetime.datetime.now()
t_index = "%s_%s_%s_%s" % (t_index.hour, t_index.minute, t_index.second, str(t_index.microsecond)[:2])
mask = img[y:y+h,x:x+w,:]
cv2.imwrite(FACES_FOLDER + '/' + USER + '/' + USER + '_' + t_index + '.png', mask)
# display results
cv2.imshow('Face detector', img)
cv2.waitKey(1)
if __name__=='__main__':
face_cascade = cv2.CascadeClassifier('/Users/albertolanaro/opencv/data/haarcascades/haarcascade_frontalface_default.xml')
# webcam
cap = cv2.VideoCapture(0)
n_img = 0
while True:
grabbed, frame = cap.read()
if grabbed:
#face + eye detection
faces = process_frame(frame, face_cascade)
if len(faces) != 0:
if SAVE_FACE:
n_img+=1
print('# saved imgs:', n_img)
else:
print('Face detected!')
display_result(frame, faces, SAVE_FACE)