-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlabeler.html
More file actions
392 lines (306 loc) · 10.2 KB
/
Copy pathlabeler.html
File metadata and controls
392 lines (306 loc) · 10.2 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<!DOCTYPE html>
<html>
<head>
<title>Image Labeling</title>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;700&display=swap" rel="stylesheet">
<style>
body {
font-family: Open sans, Arial, sans-serif;
font-size: 10pt;
font-weight: 400;
color: #606060;
background: #ffffff;
}
body a {
color: #808080;
}
body a:hover {
text-decoration: underline;
}
body h3 {
font-size: 10pt;
color: #000000;
}
button {
background-color: #a2a2a2;
border: none;
color: #ffffff;
padding: 2px 6px;
border-radius: 3px;
text-align: center;
display: inline-block;
}
button:hover {
background-color: #808080;
}
.category_help_item {
background-color: #c0c0c0;
color: #ffffff;
border-radius: 4px;
padding: 2px 5px 2px 5px;
margin-right: 5px;
}
</style>
<script src="js/kmath.js"></script>
<script src="js/klabel.js"></script>
<script>
var labeler = new ImageLabeler;
var task_id;
var num_datapoints = 0;
var description = "";
var image_urls = [];
var model_scores = [];
var user_labels = [];
var labeler_stats_div;
var cursor_el;
var annotations_since_last_save = 0;
// fetch documentation
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
function handle_start_labeling_task() {
var task_input_el = document.getElementById("task_id_box");
var labeler_name_input_el = document.getElementById("labeler_id_box");
task_id = task_input_el.value;
var labeler_name = labeler_name_input_el.value.toLowerCase();
if (labeler_name.length == 0) {
alert("Please enter a labeler name");
return false;
}
// now need to go get the data
var fetch_task_url = "/labeling_api/get_task?" + new URLSearchParams({task_id: task_id});
console.log(task_id);
fetch(fetch_task_url)
.then(response => {
if (response.status !== 200) {
console.log("problem: " + response.status);
}
return response.json();
})
.then(task_info => {
task_id = task_info.task_id;
description = task_info.description;
category_mapping = task_info.categories;
datapoint_urls = task_info.datapoint_urls;
num_datapoints = datapoint_urls.length;
console.log("Task description: " + description);
console.log("Num datapoints: " + num_datapoints);
// check to see if existing labels for the current labeler exist
var existing_labels = null;
var existing_labeling_times = null;
Object.entries(task_info.labeler_results).forEach( entry => {
if (entry[0] == labeler_name) {
existing_labels = entry[1].labels;
console.log("Existing labels for " + labeler_name + " exist.");
if (entry[1].hasOwnProperty("labeling_times")) {
existing_labeling_times = entry[1].labeling_times;
console.log("Labeling times for " + labeler_name + " exist.");
}
}
});
var image_data = [];
for (var i=0; i<num_datapoints; i++) {
var data = new ImageData;
data.source_url = datapoint_urls[i];
// populate with existing labels if they exist
if (existing_labels && existing_labels[i] != Annotation.INVALID_CATEGORY)
data.annotations.push(new PerFrameAnnotation(existing_labels[i]));
if (existing_labeling_times)
data.labeling_time = existing_labeling_times[i];
image_data.push(data);
}
// fill in task information in the UI
var task_info_div = document.getElementById("task_info_div");
var str = "";
str += "<div>" + description + "</div>"
task_info_div.innerHTML = str;
var labeler_help_div = document.getElementById("labeler_help_div");
// update the keyboard help based on the category mappings
var entries = Array(10).fill("");
Object.entries(category_mapping).forEach( entry => {
var category_name = entry[0];
var key_code = entry[1].value;
entries[key_code] = category_name;
});
var help_str = "Keyboard help: ";
for (var i=0; i<10; i++) {
var idx = (idx == 9) ? 0 : i+1;
if (entries[idx] != "") {
help_str += "<span class=\"category_help_item\">";
help_str += "" + idx + ": " + entries[idx];
help_str += "</span>";
}
}
labeler_help_div.innerHTML = help_str;
// make the labeling UI visible
var main_div = document.getElementById("main_div");
main_div.style.visibility = "visible";
// setup klabeler widget
labeler.load_image_stack(image_data);
labeler.set_categories(category_mapping);
labeler.set_focus();
// update the labeler stats
update_labeler_stats();
})
.catch(err => {
console.log('Error fetching task info: ', err);
});
}
function klabel_annotations_to_labels(klabel_annotations) {
var labels = [];
klabel_annotations.forEach( img => {
if (img.annotations.length == 0 ||
img.annotations[0].type != Annotation.ANNOTATION_MODE_PER_FRAME_CATEGORY) {
labels.push(-1);
} else {
labels.push(img.annotations[0].value);
}
});
return labels;
}
function update_labeler_stats() {
var labels = klabel_annotations_to_labels(labeler.get_annotations());
var label_counts = Array(10).fill(0);
labels.forEach( label => {
if (label != Annotation.INVALID_CATEGORY)
label_counts[label]++;
});
// FIXME(kayvonf): do this on init. Code duplication with keyboard help.
var category_names = Array(10).fill("");
Object.entries(category_mapping).forEach( entry => {
var category_name = entry[0];
var category_value = entry[1].value;
category_names[category_value] = category_name;
});
var str = "";
for (var i=0; i<10; i++) {
var idx = (i+1 < 10) ? i+1 : 0;
if (category_names[idx] != "") {
str += "<div style=\"padding-top: 2px;\">";
str += "<button type=\"button\" onclick=\"handle_goto_next_by_category(" + idx + ")\">Next</button> ";
str += category_names[idx] + ": " + label_counts[idx] + "</div>"
}
}
labeler_stats_div.innerHTML = str;
}
function handle_save_results() {
var labeler_name_input_el = document.getElementById("labeler_id_box");
var labeler_name = labeler_name_input_el.value;
// the annotations from klabler
var klabel_annotations = labeler.get_annotations();
var labels = klabel_annotations_to_labels(klabel_annotations);
var labeling_times = [];
klabel_annotations.forEach( img => {
labeling_times.push(img.labeling_time);
});
var labeling_results = {
task_id: task_id,
labeler_name: labeler_name,
labels: labels,
labeling_times: labeling_times
};
annotations_since_last_save = 0;
fetch('/labeling_api/store_labels', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(labeling_results),
})
.then(response => {
if (response.status !== 200) {
console.log("problem: " + response.status);
}
return response.json();
})
.then(data => {
console.log('Success:', data);
})
.catch(error => {
console.error('Error:', error);
});
}
function handle_annotation_changed() {
update_labeler_stats();
annotations_since_last_save++;
// save every 10 annotations
if (annotations_since_last_save > 10)
handle_save_results();
}
function handle_frame_changed(frame_num) {
cursor_el.innerHTML = "Image " + frame_num + " of " + num_datapoints;
}
function handle_goto_first() {
labeler.set_current_frame_num(0);
}
function handle_goto_last() {
labeler.set_current_frame_num(num_datapoints-1);
}
function handle_goto_next_by_category(cat) {
var labels = klabel_annotations_to_labels(labeler.get_annotations());
var current_frame_num = labeler.get_current_frame_num();
// search from the current frame forward
for (var i=current_frame_num+1; i<num_datapoints; i++) {
if (labels[i] == cat) {
labeler.set_current_frame_num(i);
return;
}
}
// start back at the start and search up to the current frame num
for (var i=0; i<current_frame_num; i++) {
if (labels[i] == cat) {
labeler.set_current_frame_num(i);
return;
}
}
}
function handle_goto_next_unlabeled() {
handle_goto_next_by_category(Annotation.INVALID_CATEGORY);
}
function handle_onload() {
cursor_el = document.getElementById("cursor_el");
labeler_stats_div = document.getElementById("labeler_stats_div");
const url_params = new URLSearchParams(window.location.search);
if (url_params.has("task_id")) {
var el = document.getElementById("task_id_box");
el.value = url_params.get("task_id");
}
if (url_params.has("labeler")) {
var el = document.getElementById("labeler_id_box");
el.value = url_params.get("labeler");
}
var main_canvas = document.getElementById("main_canvas");
labeler.init(main_canvas);
labeler.set_frame_changed_listener(handle_frame_changed);
labeler.set_annotation_changed_listener(handle_annotation_changed);
labeler.set_keypress_annotation_mode(Annotation.ANNOTATION_MODE_PER_FRAME_CATEGORY);
labeler.set_click_annotation_mode(Annotation.ANNOTATION_MODE_NONE);
labeler.set_play_audio(false);
labeler.set_letterbox(true);
labeler.set_retain_zoom(false);
labeler.set_crosshairs_viz(false);
labeler.set_background_color("#e0e0e0");
}
</script>
</head>
<body onload="handle_onload()">
<div>
Task: <input type="text" id="task_id_box" style="width: 300px;" onchange="" />
Labeler: <input type="text" id="labeler_id_box" style="width: 250px" onchange="" />
<button id="get_data_button" type="button" onclick="handle_start_labeling_task()">Get Task</button>
<button id="post_results_button" type="button" onclick="handle_save_results()">Save Results</button>
</div>
<div id="main_div" style="visibility: hidden;">
<div id="task_info_div" style="padding: 4px 0px 4px 0px; font-style: italic;"></div>
<div>
<canvas id="main_canvas" width="960" height="500" tabindex="0" style="border: 1px solid #c0c0c0;" ></canvas>
</div>
<div style="padding-top: 6px;">
<span id="cursor_el" style="width: 100px;"></span>
<button id="get_data_button" type="button" onclick="handle_goto_first()">First</button>
<button id="get_data_button" type="button" onclick="handle_goto_next_unlabeled()">Next Unlabeled</button>
<button id="post_results_button" type="button" onclick="handle_goto_last()">Last</button>
</div>
<div id="labeler_help_div" style="padding-top: 6px;"></div>
<h3>Label Counts</h3>
<div id="labeler_stats_div"></div>
</div>
</p>
</body>
</html>