-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ocr.js
More file actions
65 lines (51 loc) · 2.16 KB
/
Copy pathtest_ocr.js
File metadata and controls
65 lines (51 loc) · 2.16 KB
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
import fs from 'fs';
import { createCanvas, loadImage } from 'canvas';
import Tesseract from 'tesseract.js';
async function testOCR() {
console.log('Loading image...');
const image = await loadImage('./chart.png');
console.log(`Image dimensions: ${image.width}x${image.height}`);
// Crop the right ~18% of the image (price scale region)
const canvas = createCanvas(image.width, image.height);
const ctx = canvas.getContext('2d');
const cropX = Math.floor(image.width * 0.82);
const cropWidth = image.width - cropX;
canvas.width = cropWidth;
canvas.height = image.height;
ctx.drawImage(image, cropX, 0, cropWidth, image.height, 0, 0, cropWidth, image.height);
// Preprocess: enhance contrast
const imageData = ctx.getImageData(0, 0, cropWidth, image.height);
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
const gray = data[i] * 0.299 + data[i + 1] * 0.587 + data[i + 2] * 0.114;
const enhanced = gray < 128 ? gray * 0.5 : 128 + (gray - 128) * 1.5;
const val = enhanced > 140 ? 255 : 0;
data[i] = val;
data[i + 1] = val;
data[i + 2] = val;
}
ctx.putImageData(imageData, 0, 0);
// Save debug image
const buffer = canvas.toBuffer('image/png');
fs.writeFileSync('./debug_ocr_crop.png', buffer);
console.log('Saved cropped price scale to debug_ocr_crop.png');
console.log('Running OCR...');
const worker = await Tesseract.createWorker('eng', 1);
const result = await worker.recognize(canvas.toBuffer());
console.log('\n--- RAW TEXT ---');
console.log(result.data.text);
console.log('\n--- PARSED LIST ---');
if (result.data.words) {
for (const word of result.data.words) {
let text = word.text.replace(/[^0-9.,\-]/g, '').replace(/,/g, '');
const value = parseFloat(text);
if (!isNaN(value)) {
const bbox = word.bbox;
const y = Math.round((bbox.y0 + bbox.y1) / 2);
console.log(`Price: ${value}, Y: ${y}, Raw text: ${word.text}`);
}
}
}
await worker.terminate();
}
testOCR().catch(console.error);