-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraining.py
More file actions
54 lines (43 loc) · 2 KB
/
Training.py
File metadata and controls
54 lines (43 loc) · 2 KB
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
''''
Training Multiple Faces stored on a DataBase:
==> Each face should have a unique numeric integer ID as 1, 2, 3, etc
==> LBPH computed model will be saved on trainer/ directory. (if it does not exist, pls create one)
==> for using PIL, install pillow library with "pip install pillow"
Based on original code by Anirban Kar: https://github.com/thecodacus/Face-Recognition
Developed by Marcelo Rovai - MJRoBot.org @ 21Feb18
'''
import cv2
import numpy as np
from PIL import Image
import os
# Path for face image database
pathData = 'dataset'
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml");
# function to get the images and label data
def getImagesAndLabels(main_path):
faceSamples = []
ids = []
folder_index = 0
for user_folder in os.listdir(main_path):
user_folder_path = os.path.join(main_path, user_folder)
if os.path.isdir(user_folder_path):
for image_name in os.listdir(user_folder_path):
image_path = os.path.join(user_folder_path, image_name)
if os.path.isfile(image_path):
PIL_img = Image.open(image_path).convert('L')
img_numpy = np.array(PIL_img, 'uint8')
id = folder_index
faces = detector.detectMultiScale(img_numpy)
for (x, y, w, h) in faces:
faceSamples.append(img_numpy[y:y + h, x:x + w])
ids.append(id)
folder_index += 1
return faceSamples, ids
print ("\n [INFO] Training faces. It will take a few seconds. Wait ...")
faces,ids = getImagesAndLabels(pathData)
recognizer.train(faces, np.array(ids))
# Save the model into trainer/trainer.yml
recognizer.write('trainer/trainer.yml') # recognizer.save() worked on Mac, but not on Pi
# Print the numer of faces trained and end program
print("\n [INFO] {0} faces trained. Exiting Program".format(len(np.unique(ids))))