forked from 4kssoft/CUTIE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset_creater.html
222 lines (184 loc) · 7.01 KB
/
dataset_creater.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
<!DOCTYPE HTML>
<html>
<head>
<style>
div.footer {
position: fixed;
bottom: 0;
left: 0;
margin: 0 auto;
background: #0072BB;
color: #fff;
}
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 200px;
}
td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<label for="classes">Choose a class:</label>
<select id="classes" name="classes">
</select>
<button onclick="submitField()">Add</button>
<button onclick="downloadJson()">Download</button>
<p>Open image: <input type="file" id="inputImage" /></p>
<p>Open Json: <input type="file" id="inputJson" /></p>
<canvas id="canvas" style="margin:12px;"></canvas>
<table id="table"></table>
<br><br>
<div class="footer" id="results">Move mouse over image to show mouse location and pixel value and alpha</div>
<script>
var allclasses = ['SELLER_STATE', 'SELLER_ID', 'SELLER_NAME', 'SELLER_ADDRESS', 'SELLER_GSTIN_NUMBER',
'COUNTRY_OF_ORIGIN', 'CURRENCY', 'DESCRIPTION', 'INVOICE_NUMBER', 'INVOICE_DATE', 'DUE_DATE',
'TOTAL_INVOICE_AMOUNT_ENTERED_BY_WH_OPERATOR', 'PO_NUMBER', 'BUYER_GSTIN_NUMBER', 'SHIP_TO_ADDRESS',
'PRODUCT_ID', 'HSN', 'TITLE', 'QUANTITY', 'UNIT_PRICE', 'DISCOUNT_PERCENT', 'SGST_PERCENT',
'CGST_PERCENT', 'IGST_PERCENT', 'TOTAL_AMOUNT'];
select = document.getElementById('classes');
table = document.getElementById('table');
for (op in allclasses) {
var opt = document.createElement('option');
opt.value = allclasses[op];
opt.innerHTML = allclasses[op];
select.appendChild(opt);
var tr = document.createElement('tr');
var td1 = document.createElement('td');
var td2 = document.createElement('td');
td2.id = allclasses[op] + "td";
td1.innerHTML = allclasses[op];
td2.innerHTML = "✅";
tr.appendChild(td1);
tr.appendChild(td2);
table.appendChild(tr);
}
var URL = window.URL;
var cvs = document.getElementById('canvas');
var ctx = cvs.getContext('2d');
var res = document.getElementById('results');
var rects;
var json;
var filename;
var selectedRects = [];
var fields = [];
function submitField() {
selectedClass = document.getElementById("classes").value;
var value_text = [];
var value_id = [];
for (i in selectedRects) {
value_text.push(selectedRects[i].text);
value_id.push(selectedRects[i].id);
}
fields.push(
{
"field_name": selectedClass,
"value_id": value_id,
"value_text": value_text,
"key_id": [],
"key_text": []
}
);
console.log(fields);
selectedRects = [];
var text = document.createTextNode(selectedClass + ", ");
td = document.getElementById(selectedClass + 'td');
td.innerHTML = "✔";
}
function downloadJson() {
json["fields"] = fields;
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(json));
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", filename);
document.body.appendChild(downloadAnchorNode); // required for firefox
downloadAnchorNode.click();
downloadAnchorNode.remove();
}
cvs.addEventListener('click', function (e) {
var rect = collides(e.offsetX, e.offsetY);
fillcolor(rect);
selectedRects.push(rect);
}, false);
function collides(x, y) {
for (var i = 0, len = rects.length; i < len; i++) {
var left = rects[i].bbox[0], right = rects[i].bbox[2];
var top = rects[i].bbox[1], bottom = rects[i].bbox[3];
if (right >= x
&& left <= x
&& bottom >= y
&& top <= y) {
return rects[i];
}
}
}
function fillcolor(rect) {
ctx.fillStyle = '#00800047';
ctx.fillRect(
rect.bbox[0], rect.bbox[1],
rect.bbox[2] - rect.bbox[0],
rect.bbox[3] - rect.bbox[1]
);
}
cvs.addEventListener('mousemove', mousePos, false);
window.onload = function () {
var inputImage = document.getElementById('inputImage');
inputImage.addEventListener('change', handleImageFiles, false);
var inputJson = document.getElementById('inputJson');
inputJson.addEventListener('change', handleJsonFiles, false);
}
function handleJsonFiles(e) {
filename = e.target.files[0].name;
fr = new FileReader();
fr.onload = receivedText;
fr.readAsText(e.target.files[0]);
}
function receivedText(e) {
let lines = e.target.result;
json = JSON.parse(lines);
rects = json['text_boxes'];
if (ctx) {
ctx.beginPath();
ctx.strokeStyle = "red";
for (var i = 0, len = rects.length; i < len; i++) {
ctx.rect(
rects[i].bbox[0], rects[i].bbox[1],
rects[i].bbox[2] - rects[i].bbox[0],
rects[i].bbox[3] - rects[i].bbox[1]
);
}
ctx.stroke();
}
}
function mousePos(evt) {
var rect = cvs.getBoundingClientRect();
var x = parseInt(evt.clientX - rect.left);
var y = parseInt(evt.clientY - rect.top);
var p = ctx.getImageData(x, y, 1, 1).data;
results.innerHTML = '<table style="width:100%;table-layout:fixed"><td>X: '
+ x + '</td><td>Y: ' + y + '</td><td>Red: '
+ p[0] + '</td><td>Green: ' + p[1] + '</td><td>Blue: '
+ p[2] + '</td><td>Alpha: ' + p[3] + "</td></table>";
return { x, y };
}
function handleImageFiles(e) {
var url = URL.createObjectURL(e.target.files[0]);
var img = new Image();
img.onload = function () {
cvs.width = img.width;
cvs.height = img.height;
ctx.drawImage(img, 0, 0);
}
img.src = url;
}
</script>
</body>
</html>