def FUN_upload_image():

    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            return(redirect(url_for("FUN_root")))
        file = request.files['file']

        # if user does not select file, browser also submit a empty part without filename
        if file.filename == '':
            return(redirect(url_for("FUN_root")))

        if file and allowed_file(file.filename):
            filename = os.path.join("static/img_pool", hashlib.sha256(str(datetime.datetime.now()).encode('utf-8')).hexdigest() + secure_filename(file.filename).lower())
            file.save(filename)

            prediction_result, prediction_winner = predict(filename, local=True)
            FUN_resize_img(filename)

            # create plotly chart
            plotly_json = prediction_barchart(prediction_result, labels, names)

            return render_template("index.html", img_src = filename, 
                                                 prediction_result = prediction_result,
                                                 prediction_winner = prediction_winner,
                                                 graphJSON=plotly_json)
    return(redirect(url_for("FUN_root")))
def FUN_root():
	# Run correspoing code when the user provides the image url
	# If user chooses to upload an image instead, endpoint "/upload_image" will be invoked
    if request.method == "POST":
        img_url = request.form.get("img_url")

        prediction_result, prediction_winner = predict(img_url)

        plotly_json = prediction_barchart(prediction_result, labels, names)
        return render_template("index.html", img_src = img_url, 
                                             prediction_result = prediction_result,
                                             prediction_winner = prediction_winner,
                                             graphJSON=plotly_json)
    else:
        return render_template("index.html")
Beispiel #3
0
def FUN_fetch_image():
    if (request.method == 'GET') and (request.values.get('url')):
        #url = "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fi.ytimg.com%2Fvi%2FQGFaKLkocwQ%2Fmaxresdefault.jpg&f=1&nofb=1"
        try:
            url = request.values.get('url')
            cont = requests.get(url).content

            img = PILImage.open(BytesIO(cont))
            img.verify()
            # lib required reopening after verify()
            img = PILImage.open(BytesIO(cont))
            filename_only = hashlib.sha256(
                str(datetime.datetime.now()).encode(
                    'utf-8')).hexdigest() + "-url." + img.format.lower()
            filename = os.path.join("static/img_pool", filename_only)

            img.save(filename)

            prediction_result, prediction_winner = predict(filename,
                                                           local=True)
            #FUN_resize_img(filename)

            # create plotly chart
            plotly_json = prediction_barchart(prediction_result, labels, names)

            # write prediction results into txt file for further feedback and analysis
            # feedback_file = open(filename[:-3]+"txt", "w+")
            # feedback_file.write(f'{filename_only}; {" ".join(str(x) for x in labels)}; {json.dumps(prediction_result)}')
            # feedback_file.write(f'{filename_only};{json.dumps(prediction_result)}')
            # feedback_file.close()

            return render_template("index.jinja2",
                                   img_src=filename,
                                   prediction_result=prediction_result,
                                   prediction_winner=prediction_winner,
                                   graphJSON=plotly_json,
                                   labels=labels)
        except Exception:
            print('Error fetching of saving image from ' + url)
            return render_template("error.jinja2"), 500

    return (redirect(url_for("FUN_root")))
Beispiel #4
0
def FUN_upload_image():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            return (redirect(url_for("FUN_root")))
        file = request.files['file']

        # if user does not select file, browser also submit a empty part without filename
        if file.filename == '':
            return (redirect(url_for("FUN_root")))

        if file and allowed_file(file.filename):
            filename_only = hashlib.sha256(
                str(datetime.datetime.now()).encode('utf-8')).hexdigest(
                ) + secure_filename(file.filename).lower()
            filename = os.path.join("static/img_pool", filename_only)
            file.save(filename)

            prediction_result, prediction_winner = predict(filename,
                                                           local=True)
            #FUN_resize_img(filename)

            # create plotly chart
            plotly_json = prediction_barchart(prediction_result, labels, names)

            # write prediction results into txt file for further feedback and analysis
            # feedback_file = open(filename[:-3]+"txt", "w+")
            # feedback_file.write(f'{filename_only}; {" ".join(str(x) for x in labels)}; {json.dumps(prediction_result)}')
            # feedback_file.write(f'{filename_only};{json.dumps(prediction_result)}')
            # feedback_file.close()

            return render_template("index.jinja2",
                                   img_src=filename,
                                   prediction_result=prediction_result,
                                   prediction_winner=prediction_winner,
                                   graphJSON=plotly_json,
                                   labels=labels)
        else:
            return render_template("error.jinja2")

    return (redirect(url_for("FUN_root")))