From 9f75eda8a58ae3fb3c925578521c909d91f1db26 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Thu, 22 Nov 2018 03:56:09 +0300 Subject: [PATCH] to be tested --- FaceRecognition-python.pyproj | 1 + Trainer.py | 17 ++++++++++------- main.py | 36 +++++++++++++++-------------------- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/FaceRecognition-python.pyproj b/FaceRecognition-python.pyproj index 59872e9..050cd6e 100644 --- a/FaceRecognition-python.pyproj +++ b/FaceRecognition-python.pyproj @@ -20,6 +20,7 @@ + \ No newline at end of file diff --git a/Trainer.py b/Trainer.py index 51bf47f..f4a80fe 100644 --- a/Trainer.py +++ b/Trainer.py @@ -3,26 +3,29 @@ from io import BytesIO import cv2 import numpy as np -def train(label, path): +def addPerson(label, ImagesPath, newFileName, oldFileName): try: + #faceFilePath = '/faces/hesham-saeed2' + faceFile = '/faces/'+ oldFileName face_recognizer = cv2.createLBPHFaceRecognizer() - face_recognizer.load('/faces/hesham-saeed2') + face_recognizer.load(faceFile) except: print('[ Error ] Problem in init/loading LBPHfacerecognizer') images = [] labels = [] - image_names = os.listdir(path) + image_names = os.listdir(ImagesPath) for image_name in image_names: if image_name.startswith('.'): continue; image_path = train_path + image_name - image = cv2.imread(image_path) + 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 + face_recognizer.train(images, np.array(labels)) + face_recognizer.save('/faces/'+newFileName) + + \ No newline at end of file diff --git a/main.py b/main.py index fc2761a..fc4d449 100644 --- a/main.py +++ b/main.py @@ -12,11 +12,8 @@ try: except Exception as ex: print('[ Error ] some depandincies are missing\n'+ str(ex.args)) - def init(): go = True - #OpenShifter() - #if Sensors(): print('Initilizing Camera and cascade components..') try: camera = PiCamera() @@ -115,25 +112,22 @@ def recognize(image): return 'Saeed' else: return 'Unknown' - -def Sensors(): - - while 1: - try: - line = ser.readline() - print(line) - if 'A' in line: - return True - else: - print('Sensors not OK..') - - except: - print('[ Error ] Can not connect with arduino. Retrying..') def OpenShifter(): - ser = serial.Serial('/dev/ttyUSB1',9600) - ser.open() - ser.write('O') - ser.close() + try: + ser = serial.Serial('/dev/ttyUSB1',9600) + ser.open() + ser.write('O') + ser.close() + except Exception as ex: + print('[ Error ] Can not connect to Arduino at /dev/ttyUSB1 .. \n Trying /dev/ttyUSB0') + try: + ser = serial.Serial('/dev/ttyUSB0',9600) + ser.open() + ser.write('O') + ser.close() + except Exception as ex: + print('[ Error ] Can not connect to Arduino at /dev/ttyUSB0') + init()