diff --git a/.gitignore b/.gitignore index 0b05877..c286919 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,9 @@ test-img-vis-kernel/* **.rej patches/* +## Webserver:Guess +human_guess.csv + ## 50States10K dataset # Download it from https://drive.google.com/file/d/1Y8eqx1Uy8kuRP4BNmTCVvrNbCxx6RoiP/view # Edit and run src/scripts/generate_dataset.sh to generate train and test set diff --git a/src/webserver/app.py b/src/webserver/app.py index 04cc037..4dd7cd0 100644 --- a/src/webserver/app.py +++ b/src/webserver/app.py @@ -1,12 +1,22 @@ #!/usr/bin/python3 -from flask import Flask, render_template, request +from flask import Flask, render_template, request, session import subprocess +import secrets import json +import guess + MAGIC_NUMBER = 2051 app = Flask(__name__) +app.register_blueprint(guess.bp) + + +app.config['SECRET_KEY'] = secrets.token_hex() +app.config['SESSION_TYPE'] = 'memchached' + + @app.route("/") def index(): return render_template("index.html") diff --git a/src/webserver/guess.py b/src/webserver/guess.py new file mode 100644 index 0000000..6e133cd --- /dev/null +++ b/src/webserver/guess.py @@ -0,0 +1,81 @@ +import os +import random +import uuid +import datetime + +from flask import (Blueprint, Flask, flash, make_response, redirect, + render_template, request, send_from_directory, session, + url_for) + +bp = Blueprint('guess', __name__, url_prefix="/guess") + +# Define some constants +IMAGE_DIR = os.path.join(os.getcwd(),'data/50States10K/test') +CATEGORIES = os.listdir(IMAGE_DIR) +CATEGORIES.sort() +RESULTS_FILE = 'human_guess.csv' +HINT = False # Show answer in console + + +if not os.path.exists(RESULTS_FILE): + with open(RESULTS_FILE, "w") as f: + f.write("label,guess,corresponding,filename,uuid,datetime") + + +def ensure_cookies(session): + if "reussis" not in session: + session["reussis"] = 0 + if "essayes" not in session: + session["essayes"] = 0 + if "uuid" not in session: + session["uuid"] = str(uuid.uuid4()) # Session uuid, we don't need it to be unique but just not too common + +def give_image(session): # Store an image path in the session cookie + category = random.choice(CATEGORIES) + image = random.choice(os.listdir(os.path.join(IMAGE_DIR, category))) + session["image_filename"] = f"{category}/{image}" + if HINT: + print(category) + + + +# Define the guessing game page +@bp.route('/', methods=['GET', 'POST']) +def guess(): + ensure_cookies(session) + if request.method == 'POST': + if "image_filename" in session and "guess" in request.form: + real_filename = (session["image_filename"].split("/")[0], session["image_filename"].split("/")[1]) + + # Check if the guess is correct + result = (request.form['guess'] == real_filename[0]) + if result: + session["reussis"] += 1 + flash("C'était la bonne réponse !") + else: + flash(f"La bonne réponse était '{real_filename[0]}'") + session["essayes"] += 1 + + # Save the result to file + with open(RESULTS_FILE, 'a') as f: + f.write(f'{real_filename[0]},{request.form["guess"]},{result},{real_filename[1]},{session["uuid"]},{datetime.datetime.now()}\n') + + elif "guess" not in request.form: + flash("Veuillez faire au moins un choix") + return redirect("/guess") + else: + return redirect("/guess") + + # Display the guessing game page + resp = make_response(render_template('guess.html', CHOICES=CATEGORIES, session=session)) + give_image(session) + return resp + + +@bp.route("/image.png") +def get_media(): + if "image_filename" not in session: + abort(403) + else: + real_filename = (session["image_filename"].split("/")[0], session["image_filename"].split("/")[1]) + return send_from_directory(os.path.join(IMAGE_DIR, real_filename[0]), real_filename[1]) diff --git a/src/webserver/static/guess.css b/src/webserver/static/guess.css new file mode 100644 index 0000000..da26747 --- /dev/null +++ b/src/webserver/static/guess.css @@ -0,0 +1,98 @@ +form { + margin-top: 1em; +} + +ol { + margin: 0; + padding: 0; + list-style-type: none; +} + +li { + margin: 0.5em 0; +} + +input[type="submit"]#guess-button { + background-color: #4CAF50; + border: none; + color: white; + padding: 0.5em; + text-align: center; + text-decoration: none; + display: inline-block; + font-size: 1em; + margin-top: 1em; + cursor: pointer; + border-radius: 5px; +} + +input[type="radio"] { + margin-right: 0.5em; +} + +img { + max-width: 100%; + height: auto; + margin-top: 1em; +} + +#labels { + overflow: scroll; + height: 80vh; + border-color: black; + border-width: 1px; + border-style: solid; + max-height: 70vh; +} + +#guess-image { + width: 40vw; + height: 40vw; + border-style: solid; + border-width: 1px; + border-radius: 3px; +} + +.flash { + padding: 1em 0; + background: #cae6f6; + border: 1px solid #377ba8; + border-radius: 3px; + text-align: center; + margin: 1em 0; + } + +#guess-body { + display:flex; + justify-content:space-between; +} + +#guess-form { + background-color: white; + align-items: center; + display: grid; + position: fixed; + right: 5vw; +} + +#left-div { + width: 40vw; + margin: 0 5vw; + margin-top: 10vh; + text-align: center; +} + +#us-map { + position: fixed; + height: 45vh; + bottom: -43vh; + transition: ease 1s; + border-style: solid; + border-width: 2px; +} + + +#us-map:hover { + bottom: -2px; + transition: ease 1s; +} \ No newline at end of file diff --git a/src/webserver/static/style.css b/src/webserver/static/mnist.css similarity index 96% rename from src/webserver/static/style.css rename to src/webserver/static/mnist.css index 14b404c..0978111 100644 --- a/src/webserver/static/style.css +++ b/src/webserver/static/mnist.css @@ -1,4 +1,4 @@ -body { +body, #guess-body { flex-direction: row; display: flex; overflow: hidden; diff --git a/src/webserver/static/script.js b/src/webserver/static/mnist.js similarity index 100% rename from src/webserver/static/script.js rename to src/webserver/static/mnist.js diff --git a/src/webserver/static/usa_map.png b/src/webserver/static/usa_map.png new file mode 100644 index 0000000..c9fd2d3 Binary files /dev/null and b/src/webserver/static/usa_map.png differ diff --git a/src/webserver/templates/guess.html b/src/webserver/templates/guess.html new file mode 100644 index 0000000..7170358 --- /dev/null +++ b/src/webserver/templates/guess.html @@ -0,0 +1,35 @@ + + +
+