-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_recognizer_training.py
61 lines (42 loc) · 1.57 KB
/
face_recognizer_training.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
import cv2 as cv
import os
import numpy as np
# arrays defined for x, y of training set
features = []
labels = []
# direcory of images
people = [ 'Elon Musk', 'Robert Downey' ]
DIR = './images'
# face detection file
haar_cascade = cv.CascadeClassifier('./data/haer_file.xml')
def create_train():
for x in people:
path = os.path.join(DIR, x)
index = people.index(x)
for img in os.listdir(path):
# making the location of image
img_path = os.path.join(path, img)
# reading image from img_path
img_array = cv.imread(img_path)
if img_array is None:
continue
# converting to gray scale
gray = cv.cvtColor(img_array, cv.COLOR_BGR2GRAY)
face_rect = haar_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=9)
for (x, y, w, h) in face_rect:
faces_marked = gray[y:y+h, x:x+w]
features.append(faces_marked)
labels.append(index)
def train_model():
create_train()
X = np.array(features, dtype='object')
Y = np.array(labels)
face_recognizer = cv.face.LBPHFaceRecognizer_create()
# trainiAfflekng model
face_recognizer.train(X, Y)
print('======================= Training done =======================')
# saving model to use further
face_recognizer.save('./data/model_trained.yml')
np.save('./data/model_features.npy', X)
np.save('./data/model_labels.npy', Y)
print('======================= Files saved =======================')