How To
Use Node.js
Using the native RustO! N-API addon in Node.js server and desktop applications.
The @rustors/node package provides high-performance native N-API bindings for Node.js, compiling directly against the pure-Rust RustO! engine with zero IPC or child process overhead.
Installation
npm install @rustors/node
Ensure standard build tools (Rust & Node N-API toolchain) are present if compiling from source, or download prebuilt releases.
1. Engine Initialization
Initialize the engine once by passing a JSON configuration string specifying detection, recognition, and dictionary paths:
const { RustO } = require('@rustors/node');
const config = {
detection: {
model_path: 'models/PPOCR_v6/det.rten'
},
recognition: {
model_path: 'models/PPOCR_v6/rec.rten',
dict_path: 'models/PPOCR_v6/dict.txt'
}
};
const engine = new RustO(JSON.stringify(config));
2. Text Recognition (detectTextFile & detectTextData)
From Filesystem Path
const options = {
text_score: 0.5,
output: 'lines'
};
const results = engine.detectTextFile('receipt.jpg', JSON.stringify(options));
results.forEach((item, index) => {
console.log(`[Line ${index + 1}] ${item.text} (${(item.score * 100).toFixed(1)}%)`);
console.log(` Bounding Box: ${JSON.stringify(item.box_points)}`);
console.log(` Frame: left=${item.frame.left}, top=${item.frame.top}, w=${item.frame.width}, h=${item.frame.height}`);
});
From Buffer in Memory
const fs = require('fs');
const imageBuffer = fs.readFileSync('document.png');
const results = engine.detectTextData(imageBuffer, JSON.stringify({}));
console.log(`Detected ${results.length} text segments.`);
3. Spatial Layout Reconstruction
To preserve two-dimensional formatting, columns, tables, and receipts:
const spatialText = engine.detectTextFileSpatial('invoice.png', JSON.stringify({
line_y_threshold: 0.5,
word_x_threshold: 0.4
}));
console.log('--- Reconstructed Spatial Document ---');
console.log(spatialText);
4. TypeScript Support
@rustors/node includes complete TypeScript type definitions:
import { RustO, TextResult, Frame } from '@rustors/node';
const engine: RustO = new RustO(JSON.stringify(config));
const results: TextResult[] = engine.detectTextFile('invoice.png', '{}');