From 5be9de955472322fa5d038c032800ce94d69ee3d Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Thu, 22 Nov 2018 02:34:43 +0300 Subject: [PATCH] Training file --- Trainer.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Trainer.py diff --git a/Trainer.py b/Trainer.py new file mode 100644 index 0000000..51bf47f --- /dev/null +++ b/Trainer.py @@ -0,0 +1,28 @@ +from time import sleep +from io import BytesIO +import cv2 +import numpy as np + +def train(label, path): + try: + face_recognizer = cv2.createLBPHFaceRecognizer() + face_recognizer.load('/faces/hesham-saeed2') + except: + print('[ Error ] Problem in init/loading LBPHfacerecognizer') + images = [] + labels = [] + image_names = os.listdir(path) + for image_name in image_names: + if image_name.startswith('.'): + continue; + image_path = train_path + image_name + image = cv2.imread(image_path) + gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') + faces = face_cascade.detectMultiScale(gray, 1.3, 5) + for (x,y,w,h) in faces: + images.append(gray[y:y+h, x:x+w]) + labels.append(label) + face_recognizer.train(images, np.array(labels)) + + \ No newline at end of file