-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathapp.py
106 lines (93 loc) · 3.21 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
import sys
from os import walk
import imghdr
import csv
import argparse
from flask import Flask, redirect, url_for, request
from flask import render_template
from flask import send_file
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
@app.route('/tagger')
def tagger():
if (app.config["HEAD"] == len(app.config["FILES"])):
return redirect(url_for('bye'))
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("/bye")
def bye():
return send_file("taf.gif", mimetype='image/gif')
@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('/prev')
# def prev():
# app.config["HEAD"] = app.config["HEAD"] - 1
# return redirect(url_for('tagger'))
@app.route('/image/<f>')
def images(f):
images = app.config['IMAGES']
return send_file(images + f)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('dir', type=str, help='specify the images directory')
parser.add_argument("--out")
args = parser.parse_args()
directory = args.dir
if directory[len(directory) - 1] != "/":
directory += "/"
app.config["IMAGES"] = directory
app.config["LABELS"] = []
files = None
for (dirpath, dirnames, filenames) in walk(app.config["IMAGES"]):
files = filenames
break
if files == None:
print("No files")
exit()
app.config["FILES"] = files
app.config["HEAD"] = 0
if args.out == None:
app.config["OUT"] = "out.csv"
else:
app.config["OUT"] = args.out
print(files)
with open("out.csv",'w') as f:
f.write("image,id,name,xMin,xMax,yMin,yMax\n")
app.run(debug="True")