-
Notifications
You must be signed in to change notification settings - Fork 0
/
facerec.py
69 lines (49 loc) · 1.94 KB
/
facerec.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
import cv2, os
import numpy as np
import argparse
import sys
from face_detector import FaceDetector
from training_mode import TrainingMode
from model_mode import ModelMode
ROI = None
ap = argparse.ArgumentParser()
group = ap.add_mutually_exclusive_group(required=True)
group.add_argument("--train", help="Create a new face recognition model")
group.add_argument("--update-model", help="Update an existing face recognition model")
group.add_argument("--model", help="Path for existing face recognition model")
group.add_argument("--clear", help="Clear all data (models and images)") # TODO
args = vars(ap.parse_args())
frame_class = None # Class to which every grabbed frame will be delivered
if args["train"]:
new_model_name = args["train"]
person_name = None
while person_name is None or person_name == "":
person_name = raw_input("Please enter the person's name: ")
frame_class = TrainingMode(person_name, None, new_model_name)
if args["update_model"]:
model_path = args["update_model"]
# TODO: Check if file exists
frame_class = TrainingMode(model_path, None)
if args["model"]:
model_path = args["model"] # Model file
# TODO: Check if file exists
frame_class = ModelMode(model_path)
# Face detection is needed in all modes
face_detector = FaceDetector("haarcascade_frontalface_default.xml")
cap = cv2.VideoCapture(0)
while(True): # Main loop
ret, frame = cap.read()
frame = cv2.resize(frame, (0, 0), fx = 0.5, fy = 0.5)
ROIs = [] # Array for segmeted faces
ROIs_coordinates = [] # Array for x,y coordinates of segmented faces to draw bounding boxes
faces = face_detector.detect(frame)
for (x, y, w, h) in faces:
ROIs.append(frame[y: y+h, x: x+w])
ROIs_coordinates.append((x, y))
# cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255))
frame = frame_class.consume(frame, ROIs, ROIs_coordinates, faces)
if frame_class.EXIT_FLAG:
break
cv2.imshow('frame', frame)
cap.release()
cv2.destroyAllWindows()