Ejemplo n.º 1
0
def tune_network(folder_uncompress,  zip_file, clf):
    """
    Train a new neural model with the zip file provided
    :param folder_uncompress:
    :param zip_file:
    :param clf:
    :return:
    """
    log.debug("tune_network | Starting tuning phase ...")
    dataset = retrieve_dataset(folder_uncompress,  zip_file, clf)

    if dataset is None:
        return Response(error="ERROR DURING LOADING DAT", description="Seems that the dataset is not valid").__dict__

    else:
        timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
        neural_model_file, elapsed_time = clf.tuning(
            dataset["X"], dataset["Y"], timestamp)

        response = Response(status="OK", data=neural_model_file)
        response.description = "Model succesfully trained! | {}".format(
            time.strftime("%H:%M:%S.%f", time.gmtime(elapsed_time)))
        log.debug("train_network | Tuning phase finihsed! | {}".format(
            response.description))

        return response.__dict__
Ejemplo n.º 2
0
def predict():
    """
    Load the image using the HTML page and predict who is
    :return:
    """
    # check if the post request has the file part
    if 'file' not in request.files or request.files['file'].filename == '':
        flash('No file choosed :/', category="error")
        log.warning("predict_api | No file choosed!")
        return redirect(request.url)  # Return to HTML page [GET]
    file = request.files['file']
    treshold = request.form.get('treshold')
    log.debug("Recived file [{}] and treshold [{}]".format(file, treshold))
    if treshold is None or len(treshold) == 0:
        log.warning("Treshold not provided, using 45 as default")
        treshold = 45
    else:
        try:
            treshold = int(treshold)
        except ValueError:
            log.error("Unable to convert treshold")
            response = Response()
            response.error = "UNABLE_CAST_I  NT"
            response.description = "Treshold is not an integer!"
            response.status = "KO"
            return jsonify(response=response.__dict__)
    if not 0 <= treshold <= 100:
        log.error("Treshold wrong value")
        response = Response()
        response.error = "TRESHOLD_ERROR_VALUE"
        response.description = "Treshold have to be greater than 0 and lesser than 100!"
        response.status = "KO"
        return jsonify(response=response.__dict__)

    treshold /= 100

    log.debug("Recived file {} and treshold {}".format(file, treshold))
    filename = secure_filename(file.filename)
    img_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
    file.save(img_path)
    return jsonify(response=predict_image(img_path, clf, TMP_UPLOAD_PREDICTION,
                                          TMP_UNKNOWN, treshold))
Ejemplo n.º 3
0
def login():
    if request.method == 'GET':
        return render_template("login.html")

    email = request.form['email']
    password = request.form['password']
    log.debug("Password in input -> {}".format(password))
    # name (administrator) is not managed, only mail and psw will be used for login validation
    admin = Administrator("administrator", email, password)
    if not admin.init_redis_connection():
        log.error("Unable to connect to redis-db!")
        response = Response(status="KO", error="UNABLE_CONNECT_REDIS_DB")
        response.description = "Seems that the DB is not reachable!"
        return jsonify(response=response.__dict__)
    authenticated = admin.verify_login(password)
    admin.redis_client.close()
    if not authenticated:
        log.error("Password is not valid!")
        response = Response(status="KO", error="PASSWORD_NOT_VALID")
        response.description = "The password inserted is not valid!"
        return jsonify(response=response.__dict__)
    user = User()
    user.id = email
    login_user(user)
    log.debug("Logged in!")
    return redirect('/train')
Ejemplo n.º 4
0
def train_network(folder_uncompress, zip_file, clf, DETECTION_MODEL, JITTER, encoding_models):
    """
    Train a new neural model with the zip file provided
    :param folder_uncompress:
    :param zip_file:
    :param clf:
    :return:
    """

    log.debug("train_network | Starting training phase ...")
    dataset = retrieve_dataset(folder_uncompress, zip_file, clf, DETECTION_MODEL, JITTER, encoding_models)
    if dataset is None:
        return Response(error="ERROR DURING LOADING DAT", description="Seems that the dataset is not valid").__dict__

    timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
    neural_model_file, _ = clf.train(dataset["X"], dataset["Y"], timestamp)

    response = Response(status="OK", data=neural_model_file)
    response.description = "Model successfully trained!"
    log.debug("train_network | Tuning phase finished! | {}".format(
        response.description))

    return response.__dict__
Ejemplo n.º 5
0
def predict_image(img_path, clf, PREDICTION_PATH, TMP_UNKNOWN, treshold=45):
    """

    :param TMP_UNKNOWN:
    :param treshold:
    :param PREDICTION_PATH: global variable where image recognized are saved
    :param img_path: image that have to be predicted
    :param clf: classifier in charge to predict the image
    :return: Response dictionary jsonizable
    """
    response = Response()
    if clf is None:
        log.error("predict_image | FATAL | Classifier is None!")
        prediction = None
    else:
        log.debug("predict_image | Predicting {}".format(img_path))
        prediction = clf.predict(img_path, treshold)
        log.debug("predict_image | Result: {}".format(prediction))

    # Manage error
    if prediction is None:
        response.error = "CLASSIFIER_NOT_LOADED"
        response.description = "Classifier is None | Training mandatory"
        response.status = "KO"
        log.error("predict_image | Seems that the classifier is not loaded :/")
    # return Response(status="KO", description="CLASSIFIER_NOT_LOADED", data=prediction).__dict__

    elif isinstance(prediction, int):
        response.status = "KO"
        if prediction == -1:
            response.error = "FACE_NOT_RECOGNIZED"
            response.description = "Seems that this face is related to nobody that i've seen before ..."
            response.status = "KO"
            log.error("predict_image | Face not recognized ...")

            # Saving unkown faces for future clustering
            now = str(datetime.now())[:23]
            now = now.replace(":", "_")
            now = now.replace(".", "_")
            head, tail = os.path.split(img_path)
            filename, file_extension = os.path.splitext(tail)
            filename = filename + "__"+now+file_extension
            filename = os.path.join(TMP_UNKNOWN, filename)
            log.info("Image not recognized, saving it in: {}".format(filename))
            shutil.copy(img_path, filename)

        elif prediction == -2:
            response.error = "FILE_NOT_VALID"
            response.description = "Seems that the file that you have tried to upload is not valid ..."
            log.error(
                "predict_image |Seems that the file that you have tried to upload is not valid ...")

        # Manage no face found
        elif prediction == -3:
            log.error(
                "predict_image | Seems that this face is related to nobody that i've seen before ...")
            response.error = "FACE_NOT_FOUND"
            response.description = "No face found in the given image ..."
    # Manage success
    elif "predictions" in prediction and isinstance(prediction['predictions'], list):
        # Be sure to don't overwrite an existing image
        exists = True
        while exists:
            # img_name = os.path.join(PREDICTION_PATH, random_string() + ".png"
            img_name = random_string() + ".png"
            img_file_name = os.path.join(PREDICTION_PATH, img_name)
            if not os.path.exists(img_file_name):
                exists = False

        log.debug("predict_image | Generated a random name: {}".format(img_name))
        log.debug("predict_image | Printing prediction on image ...")
        print_prediction_on_image(
            img_path, prediction["predictions"], img_file_name)

        # response.error = "FACE_FOUND"
        # response.description = "/uploads/"+img_name
        # response.status = "OK"
        # response.data = prediction["predictions"]
        return Response(status="OK", description="/uploads/" + img_name, data=prediction).__dict__

    return response.__dict__
Ejemplo n.º 6
0
def predict():
    """
    Load the image using the HTML page and predict who is
    :return:
    """
    # check if the post request has the file part
    if 'file' not in request.files or request.files['file'].filename == '':
        # flash('No file choose :/', category="error")
        log.warning("predict_api | No file choose!")
        response = Response(status="KO", error="NO_FILE_IN_REQUEST")
        response.description = "You have sent a request without the photo to predict :/"
        return jsonify(response=response.__dict__)
        # return redirect(request.url)  # Return to HTML page [GET]
    file = request.files['file']
    threshold = request.form.get('threshold')
    log.debug("Received file [{}] and threshold [{}]".format(file, threshold))
    if threshold is None or len(threshold) == 0:
        log.warning("Threshold not provided")
        response = Response(status="KO", error="THRESHOLD_NOT_PROVIDED")
        response.description = "You have sent a request without the `threshold` parameter :/"
        return jsonify(response=response.__dict__)

    try:
        threshold = int(threshold)
    except ValueError:
        log.error("Unable to convert threshold")
        response = Response(status="KO", error="UNABLE_CAST_INT")
        response.description = "Threshold is not an integer!"
        return jsonify(response=response.__dict__)
    if not 0 <= threshold <= 100:
        log.error("Threshold wrong value")
        response = Response(status="KO", error="THRESHOLD_ERROR_VALUE")
        response.description = "Threshold have to be greater than 0 and lesser than 100!"
        return jsonify(response=response.__dict__)

    threshold /= 100

    log.debug("Received file {} and threshold {}".format(file, threshold))
    filename = secure_filename(file.filename)
    img_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
    file.save(img_path)
    return jsonify(
        response=predict_image(img_path, clf, TMP_UPLOAD_PREDICTION, TMP_UNKNOWN, DETECTION_MODEL, JITTER,
                               ENCODING_MODELS, threshold))