-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
189 lines (155 loc) · 7.12 KB
/
app.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
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
document.addEventListener('DOMContentLoaded', function() {
const video = document.getElementById('camera');
const canvas = document.getElementById('photo');
const captureButton = document.getElementById('capture');
const statusText = document.getElementById('status');
const postcodeElement = document.getElementById('postcode');
const correctedImageContainer = document.getElementById('corrected-image-container');
function getUHVVideoStream() {
navigator.mediaDevices.enumerateDevices().then(devices => {
const videoDevices = devices.filter(device => device.kind === 'videoinput');
if (videoDevices.length === 0) {
alert('No video devices found.');
return;
}
const uhvCamera = videoDevices.find(device => device.label.toLowerCase().includes('uhv')) || videoDevices[0];
console.log('Using camera:', uhvCamera.label);
const constraints = {
video: {
deviceId: { exact: uhvCamera.deviceId }
}
};
return navigator.mediaDevices.getUserMedia(constraints);
}).then(stream => {
video.srcObject = stream;
}).catch(error => {
console.error("Error accessing the UHV camera", error);
alert("Unable to access the UHV camera. Please make sure you have granted permission.");
});
}
getUHVVideoStream();
function rotateImage(image, angle) {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = image.width;
canvas.height = image.height;
context.translate(canvas.width / 2, canvas.height / 2);
context.rotate(angle * Math.PI / 180);
context.drawImage(image, -image.width / 2, -image.height / 2);
return canvas;
}
function drawDetectedText(words, canvas) {
const context = canvas.getContext('2d');
context.strokeStyle = 'blue';
context.lineWidth = 2;
words.forEach(word => {
const bbox = word.bbox;
const x = bbox.x0;
const y = bbox.y0;
const w = bbox.x1 - bbox.x0;
const h = bbox.y1 - bbox.y0;
context.strokeRect(x, y, w, h);
});
}
function onOpenCVLoaded() {
if (typeof cv === 'undefined') {
console.error("OpenCV.js is not loaded.");
statusText.textContent = "Error: OpenCV.js is not loaded.";
return;
}
console.log("OpenCV.js is initialized.");
statusText.textContent = "OpenCV.js is ready. Click 'Capture Photo' to proceed.";
}
cv['onRuntimeInitialized'] = onOpenCVLoaded;
captureButton.addEventListener('click', () => {
if (typeof cv === 'undefined') {
console.error("OpenCV.js is not loaded.");
statusText.textContent = "Error: OpenCV.js is not loaded.";
return;
}
const context = canvas.getContext('2d');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
console.log('Capturing photo with dimensions:', canvas.width, 'x', canvas.height);
context.drawImage(video, 0, 0, canvas.width, canvas.height);
const imageDataURL = canvas.toDataURL('image/png');
console.log('Captured image data URL:', imageDataURL);
const img = new Image();
img.src = imageDataURL;
img.classList.add('captured');
document.body.appendChild(img);
statusText.textContent = "Processing image...";
Tesseract.recognize(
imageDataURL,
'eng',
{
logger: m => console.log(m),
tessedit_pageseg_mode: Tesseract.PSM.AUTO
}
).then(({ data: { text, words } }) => {
console.log('Recognized text:', text);
console.log('Detected words:', words);
statusText.textContent = "Analyzing text orientation...";
if (Array.isArray(words) && words.length > 0) {
const canvasForText = document.createElement('canvas');
canvasForText.width = canvas.width;
canvasForText.height = canvas.height;
const contextForText = canvasForText.getContext('2d');
contextForText.drawImage(canvas, 0, 0);
drawDetectedText(words, canvasForText);
let averageAngle = 0;
let count = 0;
words.forEach(word => {
const bbox = word.bbox;
const w = bbox.x1 - bbox.x0;
const h = bbox.y1 - bbox.y0;
if (w && h) {
const angle = Math.atan2(h, w) * 180 / Math.PI;
averageAngle += angle;
count++;
}
});
if (count > 0) {
averageAngle /= count;
console.log("Calculated rotation angle:", averageAngle);
const correctedCanvas = rotateImage(canvas, -averageAngle);
const correctedImageDataURL = correctedCanvas.toDataURL('image/png');
const correctedImg = new Image();
correctedImg.src = correctedImageDataURL;
correctedImg.classList.add('captured');
correctedImageContainer.innerHTML = '';
correctedImageContainer.appendChild(correctedImg);
Tesseract.recognize(
correctedImageDataURL,
'eng',
{
logger: m => console.log(m),
tessedit_pageseg_mode: Tesseract.PSM.AUTO
}
).then(({ data: { text } }) => {
console.log('Recognized text after correction:', text);
statusText.textContent = "Image processed. Searching for postcode...";
const postcodePattern = /\b[A-Z]{1,2}\d[A-Z\d]? \d[A-Z]{2}\b/i;
const postcodeMatch = text.match(postcodePattern);
if (postcodeMatch) {
postcodeElement.textContent = postcodeMatch[0].toUpperCase();
} else {
postcodeElement.textContent = "No postcode found.";
}
statusText.textContent = "Done.";
}).catch(error => {
console.error("Error processing image with Tesseract.js", error);
statusText.textContent = "Error processing image.";
});
} else {
statusText.textContent = "No text detected for rotation.";
}
} else {
statusText.textContent = "Error: No words detected.";
}
}).catch(error => {
console.error("Error with Tesseract.js", error);
statusText.textContent = "Error processing image.";
});
});
});