updated trainer

This commit is contained in:
HeshamTB 2018-11-23 13:02:32 +00:00
parent 202a3e81d8
commit 9c6e3999b5
2 changed files with 24 additions and 14 deletions

View File

@ -98,15 +98,15 @@ def recognize(image):
subjects = ['','Hesham','Saeed'] subjects = ['','Hesham','Saeed']
face_recognizer = cv2.createLBPHFaceRecognizer() face_recognizer = cv2.createLBPHFaceRecognizer()
face_recognizer.load('faces/Hesham-Saeed2') face_recognizer.load('faces/m')
img = image.copy() img = image.copy()
label= face_recognizer.predict(img) label= face_recognizer.predict(img)
print(label) print(label)
if label[0] == 1 and int(label[1]) < 120: if label[0] == 1 and int(label[1]) < 120:
return 'Hesham' return 'Hesham'
elif label[0] == 2 and int(label[1]) < 90: elif label[0] == 2 and int(label[1]) < 120:
return 'Saeed' return 'Safwan'
elif label[0] == 3 and int(label[1]) < 120: elif label[0] == 4 and int(label[1]) < 120:
return 'Hesham2' return 'Hesham2'
else: else:
return 'Unknown' return 'Unknown'

View File

@ -7,12 +7,14 @@ import os
import sys import sys
picCount = 0 picCount = 0
new = False
def start(): def start():
print('Starting training..') print('Starting training..')
label = int(input('Label (Integer): ')) label = int(input('Label (Integer): '))
picCount = int(input('Number of Photos: ')) picCount = int(input('Number of Photos: '))
global new
new = promptNew() new = promptNew()
if new: if new:
newFileName = str(raw_input('New File name: ')) newFileName = str(raw_input('New File name: '))
@ -23,8 +25,8 @@ def start():
oldFileName = str(raw_input('Old File name: ')) oldFileName = str(raw_input('Old File name: '))
newFileName = str(raw_input('New File name: ')) newFileName = str(raw_input('New File name: '))
clearTrainFolder() clearTrainFolder()
print('Press any key to start capture') print('Press any key to start capture..')
cv2.waitKey(500) dum = input('')
capture(picCount) capture(picCount)
addPerson(label, newFileName, oldFileName) addPerson(label, newFileName, oldFileName)
@ -39,7 +41,12 @@ def addPerson(label, newFileName, oldFileName):
#faceFilePath = '/faces/hesham-saeed2' #faceFilePath = '/faces/hesham-saeed2'
faceFile = 'faces/'+ oldFileName faceFile = 'faces/'+ oldFileName
face_recognizer = cv2.createLBPHFaceRecognizer() face_recognizer = cv2.createLBPHFaceRecognizer()
face_recognizer.load(faceFile) global new
if new:
print 'new file. skipping load function'
else:
print 'loading old file'
face_recognizer.load(faceFile)
except: except:
print('[ Error ] Problem in init/loading LBPHfacerecognizer') print('[ Error ] Problem in init/loading LBPHfacerecognizer')
sys.exit() sys.exit()
@ -57,8 +64,11 @@ def addPerson(label, newFileName, oldFileName):
for (x,y,w,h) in faces: for (x,y,w,h) in faces:
images.append(gray[y:y+h, x:x+w]) images.append(gray[y:y+h, x:x+w])
labels.append(label) labels.append(label)
print('Learned '+ image_name) print('Learning '+ image_name)
face_recognizer.update(images, np.array(labels)) if new:
face_recognizer.train(images, np.array(labels))
else:
face_recognizer.update(images, np.array(labels))
face_recognizer.save('faces/'+newFileName) face_recognizer.save('faces/'+newFileName)
print('Updated and saved file in faces/'+newFileName) print('Updated and saved file in faces/'+newFileName)
@ -84,11 +94,11 @@ def capture(count):
print('Done!') print('Done!')
def promptNew(): def promptNew():
new = raw_input('Make new File? (y/n): ') filemode = raw_input('Make new File? (y/n): ')
if new == 'y': if filemode == 'y':
return True return True
elif new == 'n': elif filemode == 'n':
return = False return False
else: else:
print('incorrect input') print('incorrect input')
promptNew() promptNew()