-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
125 lines (105 loc) · 3.92 KB
/
script.js
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
function parseHOCR(hocrDocument) {
const parser = new DOMParser();
const doc = parser.parseFromString(hocrDocument, 'text/html');
const hocrData = [];
function parseElement(element) {
const elementData = {
type: element.classList[0],
boundingBox: element.getAttribute('title') ? element.getAttribute('title').match(/bbox (\d+)\s+(\d+)\s+(\d+)\s+(\d+)/) : null,
text: element.classList.contains('ocrx_word') ? element.textContent.trim() : ''
};
if (elementData.boundingBox) {
const [x1, y1, x2, y2] = elementData.boundingBox.slice(1).map(parseFloat);
elementData.x1 = x1;
elementData.y1 = y1;
elementData.x2 = x2;
elementData.y2 = y2;
hocrData.push(elementData);
}
for (const childElement of element.children) {
parseElement(childElement);
}
}
parseElement(doc.body);
return hocrData;
}
function visualizeHOCR(hocrData) {
const hocrContainer = document.getElementById('hocrContainer');
hocrContainer.innerHTML = ''; // Clear previous content
hocrData.forEach((data) => {
const { type, boundingBox, text } = data;
if (!boundingBox) return; // Skip elements without bounding box
const [x1, y1, x2, y2] = boundingBox.slice(1).map(parseFloat);
const hocrElement = document.createElement('div');
hocrElement.textContent = text;
hocrElement.className = type;
hocrElement.style.left = `${x1}px`;
hocrElement.style.top = `${y1}px`;
hocrElement.style.width = `${x2 - x1}px`;
hocrElement.style.height = `${y2 - y1}px`;
hocrContainer.appendChild(hocrElement);
});
}
function handleHOCRFile(event) {
const file = event.target.files[0];
const reader = new FileReader();
console.log(file);
reader.onload = function (e) {
const hocrContent = e.target.result;
const hocrData = parseHOCR(hocrContent);
visualizeHOCR(hocrData);
};
reader.readAsText(file);
}
function handleOpenImage(event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = function (e) {
const image = document.getElementById('image');
image.onload = function () {
const hocrContainer = document.getElementById('hocrContainer');
hocrContainer.style.width = `${image.width}px`;
hocrContainer.style.height = `${image.height}px`;
};
image.src = e.target.result;
};
reader.readAsDataURL(file);
}
const hocrInput = document.getElementById('hocrInput');
hocrInput.addEventListener('change', handleHOCRFile);
const openImageBtn = document.getElementById('openImageBtn');
openImageBtn.addEventListener('change', handleOpenImage);
function toggleElementVisibility(className) {
const elements = document.getElementsByClassName(className);
for (const element of elements) {
element.style.display = element.style.display === 'none' ? 'block' : 'none';
}
}
function toggleTextContentVisibility() {
const wordElements = document.getElementsByClassName('ocrx_word');
for (const element of wordElements) {
element.style.color = element.style.color === 'black' ? '' : 'black';
}
}
const imageElement = document.getElementById('image');
function toggleImageVisibility() {
imageElement.style.display = imageElement.style.display === 'none' ? 'block' : 'none';
}
const imageContainer = document.getElementById('hocrImageContainer');
const coordinatesDisplay = document.getElementById('coordinates');
imageContainer.addEventListener('mouseenter', () => {
coordinatesDisplay.style.display = 'block';
});
imageContainer.addEventListener('mousemove', (e) => {
const x = e.clientX;
const y = e.clientY;
const containerRect = imageContainer.getBoundingClientRect();
const offsetX = x - Math.round(containerRect.left);
const offsetY = y - Math.round(containerRect.top);
coordinatesDisplay.style.left = x + window.pageXOffset + 'px';
coordinatesDisplay.style.top = y + window.pageYOffset + 'px';
coordinatesDisplay.innerHTML = `X: ${offsetX} <br/>Y: ${offsetY}`;
});
imageContainer.addEventListener('mouseleave', () => {
coordinatesDisplay.style.display = 'none';
});