-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlabel.html
More file actions
434 lines (381 loc) · 15 KB
/
label.html
File metadata and controls
434 lines (381 loc) · 15 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<html>
<head>
<title>Captcah Label Tool</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.rawgit.com/needim/noty/77268c46/lib/noty.css">
</head>
<body>
<div class="container">
<h2>標記你的驗證碼 labeling the captcha</h2>
<div class="row mb-3">
<div class="col-8">
<p>驗證碼可放置於 captcha 目錄,由 1.jpg, 2.jpg, 依此類推。</p>
<p>Please locate captcha in <strong>captcha</strong> folder, 1.jpg, 2.jpg and etc.</p>
</div>
<div class="col-4">
<nav>
<ul id="pagination1" class="pagination pagination-md justify-content-center">
<li class="page-item"><a class="page-link" href="#"></a></li>
<li class="page-item active"><a class="page-link" href="#"></a></li>
<li class="page-item"><a class="page-link" href="#"></a></li>
</ul>
</nav>
</div>
</div>
<div class="row mb-3">
<div class="col-3">
<label for="captcha">Captcha目錄 (Captcha folder):</label>
<input type="text" class="form-control" id="captcha" name="captcha" value="captcha">
</div>
<div class="col-3">
<label for="preprocess">預處理目錄 (Preprocess folder):</label>
<input type="text" class="form-control" id="preprocess" name="preprocess" >
</div>
<div class="col-5">
<label for="autoTab">自動跳下一組 (Jump next captcha automatically):</label>
<select id="autoTab" name="autoTab" class="form-control">
<option value="0">Disable</option>
<option value="3">3 char</option>
<option value="4" selected>4 char</option>
<option value="5">5 char</option>
<option value="6">6 char</option>
<option value="7">7 char</option>
</select>
</div>
</div>
<div class="row mb-5">
<div class="col-8">
<label for="allowChars">可接收字元 (Allowable chars):</label>
<input type="text" class="form-control" id="allowChars" name="allowChars" placeholder="123456ABCD">
</div>
<div class="col-4">
<button id="apply" type="button" class="btn btn-lg btn-primary mr-3">Apply</button>
</div>
</div>
<div class="row">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Image</th>
<th class="processed">Processed</th>
<th>Label</th>
</tr>
</thead>
<tbody>
<tr class="rowLabel">
<td></td>
<td><img src=""></td>
<td class="processed"><img src=""></td>
<td><input type="text" class="form-control" pattern="[234579ACFHKMNPQRTYZ]{1,4}"></td>
</tr>
</tbody>
</table>
</div>
<div class="row mb-3">
<button id="loadCache" type="button" class="btn btn-lg btn-info mr-3">Load Cache</button>
<button id="saveCache" type="button" class="btn btn-lg btn-info mr-3">Save Cache</button>
<button id="export" type="button" class="btn btn-lg btn-primary mr-3">Export as CSV</button>
<button id="import" type="button" class="btn btn-lg btn-primary mr-3">Import from CSV</button>
<input type="file" id="file-input" style="display:none" accept="text/csv" />
</div>
<nav>
<ul id="pagination2" class="pagination pagination-lg justify-content-center">
<li class="page-item"><a class="page-link" href="#"></a></li>
<li class="page-item active"><a class="page-link" href="#"></a></li>
<li class="page-item"><a class="page-link" href="#"></a></li>
</ul>
</nav>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdn.rawgit.com/needim/noty/77268c46/lib/noty.min.js"></script>
<script type="text/javascript">
function generateTable(size) {
for (i = 1; i < size; i++) {
$("tbody").append("<tr>" + $(".rowLabel").html() + "</tr>");
}
}
function assignImage(captchaFolder, processedFolder, page, size) {
if (processedFolder == "") {
$(".processed").hide();
}
for (i = 1; i <= $("tbody tr").length; i++) {
let no = (page - 1) * size + i;
$("tbody tr").eq(i - 1).find("td").eq(0).text(no);
if (!captchaFolder.endsWith("/")) {
captchaFolder += "/";
}
$("tbody tr").eq(i - 1).find("img").eq(0).attr("src", captchaFolder + no + ".jpg");
if (processedFolder != "") {
if (!processedFolder.endsWith("/")) {
processedFolder += "/";
}
$("tbody tr").eq(i - 1).find("img").eq(1).attr("src", processedFolder + no + ".jpg");
}
}
}
function getQueryString(page, size, autoTab, allowChars, captchaFolder, processedFolder) {
let query = {
"page": page
};
if (size && parseInt(size) != 50) {
query["size"] = size;
}
if (autoTab && parseInt(autoTab) != 0) {
query["autoTab"] = autoTab;
}
if (allowChars && allowChars != "") {
query["allowChars"] = allowChars;
}
if (captchaFolder && captchaFolder != "captcha") {
query["captchaFolder"] = captchaFolder;
}
if (processedFolder && processedFolder != "") {
query["processedFolder"] = processedFolder;
}
return new URLSearchParams(query).toString();
}
function assignPagination(page, size, autoTab, allowChars, captchaFolder, processedFolder) {
let p = parseInt(page) - 1;
for (i = 1; i <= 3; i++) {
if (p <= 0) {
$("#pagination1 a").eq(i - 1).hide();
$("#pagination2 a").eq(i - 1).hide();
} else {
let queryStr = getQueryString(p, size, autoTab, allowChars, captchaFolder, processedFolder);
$("#pagination1 a").eq(i - 1).show();
$("#pagination1 a").eq(i - 1).text(p);
$("#pagination1 a").eq(i - 1).attr("href", "?" + queryStr);
$("#pagination2 a").eq(i - 1).show();
$("#pagination2 a").eq(i - 1).text(p);
$("#pagination2 a").eq(i - 1).attr("href", "?" + queryStr);
}
p++;
}
}
function loadCache() {
let searchParams = new URLSearchParams(window.location.search);
const page = parseInt(searchParams.get("page") ? searchParams.get("page") : 1);
const size = parseInt(searchParams.get("size") ? searchParams.get("size") : 50);
cache = window.localStorage.getItem("label");
if (cache == null) {
return;
}
cache = JSON.parse(cache);
for (i = 1; i <= $("tbody tr").length; i++) {
let no = (page - 1) * size + i;
value = cache[no];
if (value) {
$("tbody tr").eq(i - 1).find("input").val(value);
}
}
}
function saveCache(showNoty) {
let searchParams = new URLSearchParams(window.location.search);
const page = parseInt(searchParams.get("page") ? searchParams.get("page") : 1);
const size = parseInt(searchParams.get("size") ? searchParams.get("size") : 50);
cache = window.localStorage.getItem("label");
if (cache == null) {
cache = {};
} else {
cache = JSON.parse(cache);
}
for (i = 1; i <= $("tbody tr").length; i++) {
let no = (page - 1) * size + i;
let value = $("tbody tr").eq(i - 1).find("input").val();
if (value != null && value != "") {
cache[no] = value;
}
}
window.localStorage.setItem("label", JSON.stringify(cache));
new Noty({
type: "success",
text: "Save cache successfully",
timeout: 3000,
theme: "relax"
}).show();
}
function cache2Csv() {
cache = window.localStorage.getItem("label");
if (cache == null) {
return;
}
cache = JSON.parse(cache);
total = Object.keys(cache).length;
var str = '';
let count = 0;
for (var no = 1; count != total; no++) {
if (cache[no]) {
str += cache[no];
count++;
}
str += '\r\n';
}
return str;
}
function exportCSVFile() {
var csv = cache2Csv();
var blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(blob, 'label.csv');
} else {
var link = document.createElement("a");
if (link.download !== undefined) { // feature detection
// Browsers that support HTML5 download attribute
var url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", 'label.csv');
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
}
$(document).ready(function() {
let searchParams = new URLSearchParams(window.location.search);
const page = parseInt(searchParams.get("page") ? searchParams.get("page") : 1);
const size = parseInt(searchParams.get("size") ? searchParams.get("size") : 50);
let autoTab = searchParams.get("autoTab");
let allowChars = searchParams.get("allowChars");
let captchaFolder = searchParams.get("captchaFolder") ? searchParams.get("captchaFolder") : "captcha";
let processedFolder = searchParams.get("processedFolder") ? searchParams.get("processedFolder") : "";
if (autoTab == null) {
autoTab = window.localStorage.getItem("autoTab");
}
if (autoTab != null) {
$("select[name=autoTab]").val(autoTab);
}
if (allowChars == null) {
allowChars = window.localStorage.getItem("allowChars");
}
if (allowChars != null && allowChars != "") {
$("input[name=allowChars]").val(allowChars);
}
$("input[name=captcha]").val(captchaFolder);
$("input[name=preprocess]").val(processedFolder);
generateTable(size);
assignImage(captchaFolder, processedFolder, page, size);
assignPagination(page, size, autoTab, allowChars, captchaFolder, processedFolder);
loadCache();
$("tbody input").keyup(function(event) {
let val = $(event.currentTarget).val();
if (allowChars != null && allowChars != "") {
let re = new RegExp("[" + allowChars + "]*");
let match = val.match(re);
if (match[0] != val) {
$(event.currentTarget).val(match[0]);
return;
}
}
if (autoTab != 0) {
if (val.length >= autoTab) {
let index = $(event.currentTarget).parent().parent().parent().find("input").index($(event.currentTarget));
$(event.currentTarget).parent().parent().parent().find("input").eq(index + 1).focus();
}
}
});
$("#apply").click(function() {
let autoTab = $("select[name=autoTab]").val();
let allowChars = $("input[name=allowChars]").val();
let captchaFolder = $("input[name=captcha]").val();
let processedFolder = $("input[name=preprocess]").val();
let queryStr = getQueryString(page, size, autoTab, allowChars, captchaFolder, processedFolder);
window.localStorage.setItem("autoTab", autoTab);
window.location.href = "?" + queryStr;
});
$("input#allowChars").change(function() {
let chars = $("input[name=allowChars]").val();
window.localStorage.setItem("allowChars", chars);
})
$("#loadCache").click(function() {
loadCache();
});
$("#saveCache").click(function() {
saveCache(true);
});
$("#export").click(function() {
saveCache(false);
exportCSVFile();
});
$(".page-link").click(function() {
saveCache(false);
});
$("#import").click(function() {
$("#file-input").click();
});
$("#file-input").change(function() {
// list of selected files
var all_files = this.files;
if(all_files.length == 0) {
alert('Error : No file selected');
return;
}
// first file selected by user
var file = all_files[0];
// files types allowed
// we are reading text file in this example
var allowed_types = ['text/csv'];
if(allowed_types.indexOf(file.type) == -1) {
alert('Error : Incorrect file type');
return;
}
// Max 2 MB allowed
var max_size_allowed = 2*1024*1024
if(file.size > max_size_allowed) {
alert('Error : Exceeded size 2MB');
return;
}
// file validation is successful
// we will now read the file
var reader = new FileReader();
// file reading started
reader.addEventListener('loadstart', function() {
console.log('File reading started');
});
// file reading finished successfully
reader.addEventListener('load', function(e) {
var text = e.target.result;
// contents of the file
var arr = text.split(/\r?\n/);
var cache = {}
for (var key in arr) {
if (arr[key] && arr[key] != "") {
cache[parseInt(key) + 1] = arr[key];
}
}
window.localStorage.setItem("label", JSON.stringify(cache));
new Noty({
type: "success",
text: "Read csv successfully, please reload page.",
timeout: 3000,
theme: "relax"
}).show();
$("#file-input").val("");
});
// file reading failed
reader.addEventListener('error', function() {
alert('Error : Failed to read file');
});
// file read progress
reader.addEventListener('progress', function(e) {
if(e.lengthComputable == true) {
var percent_read = Math.floor((e.loaded/e.total)*100);
console.log(percent_read + '% read');
}
});
// read as text file
reader.readAsText(file);
});
var timeoutId;
if (timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(function () {
saveCache(true)
}, 60000);
});
</script>
</html>