2018-11-22 00:34:43 +01:00
|
|
|
from time import sleep
|
|
|
|
from io import BytesIO
|
|
|
|
import cv2
|
|
|
|
import numpy as np
|
2018-11-22 06:41:03 +01:00
|
|
|
import os
|
2018-11-22 07:14:31 +01:00
|
|
|
import takePhotos;
|
2018-11-22 00:34:43 +01:00
|
|
|
|
2018-11-22 07:14:31 +01:00
|
|
|
picCount = 0
|
|
|
|
|
|
|
|
def start():
|
|
|
|
print('Starting training..')
|
|
|
|
label = int(input('Label (Integer): '))
|
|
|
|
picCount = int(input('Number of Photos: '))
|
|
|
|
clearTrainFolder()
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
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)
|
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)
|
|
|
|
print('Added '+ image_name)
|
|
|
|
face_recognizer.update(images, np.array(labels))
|
|
|
|
face_recognizer.save('faces/'+newFileName)
|
2018-11-22 01:56:09 +01:00
|
|
|
|
2018-11-22 06:41:03 +01:00
|
|
|
|
|
|
|
addPerson(3,'train/','test1','Hesham-Saeed2')
|