npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-native-ocr

v0.3.9

Published

Native Node.js bindings for the Tesseract OCR project.

Downloads

786

Readme

Build Status Coverage

node-native-ocr

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 buffers.

Contributions are welcome.

Install

Via npm:

$ npm install node-native-ocr

Usage

Recognize an Image Buffer

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'

recognize(image [, options])

  • image Buffer the content buffer of the image file.
  • options node-native-ocrOptions= optional

Returns Promise.<String> the recognized text if succeeded.

node-native-ocrOptions Object

{
  // @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";
}

Promise.reject(error)

  • error Error The JavaScript Error instance
    • code String Error code.
    • message String Error message.
    • other properties of Error.

code: ERR_READ_IMAGE

Rejects if it fails to read image data from file or buffer.

code: ERR_INIT_TESSER

Rejects if tesseract fails to initialize

Example of Using with Electron

// 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);
  }
);

Compiling Troubles

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

Why another node OCR package?

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!

TSV format description

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

There are 12 columns in the table:

  1. 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
  2. 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
  3. block_num: block number within the page, starting from 0
  4. par_num: paragraph number within the block, starting from 0
  5. line_num: line number within the paragraph, starting from 0
  6. word_num: word number within the line, starting from 0
  7. left: x coordinate in pixels of the text bounding box top left corner, starting from the left of the image
  8. top: y coordinate in pixels of the text bounding box top left corner, starting from the top of the image
  9. width: width of the text bounding box in pixels
  10. height: height of the text bounding box in pixels
  11. conf: confidence value, from 0 (no confidence) to 100 (maximum confidence), -1 for all level except 5
  12. text: detected text, empty for all levels except 5

Development notes

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

License

MIT