-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
195 lines (168 loc) · 5.67 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<style type="text/css">
#drop-area.highlight {
background-color: limegreen;
}
#container {
height: 100%;
width: 100%;
}
svg text {
transform: scaleY(-1);
font-size: 11px;
}
</style>
</head>
<body>
<div id='drop-area' >
<div id='file-name'>
Drop .hocr file here...
</div>
<input type='file' accept='.hocr' onchange='openFile(event)'>
<br/>
<div id="container"><svg></svg></div>
</div>
</body>
<script type="application/javascript">
let dropArea = document.getElementById('drop-area');
let fileName = document.getElementById('file-name');
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false)
});
function preventDefaults (e) {
e.preventDefault()
e.stopPropagation()
};
['dragenter', 'dragover'].forEach(eventName => {
dropArea.addEventListener(eventName, highlight, false)
});
['dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, unhighlight, false)
});
function highlight(e) {
dropArea.classList.add('highlight')
};
function unhighlight(e) {
dropArea.classList.remove('highlight')
};
dropArea.addEventListener('drop', dropHandler, false)
class Bbox {
constructor(left, top, right, bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
this.width = this.right - this.left;
this.height = this.bottom - this.top;
}
}
function drawBboxesLayout(page_sizes, bboxes_coords) {
const svgns = 'http://www.w3.org/2000/svg';
let container = document.getElementById('container');
// Clean svn container from previous renderings
container.childNodes.forEach(el => { el.remove() });
let page = document.createElementNS(svgns, 'svg');
[width, height] = page_sizes;
page.setAttribute('width', width);
page.setAttribute('height', height);
page.setAttribute('xmlns', svgns);
let cartesian = document.createElementNS(svgns, 'g');
cartesian.setAttribute('transform', `translate(0, ${height}) scale(1,-1)`)
const bboxColor = 'rgba(0, 0, 200, 0.1)';
const textColor = 'rgba(0, 0, 0)';
for (let bbox of bboxes_coords) {
if (! (Boolean(bbox.coords.top) || Boolean(bbox.coords.left) ||
Boolean(bbox.coords.right) ||Boolean(bbox.coords.bottom))) {
continue;
}
// container to group text with bbox rect
let g = document.createElementNS(svgns, 'g');
let text = document.createElementNS(svgns, 'text');
text.textContent = bbox.text || '.';
text.setAttribute('x', bbox.coords.left);
text.setAttribute('y', -bbox.coords.top);
text.setAttribute('textLength', bbox.coords.width);
text.setAttribute('fill', textColor);
let rect = document.createElementNS(svgns, 'rect');
rect.setAttribute('x', bbox.coords.left);
rect.setAttribute('y', bbox.coords.top);
rect.setAttribute('width', bbox.coords.width);
rect.setAttribute('height', bbox.coords.height);
rect.setAttribute('fill', bboxColor);
g.appendChild(text);
g.appendChild(rect);
cartesian.appendChild(g);
}
page.appendChild(cartesian);
container.appendChild(page);
}
function openFile(event) {
var input = event.target;
var reader = new FileReader();
reader.onload = (event) => {
text = reader.result;
let {page_sizes, bboxes_coords} = parseHOCR(text);
drawBboxesLayout(page_sizes, bboxes_coords);
};
reader.readAsText(input.files[0]);
};
function processFile(file) {
let reader = new FileReader();
reader.onload = (event) => {
text = reader.result;
let {page_sizes, bboxes_coords} = parseHOCR(text);
drawBboxesLayout(page_sizes, bboxes_coords);
};
reader.readAsText(file);
};
//this will parse XML file and output it to website
function parseHOCR(text) {
let responseDoc = new DOMParser().parseFromString(text, 'application/xml');
var bboxes_coords = [];
let page = responseDoc.querySelector('.ocr_page[title*="bbox"]');
var page_sizes = page.title
.split(/;\s*/)
.filter(_ => _.startsWith('bbox'))[0]
.replace('bbox ', '')
.split(' ')
.slice(2)
.map(_ => parseInt(_));
for (let bbox of responseDoc.querySelectorAll('*[title*="bbox"]')) {
let coords = bbox.title.replace('bbox ', '').split(' ');
bboxes_coords.push({
coords: new Bbox(...coords.map(_ => parseInt(_))),
text: [].reduce.call(
bbox.childNodes, (a, b) => a + (b.nodeType === 3 ? b.textContent : ''), '') || ''
});
}
return {page_sizes, bboxes_coords};
};
function dropHandler(ev) {
console.log('File(s) dropped');
// Prevent default behavior (Prevent file from being opened)
ev.preventDefault();
if (ev.dataTransfer.items) {
// Use DataTransferItemList interface to access the file(s)
for (let item of ev.dataTransfer.items) {
// If dropped items aren't files, reject them
if (item.kind === 'file') {
let file = item.getAsFile();
console.log('... file.name = ' + file.name);
fileName.innerHTML = file.name;
processFile(file);
}
}
} else {
// Use DataTransfer interface to access the file(s)
for (let file of ev.dataTransfer.files) {
console.log('... file.name = ' + file.name);
fileName.innerHTML = file.name;
processFile(file);
}
}
}
</script>
</html>