diff --git a/api/Api.py b/api/Api.py index 171f8af..2d84d6b 100644 --- a/api/Api.py +++ b/api/Api.py @@ -8,7 +8,7 @@ from os.path import join as path_join from datastructure.Response import Response -from utils.util import print_prediction_on_image, random_string,remove_dir +from utils.util import print_prediction_on_image, random_string, remove_dir log = getLogger() diff --git a/conf/test.json b/conf/test.json index 5fcf24f..bd92348 100644 --- a/conf/test.json +++ b/conf/test.json @@ -23,7 +23,7 @@ "classifier": { "trainin_dir": "dataset/images/", "model_path": "dataset/model/", - "model": "model-20190518_191827.clf", + "model": "model-20190519_125204.clf", "n_neighbors": "", "knn_algo": "" }, diff --git a/datastructure/Classifier.py b/datastructure/Classifier.py index 74712aa..adff3ed 100644 --- a/datastructure/Classifier.py +++ b/datastructure/Classifier.py @@ -178,7 +178,7 @@ def init_dataset(self): return DATASET - def predict(self, X_img_path, distance_threshold=0.6): + def predict(self, X_img_path, distance_threshold=0.45): """ Recognizes faces in given image using a trained KNN classifier diff --git a/datastructure/Person.py b/datastructure/Person.py index e628985..b21f165 100644 --- a/datastructure/Person.py +++ b/datastructure/Person.py @@ -64,6 +64,6 @@ def init_dataset(self): log.error("initDataset | Found more than one face, too much for me Sir :&") # Loading the Y [target] for i in range(len(self.dataset["X"])): - log.debug("Adding name " + self.name) self.dataset["Y"].append(self.name) + log.debug("Adding {} entries for {}".format(len(self.dataset["X"]), self.name)) return diff --git a/main.py b/main.py index c503e49..b78e9dc 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,6 @@ PyRecognizer loader """ import base64 -import json import os import flask_monitoringdashboard as dashboard @@ -12,22 +11,13 @@ from api.Api import predict_image, train_network from datastructure.Classifier import Classifier -from utils.util import load_logger +from utils.util import init_main_data # ===== LOAD CONFIGURATION FILE ===== # TODO: Add argument parser for manage configuration file CONFIG_FILE = "conf/test.json" -with open(CONFIG_FILE) as f: - CFG = json.load(f) - -log = load_logger(CFG["logging"]["level"], CFG["logging"]["path"], CFG["logging"]["prefix"]) - -# TODO: Verify the presence -> create directory -# NOTE: create a directory every time that you need to use this folder -TMP_UPLOAD_PREDICTION = CFG["PyRecognizer"]["temp_upload_predict"] -TMP_UPLOAD_TRAINING = CFG["PyRecognizer"]["temp_upload_training"] -TMP_UPLOAD = CFG["PyRecognizer"]["temp_upload"] +CFG, log, TMP_UPLOAD_PREDICTION, TMP_UPLOAD_TRAINING, TMP_UPLOAD = init_main_data(CONFIG_FILE) # $(base64 /dev/urandom | head -n 1 | md5sum | awk '{print $1}') SECRET_KEY = str(base64.b64encode(bytes(os.urandom(24)))).encode() @@ -51,6 +41,7 @@ clf.model_path = CFG["classifier"]["model_path"] clf.load_classifier_from_file(CFG["classifier"]["model"]) +# TODO Add check on extension allowed_ext = ["jpg", "jpeg", "png"] @@ -73,7 +64,6 @@ def predict(): flash('No file choosed :/', category="error") return redirect(request.url) # Return to HTML page [GET] file = request.files['file'] - # TODO: Add check on extension filename = secure_filename(file.filename) img_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(img_path) diff --git a/utils/util.py b/utils/util.py index e67a330..fcb98df 100644 --- a/utils/util.py +++ b/utils/util.py @@ -2,13 +2,14 @@ """ Common method for reuse code """ - +import json import logging import os import random +import shutil import string from logging.handlers import TimedRotatingFileHandler -import shutil + from PIL import Image, ImageDraw levels = { @@ -53,6 +54,33 @@ def print_prediction_on_image(img_path, predictions, path_to_save, file_to_save) pil_image.save(os.path.join(path_to_save, file_to_save), "PNG") +def init_main_data(config_file): + """ + Parse the configuration file and return the necessary data for initalize the tool + :param config_file: + :return: + """ + with open(config_file) as f: + CFG = json.load(f) + + log = load_logger(CFG["logging"]["level"], CFG["logging"]["path"], CFG["logging"]["prefix"]) + + # TODO: Verify the presence -> create directory + # NOTE: create a directory every time that you need to use this folder + TMP_UPLOAD_PREDICTION = CFG["PyRecognizer"]["temp_upload_predict"] + TMP_UPLOAD_TRAINING = CFG["PyRecognizer"]["temp_upload_training"] + TMP_UPLOAD = CFG["PyRecognizer"]["temp_upload"] + + if not os.path.exists(TMP_UPLOAD_PREDICTION): + os.makedirs(TMP_UPLOAD_PREDICTION) + if not os.path.exists(TMP_UPLOAD_TRAINING): + os.makedirs(TMP_UPLOAD_TRAINING) + if not os.path.exists(TMP_UPLOAD): + os.makedirs(TMP_UPLOAD) + + return CFG, log, TMP_UPLOAD_PREDICTION, TMP_UPLOAD_TRAINING, TMP_UPLOAD + + def load_logger(level, path, name): """ @@ -85,7 +113,7 @@ def random_string(string_length=10): return ''.join(random.choice(letters) for i in range(string_length)) -def zip_data(file_to_zip,path): +def zip_data(file_to_zip, path): """ :param file_to_zip: @@ -99,5 +127,3 @@ def zip_data(file_to_zip,path): def remove_dir(dir): if os.path.isdir(dir): shutil.rmtree(dir) - -