forked from marian42/butterflies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabels_server.py
211 lines (181 loc) · 6.64 KB
/
labels_server.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import os
from flask import Flask, send_file, request
import metadata
import random
import csv
items = metadata.load()
app = Flask(__name__)
IMAGE_FILE_FORMAT = 'data/images_alpha/{:s}.png'
INDEX_HTML = '''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Dataset Labelling</title>
</head>
<body>
<div id="viewer">
<div id="image" draggable="false" ondragstart="return false;"></div>
<div class="hline" style="top:100px;"></div>
<div class="hline" style="top:150px;"></div>
<div class="hline" style="top:200px;"></div>
<div class="hline" style="top:250px;"></div>
<div class="hline" style="top:300px;"></div>
<div class="hline" style="top:350px;"></div>
<div class="hline" style="top:400px;"></div>
<div class="hline" style="top:450px;"></div>
<div class="hline" style="top:500px;"></div>
<div class="hline" style="top:550px;"></div>
<div class="hline" style="top:600px;"></div>
<div class="vline" style="left:350px;"></div>
</div>
<div id="image_id"></div>
<button id="btn_load">Skip</button>
<button id="btn_save">Save</button>
<style>
#viewer {
background-color: #bdbdbd;
position: relative;
width: 700px;
height: 700px;
}
#image {
width: 500px;
height: 500px;
left: 100px;
top: 100px;
position: relative;
pointer-events: none;
user-select: none;
-webkit-user-drag: none;
background-size: 500px;
}
.hline {
width: 100%;
height: 2px;
background-color: rgba(0, 0, 0, 0.3);
position: absolute;
left: 0px;
pointer-events: none;
}
.vline {
height: 100%;
width: 2px;
background-color: rgba(0, 0, 0, 0.3);
position: absolute;
top: 0px;
pointer-events: none;
}
button {
margin: 10px;
padding: 20px 80px;
}
</style>
<script>
var viewer = document.getElementById('viewer');
var image = document.getElementById('image');
var btnLoad = document.getElementById('btn_load');
var btnSave = document.getElementById('btn_save');
var currentIdDiv = document.getElementById('image_id');
var currentId = null;
var currentRotation = 0;
function loadImage(imageId) {
currentId = imageId;
image.style.backgroundImage = 'url(image/' + currentId + '.png)';
image_id.innerHTML = currentId;
setRotation(60 * Math.random() - 30);
}
function requestImage() {
var request = new XMLHttpRequest();
request.open('GET', 'getid');
request.onload = function() {
if (request.status === 200) {
loadImage(request.responseText);
}
};
request.send();
}
function setRotation(value) {
image.style.transform = 'rotate(' + value + 'deg)';
currentRotation = value;
}
var urlId = document.location.href.split('?')[1];
if (urlId !== undefined) {
loadImage(urlId);
} else {
requestImage();
}
btnLoad.onclick = requestImage;
var mousePressed = false;
var lastMousePosition = 0;
function getMousePosition(event) {
return -Math.atan2(event.clientX - 350, event.clientY - 350) / Math.PI * 180;
}
viewer.onmousedown = function(event) {
mousePressed = true;
lastMousePosition = getMousePosition(event);
};
viewer.onmouseup = function(event) {
mousePressed = false;
};
viewer.onmousemove = function(event) {
if (mousePressed) {
movement = getMousePosition(event) - lastMousePosition;
lastMousePosition = getMousePosition(event);
setRotation(currentRotation + movement);
}
};
btnSave.onclick = function() {
var request = new XMLHttpRequest();
request.open('POST', 'save_rotation?id=' + currentId + '&rotation=' + currentRotation);
request.onload = function() {
if (request.status === 200) {
requestImage();
}
};
request.send();
};
document.addEventListener("drag", function( event ) {}, false);
document.addEventListener("dragover", function( event ) {
event.preventDefault();
}, false);
document.addEventListener('drop', function(event) {
event.preventDefault();
var fileName = event.dataTransfer.files[0].name;
var imageId = fileName.substring(0, fileName.length - 4);
loadImage(imageId);
}, false);
</script>
</body>
</html>'''
rotation_file = open('data/rotations.csv', 'r')
reader = csv.reader(rotation_file)
existing_ids = set(row[0] for row in reader)
rotation_file.close
rotation_file = open('data/rotations.csv', 'a')
@app.route('/')
def index():
return INDEX_HTML
@app.route('/image/<id>.png')
def get_image(id):
try:
return send_file(IMAGE_FILE_FORMAT.format(id))
except FileNotFoundError:
return "File not found", 404
@app.route('/getid')
def get_id():
while True:
item = random.choice(items)
if os.path.exists(IMAGE_FILE_FORMAT.format(item.image_id)) or item.image_id in existing_ids:
return item.image_id
@app.route('/save_rotation', methods=['POST'])
def save_rotation():
image_id = request.args.get('id')
if image_id in existing_ids:
print("Skipping duplicate id.")
return 'ok', 200
existing_ids.add(image_id)
rotation = request.args.get('rotation')
rotation_file.write('{:s},{:s}\n'.format(image_id, rotation))
rotation_file.flush()
return 'ok', 200
app.run(host='0.0.0.0')