-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
245 lines (214 loc) · 6.16 KB
/
index.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
242
243
244
245
<html>
<head>
<title>Web PHP Annotator</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<style>
.tclass {
width: 100%;
border: 1px solid black;
cursor: pointer;
font-size: 22px;
}
.title {
font-size:2em;
}
.datainfo {
font-size: 1em;
float: right;
}
.segimg {
width:300px;
}
.imglist {
overflow-y: auto;
height: 500px;
}
.dtitle {
font-size:1.2em;
background-color:gray;
color: white;
}
</style>
</head>
<body onload='init()' style="overflow-x:hidden;">
<table border='1'>
<tr>
<td style="width:1024px; height:768px;">
<div>
<span class='title'>PHP Annotator</span><span id='data_info' class='datainfo'>T</span>
</div>
<hr/>
<div class='dtitle'>Image filename: <span id='filename'></span></div>
<canvas id='canvas'></canvas>
</td>
<td valign='top' style='width:300px; text-align:center;'>
<div class='dtitle'>Classes</div>
<div id='classes'>
</div>
<br/>
<div id='debug'></div>
<br/>
<button onclick='undoLast()'>Undo Last Annotation</button>
<button onclick='clearAll()'>Clear Annotations</button><br/>
<br/>
<button onclick='save()'>Save</button><br/>
<br/>
<div style='width:100%;'><div class='dtitle'>Segmented Images</div>
<div class='imglist' id='imglist'></div>
</div>
</td>
</tr>
</table>
<script>
var IMAGE_WIDTH = 1536;
var IMAGE_HEIGHT = 1024;
var scale = 1;
var originx = 0;
var originy = 0;
var zoomIntensity = 0.2;
var tclass = null;
var classes = [];
var classlist = [];
var imagename = null;
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d'),
rect = {},
img = new Image(),
drag = false;
function class_click(cls) {
console.log("Class changed to",cls);
tclass = cls;
$("#class_" + cls).prop("checked", true);
}
function clearAll() {
classes = [];
$("#imglist").html("");
clear();
}
function undoLast() {
classes.pop();
$("#imglist div").first().remove();
clear();
}
function init() {
classes = [];
canvas.width = IMAGE_WIDTH;
canvas.height = IMAGE_HEIGHT;
scale = 1;
originx = 0;
originy = 0;
$("#debug").html("");
$("#imglist").html("");
//ctx.fillStyle = "blue";
//ctx.fillRect(0, 0, canvas.width, canvas.height);
canvas.addEventListener('mousedown', mouseDown, false);
canvas.addEventListener('mouseup', mouseUp, false);
canvas.addEventListener('mousemove', mouseMove, false);
$.getJSON( "process.php", {
f: "load",
format: "json"
})
.done(function( data ) {
console.log(data);
imagename = data.image;
$("#filename").html(imagename);
img.src = 'data/images/' + data.image;
img.onload = function() {
ctx.drawImage(img, 0, 0,IMAGE_WIDTH,IMAGE_HEIGHT);
}
$("#data_info").html("Total images:<b>" + data.total_images + "</b><br/>Total annotations:<b>" + data.total_annotated + " (" + (100.0*data.total_annotated/data.total_images).toFixed(1) + "%)</b>");
col = 0;
classlist = data.classes;
var r = '';
for(var a in data.classes) {
r += "<div class='tclass' style='background-color:hsl(" + col + ",100%,80%)' onclick='class_click(\"" + a+"\");'><input type=\"radio\" name='classes' id=\"class_" + a + "\"'/>" + data.classes[a] + "</div>";
col += 60;
}
$('#classes').html(r);
});
}
function save() {
$.post( "process.php", "f=save&image=" + imagename + "&classes="+JSON.stringify(classes), function( data ) {
console.log(data);
init();
}, "json");
}
function getSegment(ann) {
$("#imglist").prepend("<div class='dtitle'>" + classlist[ann.class] + "<br/><img class='segimg' src='process.php?f=seg&image="+imagename+"&segment="+JSON.stringify(ann) + "'></div>");
}
function mouseDown(e) {
if (tclass == null) return;
var rct = canvas.getBoundingClientRect();
var mx = e.pageX - rct.left;
var my = e.pageY - rct.top;
rect.startX = mx / scale + originx;
rect.startY = my / scale + originy;
drag = true;
}
function mouseUp(e) {
if (tclass == null) return;
var rct = canvas.getBoundingClientRect();
var mx = e.pageX - rct.left;
var my = e.pageY - rct.top;
var x2 = mx /scale + originx;
var y2 = my / scale + originy;
var z = {"class":tclass, "x1":(rect.startX/canvas.width), "y1":(rect.startY/canvas.height), "x2":(x2/canvas.width), "y2":(y2/canvas.height)};
classes.push(z);
getSegment(z);
//$("#debug").html(JSON.stringify(classes));
console.log(classes);
drag = false;
clear();
}
function mouseMove(e) {
if (drag) {
var rct = canvas.getBoundingClientRect();
var mx = e.pageX - rct.left;
var my = e.pageY - rct.top;
rect.w = mx / scale + originx - rect.startX;
rect.h = my / scale + originy - rect.startY ;
clear()
draw();
}
}
function draw() {
ctx.setLineDash([6]);
ctx.lineWidth = 5;
ctx.fillStyle = "hsla(" + (tclass*60) + ", 100%, 50%, 0.3)";
ctx.fillRect(rect.startX, rect.startY, rect.w, rect.h);
}
function clear() {
ctx.drawImage(img, 0, 0,IMAGE_WIDTH,IMAGE_HEIGHT);
for(var a in classes) {
c = classes[a];
ctx.fillStyle = "hsla(" + (c.class*60) + ", 100%, 50%, 0.3)";
ctx.fillRect(c.x1*canvas.width, c.y1*canvas.height, (c.x2-c.x1)*canvas.width, (c.y2-c.y1)*canvas.height);
}
}
canvas.onwheel = function(event) {
event.preventDefault();
var rct = canvas.getBoundingClientRect();
var mousex = event.clientX - rct.left;
var mousey = event.clientY - rct.top;
var wheel = event.deltaY < 0 ? 1 : -1;
var zoom = Math.exp(wheel*zoomIntensity);
if (scale * zoom < 1.0) zoom = 1.0 / scale;
var ox = originx - (mousex/(scale*zoom) - mousex/scale);
var oy = originy - (mousey/(scale*zoom) - mousey/scale);
if (ox < 0 || scale*zoom == 1.0) ox = 0;
if (oy < 0 || scale*zoom == 1.0) oy = 0;
ctx.translate(originx, originy);
originx = ox;
originy = oy;
ctx.scale(zoom, zoom);
ctx.translate(-originx, -originy);
clear();
scale *= zoom;
visibleWidth = canvas.width / scale;
visibleHeight = canvas.height / scale;
console.log("scale", scale, "originx", originx, "originy", originy);
}
init();
</script>
</body>
</html>