Guten OCR

repository·main·Indexed 41 days ago

https://github.com/gutenye/ocr

A multi-platform JavaScript OCR library supporting Node.js, Browser, React Native, and C++. Based on PaddleOCR and ONNX Runtime, it supports the PP-OCRv4 model for text detection and recognition. The library provides a unified API across platforms via packages like @gutenye/ocr-node, @gutenye/ocr-browser, and @gutenye/ocr-react-native.

Tokens
9.9K
Snippets
49
Records
57
Agent score
47%

What's inside Guten OCR

  1. Install and use Guten OCR in the Browser

    main

    To use Guten OCR in a web browser, install @gutenye/ocr-browser. When calling Ocr.create(), you must provide a models configuration object specifying the paths to the required ONNX model files and the dictionary file.

    bun add @gutenye/ocr-browser
    import Ocr from '@gutenye/ocr-browser'
    const ocr = await Ocr.create({
      models: {
        detectionPath: '/assets/ch_PP-OCRv4_det_infer.onnx',
        recognitionPath: '/assets/ch_PP-OCRv4_rec_infer.onnx',
        dictionaryPath: '/assets/ppocr_keys_v1.txt'
      }
    })
    const result = await ocr.detect('/a.jpg')
  2. Set up the C++ Example

    main

    To run the C++ example, you must install the ONNX Runtime and OpenCV dependencies, then use the provided build tool to start the project.

    1. Download the appropriate onnxruntime release from the Microsoft ONNX Runtime releases page.
    2. Install OpenCV via Homebrew.
    3. Run the project using ./ake start.
    download onnxruntime from https://github.com/microsoft/onnxruntime/releases
    brew install opencv
    ./ake start
  3. Use Guten OCR in C++

    main

    For C++ environments, include native-ocr.h and instantiate a NativeOcr object to perform detection.

    #include "native-ocr.h"
    NativeOcr* ocr = new NativeOcr(..)
    auto result = ocr->detect("a.jpg");
  4. Setup @gutenye/ocr-react-native

    main

    To set up the React Native package, navigate to the package directory and copy the required assets into a resources folder.

    Important Limitation: The iOS Simulator is not supported on Apple Silicon (M1/M2/M3) because ExpoModulesCore can only run on the x86_64 simulator via Rosetta. You must use a physical device or an x86_64 simulator environment.

    cd packages/react-native
    cp -rL ../../assets resources
  5. Install and use Guten OCR in Node.js

    main

    To use Guten OCR in a Node.js environment, install the @gutenye/ocr-node package. You can initialize the OCR engine using Ocr.create() and then perform text detection on an image file path using the .detect() method.

    bun add @gutenye/ocr-node
    import Ocr from '@gutenye/ocr-node'
    const ocr = await Ocr.create()
    const result = await ocr.detect('a.jpg')
  6. Install and use Guten OCR in React Native

    main

    To use Guten OCR in a React Native project, install @gutenye/ocr-react-native. The engine can be initialized and used similarly to the Node.js implementation.

    bun add @gutenye/ocr-react-native
    import Ocr from '@gutenye/ocr-react-native'
    const ocr = await Ocr.create()
    const result = await ocr.detect('a.jpg')
  7. Run the Browser Example

    main

    To run the browser-based example project, you must first install the dependencies for the entire monorepo and then install the local dependencies for the example directory. Finally, use the ake build tool to start the development server.

    Ensure you are in the correct directory relative to the browser/example folder when running these commands.

    cd ../.. && bun install
    cd browser/example
    bun install
    ./ake start
  8. Run the Node.js example project

    main

    To run the Node.js example, you must first install the dependencies for the entire repository (monorepo) and then install the dependencies specific to the examples/node directory. Finally, use the ake command to start the example with a specific image asset.

    Ensure you are in the examples/node directory before running the local installation and start commands.

    # From the examples/node directory:
    cd ../.. && bun install
    cd examples/node
    bun install
    ./ake start assets/cn-01.jpg
  9. Use @gutenye/ocr-browser in the browser

    main

    The @gutenye/ocr-browser package provides a browser-compatible implementation of the Guten OCR API. It automatically registers the necessary browser-specific backends (such as onnxruntime-web for inference and FileUtils/ImageRaw for image handling) so you can use the standard Ocr interface.

    To use it, import the default Ocr export from the package. This allows you to perform OCR tasks directly in web environments using the same API surface as the Node.js or React Native versions.

    import Ocr from '@gutenye/ocr-browser';
    
    // Use the Ocr instance as you would in any other environment
    // const results = await Ocr.recognize(imageSource);
  10. Troubleshoot C++ Example dependencies

    main

    If you encounter errors during execution, check the following common dependency issues:

    • Missing libavcodec.60.dylib: Reinstall ffmpeg@6 using Homebrew.
    • OpenCV not found: Reinstall opencv using Homebrew.
    - libavcodec.60.dylib not found: brew reinstall ffmpeg@6
    - OpenCV not found: brew resinstall opencv