FaceRecognition-python/trainer.py

107 lines
2.6 KiB
Python
Raw Normal View History

2018-11-22 00:34:43 +01:00
from time import sleep
from io import BytesIO
2018-11-22 11:14:28 +01:00
from picamera import PiCamera
2018-11-22 00:34:43 +01:00
import cv2
import numpy as np
2018-11-22 06:41:03 +01:00
import os
2018-11-22 11:14:28 +01:00
import sys
2018-11-22 00:34:43 +01:00
2018-11-23 14:02:32 +01:00
picCount = 0
new = False
2018-11-22 07:14:31 +01:00
def start():
print('Starting training..')
label = int(input('Label (Integer): '))
picCount = int(input('Number of Photos: '))
2018-11-23 14:02:32 +01:00
global new
2018-11-22 11:14:28 +01:00
new = promptNew()
if new:
newFileName = str(raw_input('New File name: '))
oldFileName = newFileName
else:
print('Found in faces:')
print(os.listdir('faces/'))
oldFileName = str(raw_input('Old File name: '))
newFileName = str(raw_input('New File name: '))
2018-11-22 07:14:31 +01:00
clearTrainFolder()
2018-11-23 14:02:32 +01:00
print('Press any key to start capture..')
dum = input('')
2018-11-22 11:14:28 +01:00
capture(picCount)
addPerson(label, newFileName, oldFileName)
2018-11-22 07:14:31 +01:00
def clearTrainFolder():
print('clearing train folder')
filelist = [ f for f in os.listdir('train/') if f.endswith(".jpg") ]
for f in filelist:
os.remove(os.path.join('train/', f))
def addPerson(label, newFileName, oldFileName):
2018-11-22 06:41:03 +01:00
try:
#faceFilePath = '/faces/hesham-saeed2'
faceFile = 'faces/'+ oldFileName
face_recognizer = cv2.createLBPHFaceRecognizer()
2018-11-23 14:02:32 +01:00
global new
if new:
print 'new file. skipping load function'
else:
print 'loading old file'
face_recognizer.load(faceFile)
2018-11-22 06:41:03 +01:00
except:
print('[ Error ] Problem in init/loading LBPHfacerecognizer')
2018-11-22 11:14:28 +01:00
sys.exit()
2018-11-22 06:41:03 +01:00
images = []
labels = []
2018-11-22 11:14:28 +01:00
image_names = os.listdir('train/')
2018-11-22 06:41:03 +01:00
for image_name in image_names:
if image_name.startswith('.'):
continue;
2018-11-22 11:14:28 +01:00
image_path = 'train/' + image_name
2018-11-22 06:41:03 +01:00
image = cv2.imread(image_path)
2018-11-22 00:34:43 +01:00
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
2018-11-22 06:41:03 +01:00
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)
2018-11-23 14:02:32 +01:00
print('Learning '+ image_name)
if new:
face_recognizer.train(images, np.array(labels))
else:
face_recognizer.update(images, np.array(labels))
2018-11-22 06:41:03 +01:00
face_recognizer.save('faces/'+newFileName)
2018-11-22 11:14:28 +01:00
print('Updated and saved file in faces/'+newFileName)
def capture(count):
try:
print('Initializing camera')
camera = PiCamera()
camera.resolution = (640, 480)
print('[ OK ] Camera')
sleep(2)
except:
print('[ Error ] Can not initialize PiCamera')
sys.exit()
for i in range(1, count+1):
pic = 'train/'+str(i)+'.jpg'
camera.capture(pic)
photo = cv2.imread(pic,1)
cv2.imshow('Photo',photo)
print('Captured '+str(i)+'\nPath '+ pic)
cv2.waitKey(100)
sleep(1)
cv2.destroyAllWindows()
print('Done!')
2018-11-22 01:56:09 +01:00
2018-11-22 11:14:28 +01:00
def promptNew():
2018-11-23 14:02:32 +01:00
filemode = raw_input('Make new File? (y/n): ')
if filemode == 'y':
2018-11-22 11:14:28 +01:00
return True
2018-11-23 14:02:32 +01:00
elif filemode == 'n':
return False
2018-11-22 11:14:28 +01:00
else:
print('incorrect input')
promptNew()
2018-11-22 06:41:03 +01:00
2018-11-22 11:14:28 +01:00
start()