import cv2, os
import numpy as np

images = []
labels = []
path = './images'

for file in os.listdir(path):
    fullname = os.path.join(path, file)

    if os.path.isdir(fullname):
        print('{} <DIR>'.format(file))

        for index in range(100):
            filename = 'images/{}/{:03d}.pgm'.format(file, index)
            print('read ' + filename)
            img = cv2.imread(filename, cv2.COLOR_BGR2GRAY)
            images.append(img)
            labels.append(int(file))
    else:
        print('{}'.format(file))

print('training...')
model = cv2.face.LBPHFaceRecognizer_create()
model.train(np.asarray(images), np.asarray(labels))
model.save('faces.data')
print('training done')