-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrainer.py
More file actions
34 lines (27 loc) · 971 Bytes
/
trainer.py
File metadata and controls
34 lines (27 loc) · 971 Bytes
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
import cv2
import os
import numpy as np
data_path = 'training_data'
people = [person for person in os.listdir(data_path) if os.path.isdir(os.path.join(data_path, person))]
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
labels = []
faces = []
label_id = 0
label_map = {}
for person in people:
person_path = os.path.join(data_path, person)
label_map[label_id] = person
for image_name in os.listdir(person_path):
img_path = os.path.join(person_path, image_name)
image = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
if image is None:
continue
if image.shape[-1] == 4:
image = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
else:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces.append(image)
labels.append(label_id)
label_id += 1
face_recognizer.train(faces, np.array(labels))
face_recognizer.write('trained_model.yml')