From e65971af1c75bf02e07cedeb0f1a66dc9e1a1690 Mon Sep 17 00:00:00 2001 From: Saumit Dinesan Date: Sat, 18 Mar 2023 17:10:53 +0530 Subject: Adding face capture,training,recognition script --- face-detection/02_face_training.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 face-detection/02_face_training.py (limited to 'face-detection/02_face_training.py') 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)))) -- cgit v1.2.3