-
Notifications
You must be signed in to change notification settings - Fork 3
/
annotator.html
241 lines (219 loc) · 7.05 KB
/
annotator.html
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
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE">
<title>Red Hen Rapid Annotator</title>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<style>
body {
font: normal 14px Verdana, Arial, sans-serif;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://cdn.jsdelivr.net/hls.js/latest/hls.min.js"></script>
<script>
//First Add this to extend jQuery
$.extend({
getUrlVars: function(){
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
},
getUrlVar: function(name){
return $.getUrlVars()[name];
}
});
//Second call with this:
// Get object of URL parameters
// var allVars = $.getUrlVars();
// Getting URL var by its nam
// var byName = $.getUrlVar('name');
var listofimages = [];
var listofkeys = [];
var listofannotations = [];
var currentimage = "";
var lastimage;
var listofpreloadedimages = [];
var paramset = $.getUrlVar('paramset');
var imageset = $.getUrlVar('imageset');
var random = $.getUrlVar('random');
var user = $.getUrlVar('user');
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function preload(image) {
if (image === undefined) {
return;
}
listofpreloadedimages.push(image);
if (image == "NO_FURTHER_IMAGES")
{
return;
} else if (/^video\t(.*?)\t(.*)/.exec(image)) {
var name = (/^video\t(.*?)\t(.*)/.exec(image))[1];
var url = (/^video\t(.*?)\t(.*)/.exec(image))[2];
$('<video id="' + name + '" controls loop />').attr('name',name).attr('data-url',url).appendTo("#preload").css('display','none');
return;
}
$('<img id="' + image + '" />').attr('src','image_files/' + image).appendTo("#preload").css('display','none');
}
function shownextimage(annotation) {
// $("#lastpicture").empty();
// lastimage = currentimage;
// $("#lastfileinfo").text(lastimage);
// $("#lastannotation").text(annotation);
// $("#picture :first-child" ).appendTo("#lastpicture").css({
// 'display': 'inherit',
// 'height': 'auto',
// 'width': 'auto',
// 'max-width': '200px',
// 'max-height': '300px'
// });
$("#picture video").each(function(idx, videoElement) {
$(videoElement).off();
videoElement.src = '';
videoElement.play();
});
$("#picture").empty();
$("#preload :first-child" ).appendTo("#picture").css({
'display': 'inherit',
'height': 'auto',
'width': 'auto',
'max-width': '700px',
'max-height': '700px'
});
currentimage = listofpreloadedimages.shift();
if (currentimage == "NO_FURTHER_IMAGES")
{
$("#fileinfo").empty().append("<span style='font-weight: bold; color: red;'>No more images to annotate. Thank you!</span>");
} else if (/^video\t(.*?)\t(.*)/.exec(currentimage)) {
var name = (/^video\t(.*?)\t(.*)/.exec(currentimage))[1];
$("#fileinfo").text(name);
var videoElement = $('#picture video')[0];
if (videoElement != undefined) {
var url = $(videoElement).attr('data-url');
if (url != undefined) {
var hls = new Hls();
hls.loadSource(url);
hls.attachMedia(videoElement);
hls.on(Hls.Events.MANIFEST_PARSED,function() {
videoElement.play();
});
}
}
}
else {
$("#fileinfo").text(currentimage);
}
}
function annotate(image, annotation) {
var name;
var time;
if (image.indexOf("video\t") === 0) {
var arr = image.split("\t");
name = arr[1];
var starttime = Number(arr[3]);
var reltime = Number($('#picture video')[0].currentTime);
time = starttime + reltime;
} else {
name = image;
time = undefined;
}
$.get('json_annotator.cgi?action=annotate&file=' + encodeURIComponent(name) + "&annotation=" + encodeURIComponent(annotation) + "¶mset=" + paramset + "&imageset=" + imageset + "&user=" + user + (time != undefined ? ("&time=" + time) : ""));
preload(listofimages.shift());
shownextimage(annotation);
}
$(document).ready(function() {
$.getJSON('json_annotator.cgi?action=getparameters¶meterset=' + paramset, function (data) {
$.each(data, function (key, pair) {
for (var i in pair)
{
$("#annotation").append(i + ": " + pair[i] + "<br />");
listofkeys.push(i);
listofannotations[i] = pair[i];
$("#buttons").append('<button type="button" value="' + i + '" id="btn_' + i + '">' + pair[i] + '</button>');
$("#buttons").on('click', '#btn_' + i, function() {
// console.log(this);
annotate(currentimage, listofannotations[this.value]);
});
}
});
});
$.getJSON('json_annotator.cgi?action=getimagelist&imageset=' + imageset + "¶mset=" + paramset + "&user=" + user, function (data) {
listofimages = data.slice();
if (random == "yes") {
listofimages = shuffle(listofimages);
}
listofimages.push("NO_FURTHER_IMAGES");
preload(listofimages.shift());
preload(listofimages.shift());
preload(listofimages.shift());
preload(listofimages.shift());
shownextimage();
});
$(document).on('keyup', function(event) {
thekey = event.which.toString();
thekey = String.fromCharCode(thekey);
console.log(thekey);
if ($.inArray(thekey, listofkeys) >= 0)
{
annotate(currentimage, listofannotations[thekey]);
console.log("Bello " + thekey);
console.log(listofkeys);
}
else {console.log("Problem mit " + thekey);}
});
});
</script>
</head>
<body>
<h1>Red Hen Rapid Annotator</h1>
<div id="buttons"></div>
<div style="float:left;">
<p>
<div id="annotation"></div>
</p>
<p>
<div style="width:220px;">
<span id="fileinfo" style="width:200px; word-wrap:break-word; display:inline-block;"></div>
</p>
<!-- This was a display of the last picture, its filename and its annotation in the left-hand pane. Could be extended to implement an undo functionality.
<h3>Last image:</h3>
<p>
<div id="lastpicture"></div>
</p>
<p>
<div id="lastfileinfo" style="width:200px;"></div>
</p>
<p>
<div id="lastannotation"></div>
</p>
-->
</div>
<p>
<div id="picture" style="float:left;"></div>
</p>
<div id="preload"></div>
<div style="clear:both;"><p><br /><br />written 2016 by Peter Uhrig</p></div>
</body>
</html>