Updated-Teted Trainer

This commit is contained in:
HeshamTB 2018-11-22 05:41:03 +00:00
parent 50bb02ab67
commit ecd7ad2c48
3 changed files with 50 additions and 40 deletions

View File

@ -2,30 +2,33 @@ from time import sleep
from io import BytesIO
import cv2
import numpy as np
import os
def addPerson(label, ImagesPath, newFileName, oldFileName):
try:
#faceFilePath = '/faces/hesham-saeed2'
faceFile = '/faces/'+ oldFileName
face_recognizer = cv2.createLBPHFaceRecognizer()
face_recognizer.load(faceFile)
except:
print('[ Error ] Problem in init/loading LBPHfacerecognizer')
images = []
labels = []
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)
try:
#faceFilePath = '/faces/hesham-saeed2'
faceFile = 'faces/'+ oldFileName
face_recognizer = cv2.createLBPHFaceRecognizer()
face_recognizer.load(faceFile)
except:
print('[ Error ] Problem in init/loading LBPHfacerecognizer')
images = []
labels = []
image_names = os.listdir(ImagesPath)
for image_name in image_names:
if image_name.startswith('.'):
continue;
image_path = ImagesPath + 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))
face_recognizer.save('/faces/'+newFileName)
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)
print('Added '+ image_name)
face_recognizer.update(images, np.array(labels))
face_recognizer.save('faces/'+newFileName)
addPerson(3,'train/','test1','Hesham-Saeed2')

30
main.py
View File

@ -47,7 +47,7 @@ def start( camera, face_cascade):
camera.capture('img.jpg')
img = cv2.imread('img.jpg',1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
i = 0;
for (x,y,w,h) in faces:
i += 1
@ -102,7 +102,7 @@ def recognize(image):
subjects = ['','Hesham','Saeed']
face_recognizer = cv2.createLBPHFaceRecognizer()
face_recognizer.load('faces/Hesham-Saeed2')
face_recognizer.load('faces/test1')
img = image.copy()
label= face_recognizer.predict(img)
print(label)
@ -110,24 +110,26 @@ def recognize(image):
return 'Hesham'
elif label[0] == 2 and int(label[1]) < 90:
return 'Saeed'
elif label[0] == 3 and int(label[1]) < 120:
return 'Hesham2'
else:
return 'Unknown'
def OpenShifter():
try:
ser = serial.Serial('/dev/ttyUSB1',9600)
ser.open()
ser.write('O')
ser.close()
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')
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()

View File

@ -3,6 +3,7 @@
import numpy as np
from picamera import PiCamera
from time import sleep
import cv2
def capture(path):
try:
@ -14,10 +15,14 @@ def capture(path):
sleep(2)
except:
print('[ Error ] Can not initialize PiCamera')
for i in range(0, choice):
for i in range(1, choice):
camera.capture(path+str(i)+'.jpg')
photo = cv2.imread(path+str(i)+'.jpg',1)
cv2.imshow('Photo',photo)
print('Captured '+str(i)+'\nPath ('+path+str(i)+'.jpg)')
cv2.waitKey(100)
sleep(1)
capture('test/')
cv2.destroyAllWindows()
print('Done!')
capture('train/')