summaryrefslogtreecommitdiff
path: root/face-detection/02_face_training.py
diff options
context:
space:
mode:
Diffstat (limited to 'face-detection/02_face_training.py')
-rw-r--r--face-detection/02_face_training.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/face-detection/02_face_training.py b/face-detection/02_face_training.py
new file mode 100644
index 0000000..938761b
--- /dev/null
+++ b/face-detection/02_face_training.py
@@ -0,0 +1,31 @@
+import cv2
+import numpy as np
+from PIL import Image
+import os
+
+path = 'dataset'
+# Using LBPH(Local Binary Patterns Histograms) recognizer
+recognizer = cv2.face.LBPHFaceRecognizer_create()
+detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml");
+# function to get the images and label data
+def getImagesAndLabels(path):
+ imagePaths = [os.path.join(path,f) for f in os.listdir(path)]
+ faceSamples=[]
+ ids = []
+ for imagePath in imagePaths:
+ PIL_img = Image.open(imagePath).convert('L') # grayscale
+ img_numpy = np.array(PIL_img,'uint8')
+ id = int(os.path.split(imagePath)[-1].split(".")[1])
+ 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)
+ return faceSamples,ids
+print ("\n [INFO] Training faces. It will take a few seconds. Wait ...")
+#returns two arrays faces and ids
+faces,ids = getImagesAndLabels(path)
+recognizer.train(faces, np.array(ids))
+#save the model into trainer/trainer.yml
+recognizer.write('trainer/trainer.yml')
+# Print the numer of faces trained and end program
+print("\n [INFO] {0} faces trained. Exiting Program".format(len(np.unique(ids))))