-
Notifications
You must be signed in to change notification settings - Fork 3
/
trainer.py
65 lines (53 loc) · 2.04 KB
/
trainer.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
# trainer.py
# Train recognizer using captured face images
#
# Project: Face Recognition using OpenCV and Raspberry Pi
# Ref: https://www.pytorials.com/face-recognition-using-opencv-part-2/
# By: Mickey Chan @ 2019
# Import required modules
import os
import numpy as np
from PIL import Image
import cv2
import time
# Setup Classifer and create Recognizer
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
recognizer = cv2.face.createLBPHFaceRecognizer() # or LBPHFaceRecognizer_create()
# Create directory for storing trained data
baseDir = os.path.dirname(os.path.abspath(__file__))
imageDir = os.path.join(baseDir, "dataset")
recognizerDir = os.path.join(baseDir, "recognizer")
if not os.path.exists(recognizerDir):
os.makedirs(recognizerDir)
# Dataset of face data for training
yIDs = []
xFaces = []
# Walk through all captured face data
beginTime = time.time()
for root, dirs, files in os.walk(imageDir):
print(root, dirs, files)
for file in files:
print(file)
# Only process PNG and JPEG images
if file.endswith("png") or file.endswith("jpg"):
# Retrieve USER ID from directory name
path = os.path.join(root, file)
id_ = int(os.path.basename(root))
print("UID:" + str(id_))
# Convert the face image to grayscale and convert pixel data to Numpy Array
faceImage = Image.open(path).convert("L")
faceArray = np.array(faceImage, "uint8")
# Insert USER ID and face data into dataset
yIDs.append(id_)
xFaces.append(faceArray)
# Display the face image to be used for training
cv2.imshow("training", faceArray)
cv2.waitKey(10)
# Train recognizer and then save trained model
recognizer.train(xFaces, np.array(yIDs))
recognizer.save(recognizerDir + "/trainingData.yml")
# Clean up
cv2.destroyAllWindows()
print("DONE")
elapsedTime = round(time.time() - beginTime, 4)
print("Elapsed time: " + str(elapsedTime) + "s")