Skip to content

Commit

Permalink
- Reduce distance_threshold during predict for reduce miss-classifica…
Browse files Browse the repository at this point in the history
…tion

- Create a delegated method for init main data
  • Loading branch information
AlessioSavi committed May 19, 2019
1 parent eb71697 commit 16997c7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion api/Api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion conf/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": ""
},
Expand Down
2 changes: 1 addition & 1 deletion datastructure/Classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion datastructure/Person.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 3 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
PyRecognizer loader
"""
import base64
import json
import os

import flask_monitoringdashboard as dashboard
Expand All @@ -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()
Expand All @@ -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"]


Expand All @@ -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)
Expand Down
36 changes: 31 additions & 5 deletions utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand All @@ -99,5 +127,3 @@ def zip_data(file_to_zip,path):
def remove_dir(dir):
if os.path.isdir(dir):
shutil.rmtree(dir)


0 comments on commit 16997c7

Please sign in to comment.