Esempio n. 1
0
import pickle
import math
from data_reader import DataReader
from feature_extractor import FeatureExtractor
from sklearn.naive_bayes import GaussianNB

data_reader = DataReader(
)  # reads the images files and converts them into numpy 2D arrays
feature_extractor = FeatureExtractor(
)  # calculates the eigenfaces. Follows the fit->transform paradigm.
clf = GaussianNB(
)  # a naive bayes classifier where the individual variables are supposed to follow a gaussian distribution

# since the number of images available is relatively low (400 images),
# we'll use cross-validation to assess the performance of the face recognition system.
data = data_reader.getAllData(
    shuffle=True)  # we shuffle the data so we can do Cross-Validation
num_folds = 10
fold_length = math.floor(len(data[0]) / num_folds)
average_accuracy = 0.0  # the performance measure of the system

for k in range(num_folds):
    # get train data and test data from data
    train_data, test_data = [None, None], [None, None]
    for i in range(2):
        if k == num_folds - 1:
            train_data[i] = data[i][:k * fold_length]
            test_data[i] = data[i][k * fold_length:]

        else:
            train_data[i] = data[i][:k * fold_length] + data[i][(k + 1) *
                                                                fold_length:]