| import os |
| import shutil |
| import uuid |
| import logging |
|
|
| from flask import Flask, redirect, url_for, request, flash, session |
| from flask import render_template |
| from flask import send_file |
|
|
| logger = logging.getLogger(__name__) |
| logging.basicConfig(level=logging.INFO) |
| app = Flask(__name__) |
| app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 |
| app.config["SECRET_KEY"] = '#thaistartsfirst!' |
| app.config["IMAGES"] = 'images' |
| app.config["OUT"] = "anno" |
| app.config["LABELS"] = [] |
| app.config["HEAD"] = 0 |
| app.config["SESSION_PERMANENT"] = False |
| try: |
| os.mkdir(app.config["OUT"]) |
| os.mkdir(app.config["IMAGES"]) |
| except: |
| pass |
|
|
|
|
| @app.route('/', methods=['GET', 'POST']) |
| def index(): |
| user_id = session.get('_id') |
| if user_id is None: |
| user_id = uuid.uuid4() |
| session['_id'] = user_id |
| logger.info(user_id) |
| with open(f"{app.config['OUT']}/{user_id}.csv", 'w') as f: |
| f.write("image,id,name,xMin,xMax,yMin,yMax\n") |
|
|
| if request.method == 'POST': |
| if 'file' not in request.files: |
| flash('No files selected') |
| return redirect('/') |
| img_folder = f'{app.config["IMAGES"]}/{user_id}' |
| try: |
| os.mkdir(img_folder) |
| except: |
| logger.info('user already has an active session') |
| files = request.files.getlist("file") |
| filenames = [] |
| for f in files: |
| f.save(os.path.join(img_folder, f.filename)) |
| filenames.append(f.filename) |
| app.config["FILES"] = filenames |
| logger.info(app.config["FILES"], app.config["HEAD"]) |
| return redirect('/tagger', code=302) |
| else: |
| return render_template('index.html') |
|
|
|
|
| @app.route('/tagger') |
| def tagger(): |
| if (app.config["HEAD"] == len(app.config["FILES"])): |
| |
| app.config["HEAD"] = 0 |
| return redirect(url_for('final')) |
| user_id = session.get('_id') |
| directory = app.config["IMAGES"] + f'/{user_id}' |
| image = app.config["FILES"][app.config["HEAD"]] |
| labels = app.config["LABELS"] |
| not_end = not(app.config["HEAD"] == len(app.config["FILES"]) - 1) |
| |
| return render_template('tagger.html', not_end=not_end, directory=directory, image=image, labels=labels, head=app.config["HEAD"] + 1, len=len(app.config["FILES"])) |
|
|
|
|
| @app.route('/next') |
| def next(): |
| image = app.config["FILES"][app.config["HEAD"]] |
| app.config["HEAD"] = app.config["HEAD"] + 1 |
| user_id = session.get("_id") |
| with open(f"{app.config['OUT']}/{user_id}.csv", 'a+') as f: |
| for label in app.config["LABELS"]: |
| f.write(image + "," + |
| label["id"] + "," + |
| label["name"] + "," + |
| str(round(float(label["xMin"]))) + "," + |
| str(round(float(label["xMax"]))) + "," + |
| str(round(float(label["yMin"]))) + "," + |
| str(round(float(label["yMax"]))) + "\n") |
| app.config["LABELS"] = [] |
| return redirect(url_for('tagger')) |
|
|
| @app.route("/final") |
| def final(): |
| return render_template('final.html') |
|
|
| @app.route('/add/<id>') |
| def add(id): |
| xMin = request.args.get("xMin") |
| xMax = request.args.get("xMax") |
| yMin = request.args.get("yMin") |
| yMax = request.args.get("yMax") |
| app.config["LABELS"].append({"id":id, "name":"", "xMin":xMin, "xMax":xMax, "yMin":yMin, "yMax":yMax}) |
| return redirect(url_for('tagger')) |
|
|
| @app.route('/remove/<id>') |
| def remove(id): |
| index = int(id) - 1 |
| del app.config["LABELS"][index] |
| for label in app.config["LABELS"][index:]: |
| label["id"] = str(int(label["id"]) - 1) |
| return redirect(url_for('tagger')) |
|
|
| @app.route('/label/<id>') |
| def label(id): |
| name = request.args.get("name") |
| app.config["LABELS"][int(id) - 1]["name"] = name |
| return redirect(url_for('tagger')) |
|
|
| @app.route('/image/<f>') |
| def images(f): |
| user_id = session.get('_id') |
| images = app.config["IMAGES"] + f'/{user_id}' |
| print(images, '/', f) |
| return send_file(images +'/'+f) |
|
|
| @app.route('/download') |
| def download(): |
| user_id = session.get('_id') |
| anno_path = f"{app.config['OUT']}/{user_id}.csv" |
| directory = app.config["IMAGES"] + f'/{user_id}' |
| shutil.copyfile(anno_path, f'{directory}/annotations.csv') |
| shutil.make_archive('final', 'zip', directory) |
| return send_file('final.zip', |
| mimetype='text/csv', |
| download_name='final.zip', |
| as_attachment=True) |
|
|
| if __name__ == "__main__": |
| app.run(debug="True") |
|
|