-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.py
109 lines (96 loc) · 3.46 KB
/
app.py
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
import os
import shutil
from flask import Flask, redirect, url_for, request, flash
from flask import render_template
from flask import send_file
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
@app.route('/',methods = ['GET', 'POST'])
def index():
if request.method == 'POST':
if 'file' not in request.files:
flash('No files selected')
return redirect('/')
try:
shutil.rmtree('./images')
except:
pass
os.mkdir('./images')
files = request.files.getlist("file")
for f in files:
f.save(os.path.join('./images', f.filename))
for (dirpath, dirnames, filenames) in os.walk(app.config["IMAGES"]):
files = filenames
break
app.config["FILES"] = files
return redirect('/tagger', code=302)
else:
return render_template('index.html')
@app.route('/tagger')
def tagger():
if (app.config["HEAD"] == len(app.config["FILES"])):
return redirect(url_for('final'))
directory = app.config["IMAGES"]
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
with open(app.config["OUT"],'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):
images = app.config["IMAGES"]
return send_file(images +'/'+f)
@app.route('/download')
def download():
shutil.copyfile('out.csv', 'images/annotations.csv')
shutil.make_archive('final', 'zip', 'images')
return send_file('final.zip',
mimetype='text/csv',
attachment_filename='final.zip',
as_attachment=True)
if __name__ == "__main__":
app.config["IMAGES"] = 'images'
app.config["LABELS"] = []
app.config["HEAD"] = 0
app.config["OUT"] = "out.csv"
with open("out.csv",'w') as f:
f.write("image,id,name,xMin,xMax,yMin,yMax\n")
app.run(debug="True")