File size: 4,315 Bytes
9bbf7fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import shutil
import uuid

from flask import Flask, redirect, url_for, request, flash, session
from flask import render_template
from flask import send_file

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
        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:
            print('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
        print(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"])):
        # done annotating the batch of images
        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)
    # print(not_end)
    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}'
    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")