The native Node.js bindings to the Tesseract OCR project using N-API and node-addon-api.
Benefits:
- Avoid spawning
tesseract
command line. - Asynchronous I/O: Image reading and processing in insulated event loop backed by libuv.
- Support to read image data from JavaScript
buffer
s.
Contributions are welcome.
Via npm:
$ npm install node-native-ocr
import { recognize } from "node-native-ocr";
import fs from "fs-extra";
const filepath = path.join(
__dirname,
"test",
"fixtures",
"node-native-ocr.jpg"
);
fs.readFile(filepath).then(recognize).then(console.log); // 'node-native-ocr'
- image
Buffer
the content buffer of the image file. - options
node-native-ocrOptions=
optional
Returns Promise.<String>
the recognized text if succeeded.
{
// @type `(String|Array.<String>)=eng`,
//
// Specifies language(s) used for OCR.
// Run `tesseract --list-langs` in command line for all supported languages.
// Defaults to `'eng'`.
//
// To specify multiple languages, use an array.
// English and Simplified Chinese, for example:
// ```
// lang: ['eng', 'chi_sim']
// ```
lang: "eng";
}
- error
Error
The JavaScriptError
instance- code
String
Error code. - message
String
Error message. - other properties of
Error
.
- code
Rejects if it fails to read image data from file or buffer.
Rejects if tesseract fails to initialize
// For details of `mainWindow: BrowserWindow`, see
// https://github.com/electron/electron/blob/master/docs/api/browser-window.md
mainWindow.capturePage(
{
x: 10,
y: 10,
width: 100,
height: 10,
},
(data) => {
const appPath = (electron.app || electron.remote.app).getAppPath();
const tessdataPath = path.resolve(appPath, ocrPackagePath, "tessdata");
recognize(data.toPNG(), {
lang: ["eng", "ita"],
// output can be 'tsv' or 'txt'
output: "txt",
tessdataPath,
}).then(console.log);
}
);
For Mac OS users, if you are experiencing trouble when compiling, run the following command:
$ xcode-select --install
will resolve most problems.
Warnings:
xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance
resolver:
$ sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
After doing a lot of research and trying to compile other node OCR packages for electron without success, I decided to create my own. Based on N-API, which would save me from a lot of trouble. Now it is used in Repeato, a low-code mobile app testing tool which works based on Computer Visions. Node-native-ocr enables Repeato to do text recognition and text assertions on Android and iOS and acrross all platforms such as React Native, Flutter or Unity.
Let me know about your projects too, and I can add them here to the list!
If you set the output option to 'tsv', you will get a table of data, cells separated by tabs. Since I had a hard time finding the format for it, I will paste it here: This is the original reference: https://www.tomrochette.com/tesseract-tsv-format
- level: hierarchical layout (a word is in a line, which is in a paragraph, which is in a block, which is in a page), a value from 1 to 5
- 1: page
- 2: block
- 3: paragraph
- 4: line
- 5: word
- page_num: when provided with a list of images, indicates the number of the file, when provided with a multi-pages document, indicates the page number, starting from 1
- block_num: block number within the page, starting from 0
- par_num: paragraph number within the block, starting from 0
- line_num: line number within the paragraph, starting from 0
- word_num: word number within the line, starting from 0
- left: x coordinate in pixels of the text bounding box top left corner, starting from the left of the image
- top: y coordinate in pixels of the text bounding box top left corner, starting from the top of the image
- width: width of the text bounding box in pixels
- height: height of the text bounding box in pixels
- conf: confidence value, from 0 (no confidence) to 100 (maximum confidence), -1 for all level except 5
- text: detected text, empty for all levels except 5
To build the node-native-ocr project for testing, navigate into the node-native-ocr directory and exec
npm run install
I had problems with some node versions. A version which definitelly worked for me was v16.13.0.
After changing the code, and rebuilding the project, just run npm run test
To test changes, compilation is needed before. prebuild will only recompile if there is no precompiled version for your platform in the "prebuilds" dir. That's we have to delete it before starting the compilation. You can use npm run rebuild-and-test
for that
MIT