-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
233 lines (199 loc) · 8.01 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
<!doctype html>
<html lang="en">
<head>
<title>Shape Parser</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
<script src="./fs.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body class="mx-auto">
<h1 class="text-xl mb-2">Annotated Shape Parser</h1>
<p>Select the annotated json files</p>
<form class="flex flex-col justify-center align-center gap-2">
<input class="mt-2 p-2 file:bg-black file:text-white border border-black" type="file" name="annotated" id="input_files" onchange="readThis(this)" multiple />
<button type="button" onclick="readThis(this)" class="bg-black text-white p-1">Read</button>
<button type="button" onclick="readFromStorage(this)" class="bg-black text-white p-1">Fetch Annotated</button>
</form>
<hr class="my-1 bg-black h-1"/>
<p>Label Info: </p>
<form>
<select id="select-labels" class="border border-solid border-black border-2 w-full h-full" multiple>
<option>None</option>
</select>
<div class="flex flex-row flex-wrap gap-2">
<button class="bg-black text-white p-1" type="button" onclick="removeSelected()">Remove Selected</button>
<button class="bg-black text-white p-1" type="button" onclick="saveSelected()">Save Selected</button>
<button class="bg-black text-white p-1" type="button" onclick="saveStaticSelected()">Save Static Selected</button>
<button class="bg-black text-white p-1" type="button" onclick="replaceSelected(this)">Replace Selected</button>
<button class="bg-black text-white p-1" type="button" onclick="findSelected(this)">Find All</button>
<button class="bg-black text-white p-1" type="button" onclick="printOutput(this)">Print Output</button>
<button class="bg-black text-white p-1" type="button" onclick="saveUniqueLabels()">Save labels</button>
<button class="bg-black text-white p-1" type="button" onclick="saveAnnotation()">Save Annotation</button>
</div>
<pre>
<span id="found_selected"></span>
</pre>
</form>
<pre><span id="total_labels">Result show once readed</span></pre>
<!-- <script src="fileSaver.js"></script> -->
<script>
let inputFiles = document.querySelector("#input_files");
let total_labels = document.querySelector("#total_labels");
let select_labels = document.querySelector("#select-labels");
let found_selected = document.querySelector("#found_selected");
let find_list = []
let annotated = [];
let output = "";
let labels = [];
let suffix = "";
function saveUniqueLabels() {
const tmp = [...select_labels.children].map(opt => opt.textContent);
tmp.unshift("_background_");
tmp.unshift("__ignore__");
let blob = new Blob([tmp.join("\n")], { type: "text/plain;charset=utf-8" });
saveAs(blob, "labels.txt");
}
function saveAnnotation(annotation=null) {
let tmp;
if (annotation) {
tmp = annotation
} else {
tmp = annotated
}
tmp.forEach((annote, index) => {
let blob = new Blob([JSON.stringify(annote, null, 1)], { type: "application/json;charset=utf-8" });
let fPath = annote.imagePath.split("\\");
let fName = fPath[fPath.length - 1].split(".")[0] + suffix + ".json";
saveAs(blob, fName);
})
}
function printOutput(e) {
alert(output);
}
async function findSelected(e) {
annotated.length > 0 ? readFromStorage(true) : readThis(this, true);
}
function updateLabelsDisplay(labels) {
[...select_labels.children].forEach(opt => select_labels.removeChild(opt))
labels = labels.flat().sort();
let labelsLength = labels.length;
let uniqueLabels = [...new Set(labels)];
console.log("L29", { labelsLength, uniqueLabels })
// total_labels.textContent = JSON.stringify({uniqueLabels, labelsLength}, null, 4);
uniqueLabels.forEach(label => {
var option = document.createElement("option");
option.text = label;
select_labels.add(option);
select_labels.size = "30"
})
}
function readFromStorage(find = false) {
try {
let fList = [...select_labels.selectedOptions].map(item => item.textContent)
let labels = [];
let foundTarget = [];
annotated.forEach((anno, index) => {
let tmp = anno.shapes.map(shape => shape.label);
labels.push(tmp);
fList.forEach(ele => {
if (tmp.find(item => item == ele)) {
let obj = { ele, file: anno.imagePath };
foundTarget.push(obj);
console.log(obj)
output += JSON.stringify(obj);
console.log({ output })
}
})
})
updateLabelsDisplay(labels);
} catch (err) {
console.log({ err });
}
}
async function saveStaticSelected() {
let selected_list = [...select_labels.selectedOptions].map(option => option.textContent);
let tmp = annotated.filter(annote => {
const labels = annote.shapes.map(i => i.label);
return selected_list.every(ele => labels.includes(ele));
})
tmp = tmp.map(json_file => {
json_file.shapes = json_file.shapes.filter(shape => selected_list.find(label => label == shape.label) ? true : false);
return json_file;
})
console.log({tmp});
saveAnnotation(tmp);
}
async function saveSelected() {
let selected_list = [...select_labels.selectedOptions].map(option => option.textContent);
const tmp = annotated.map(json_file => {
json_file.shapes = json_file.shapes.filter(shape => selected_list.find(label => label == shape.label) ? true : false);
return json_file;
})
saveAnnotation(tmp);
}
async function removeSelected() {
let remove_list = [...select_labels.selectedOptions].map(option => option.textContent);
annotated = annotated.map(json_file => {
json_file.shapes = json_file.shapes.filter(shape => remove_list.find(label => label == shape.label) ? false : true)
return json_file;
})
readFromStorage(null);
}
async function replaceSelected(e) {
let old_values_list = [...select_labels.selectedOptions].map(option => option.textContent);
let new_value = prompt(`New Value for selected? ${JSON.stringify(old_values_list)}`)
annotated = annotated.map(json_file => {
return replaceShape(json_file, old_values_list, new_value);
})
readFromStorage(null);
}
function replaceShape(json_file, old_labels, new_label) {
json_file.shapes.forEach(shape => {
if (old_labels.find(label => label == shape.label)) {
shape.label = new_label;
}
})
return json_file;
}
async function readThis(e, findSelected = false) {
let foundTarget = [];
annotated = [];
for (let index = 0; index < inputFiles.files.length; index++) {
let reader = new FileReader();
let file = inputFiles.files[index];
reader.readAsText(file);
reader.onload = function () {
let fList = [...select_labels.selectedOptions].map(item => item.textContent)
let result = "";
new Promise(resolve => {
result = reader.result;
if (result.length > 0) {
resolve();
}
})
let pResult = JSON.parse(result);
annotated.push(pResult);
let tmp = pResult.shapes.map(i => i.label);
fList.forEach(ele => {
if (tmp.find(item => item == ele)) {
foundTarget.push({ ele, name: file.name });
console.log({ ele, name: file.name })
output += JSON.stringify({ ele, name: file.name }, null, 4);
console.log({ output })
}
})
labels.push(tmp);
};
reader.onerror = function () {
console.log(reader.error);
};
}
setTimeout(() => {
updateLabelsDisplay(labels);
}, 1000);
}
</script>
</body>
</html>