summaryrefslogtreecommitdiff
path: root/face-detection/02_face_training.py
diff options
context:
space:
mode:
authorSaumit Dinesan <justsaumit@protonmail.com>2023-03-18 17:10:53 +0530
committerSaumit Dinesan <justsaumit@protonmail.com>2023-03-18 17:10:53 +0530
commite65971af1c75bf02e07cedeb0f1a66dc9e1a1690 (patch)
tree569ab43c8b96cd11ba55c47be89cb9b180838bf6 /face-detection/02_face_training.py
parent0cacd048bf88549de4385d5bec3ed6edcf67859c (diff)
Adding face capture,training,recognition script
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))))