import csv
import os
from sklearn.neural_network import MLPClassifier

def load_data(fname):
    '''загрузить файл с данными о позах формата csv'''
    with open(fname, 'rt') as f:
        rd = csv.reader(f,delimiter=';')
        poses = []
        poses_classes = []
        for row in rd:
            data = [float(v) for v in row[:-1]]
            data_class = int(row[-1])
            poses.append(data)
            poses_classes.append(data_class)
    return poses, poses_classes

poses = []
poses_classes = []
listdir = os.listdir('poses')
for fname in listdir:
    ps, pcs = load_data('poses/' + fname)
    poses.extend(ps)
    poses_classes.extend(pcs)

#poses, poses_classes = load_data('poses/poses.csv')
#poses2, poses_classes2 = load_data('poses_vladimir.csv')
#poses.extend(poses2)
#poses_classes.extend(poses_classes2)

cl = MLPClassifier(hidden_layer_sizes=[20,20], alpha=0.01)
cl.fit(poses, poses_classes)
predicted_classes = cl.predict(poses)
#print('Ground truth:', poses_classes)
#print('Predicted:', predicted_classes)

T = 0
F = 0
for u,v in zip(poses_classes, predicted_classes):
    if u == v:
        T += 1
    else:
        F += 1
print(f'Accuracy = {T/(T+F):.3f}')
