RapidOcr-Java Documentation

repository·main·Indexed 20 days ago

https://github.com/mymonstercat/rapidocr-java

A pure Java wrapper for RapidOCR that provides an interface for OCR tasks using ncnn and onnx inference engines for CPU-based recognition. It supports PP-OCRv3 and PP-OCRv4 models and provides platform-specific dependencies for CPU-based environments (rapidocr-onnx-platform) and mobile environments (rapidocr-ncnn-platform). The library allows for fine-tuning via OcrConfig parameters and supports various system architectures across Windows, macOS, and Linux.

Tokens
6.1K
Snippets
13
Records
21
Agent score
69%

What's inside RapidOcr-Java

  1. How to use custom models or binary data

    main

    The current stable release (v0.0.7) primarily supports image paths. To use custom models or pass binary image data, you must build the project from the main branch.

    Using Custom Models

    1. Clone the latest source code.
    2. Place your model files in rapidocr-onnx-models/src/main/resources/models.
    3. Add the corresponding configuration in Model.java.
    4. Rebuild the project.

    Passing Binary Data

    Binary data support is currently available for mac-arm64 in the latest source. For other platforms, you must follow the manual compilation steps described in Compile Lib and replace the dynamic libraries in the source before building.

  2. Compare OCR implementation methods in Java

    main

    When choosing how to implement OCR in a Java application, there are four primary approaches. RapidOcr-Java is the recommended method for most developers because it abstracts the complexity of OCR models through JNI (Java Native Interface).

    Comparison of Methods

    MethodPrincipleProsConsNotes
    RapidOcr-JavaJava calls compiled OCR dynamic libraries via JNIReady to use, cross-platform, no OCR expertise requiredLimited direct model control (parameters only)Recommended (This project)
    Direct ONNX RuntimeCalls and uses models via onnx-runtimeReady to use, cross-platform, full model controlRequires deep OCR knowledgeRecommended (but not open-sourced)
    DJL (Deep Java Library)Indirectly calls onnx-runtime via DJLNo need to manage platform dependencies during packagingHigh learning curve for DJL syntax; requires OCR knowledgeNot recommended
    API CallOCR service runs in a server/container; accessed via HTTPDecouples functionalityRequires managing separate services/containers (e.g., Docker)Use services like RapidOCR-web
  3. Analyze dynamic library dependencies with ldd

    main

    Before attempting to upgrade system components, verify if your environment meets the minimum requirements for libRapidOcr.so. Use the ldd -r command to check for missing symbols or version mismatches (e.g., GLIBCXX_3.4.26 not found).

    To ensure successful linking, your environment should ideally meet these minimums:

    • gcc >= 9.1.0
    • cmake >= 4
    • GLIBC >= 2.26

    Note: The library file is typically located at tmp/ocrJava/onnx/libRapid0cr.so or similar paths depending on your setup.

    ldd -r /path/to/libRapidOcr.so
  4. Upgrade CMake to version 4

    main

    If your system requires a newer version of CMake, you can compile it from source and install it to /usr/local/make to avoid conflicts, then symlink it to /usr/bin/make.

    wget http://ftp.gnu.org/gnu/make/make-4.3.tar.gz
    tar -xzvf make-4.3.tar.gz && cd make-4.3/
    ./configure --prefix=/usr/local/make
    make && make install
    cd /usr/bin/ && mv make make.bak
    ln -sv /usr/local/make/bin/make /usr/bin/make
  5. Add RapidOcr-Java dependencies

    main

    To use RapidOcr-Java, you must include the core rapidocr dependency and exactly one platform-specific inference engine dependency.

    • Use rapidocr-onnx-platform for CPU-based environments.
    • Use rapidocr-ncnn-platform for mobile environments.

    The dependencies automatically select the correct JAR based on your operating system. Check the Version Description for supported systems.

    <!-- Core dependency (required) -->
    <dependency>
        <groupId>io.github.mymonstercat</groupId>
        <artifactId>rapidocr</artifactId>
        <version>0.0.7</version>
    </dependency>
    
    <!-- Choose ONE engine: ONNX for CPU, NCNN for mobile -->
    <dependency>
        <groupId>io.github.mymonstercat</groupId>
        <artifactId>rapidocr-onnx-platform</artifactId>
        <version>0.0.7</version>
    </dependency>
    
    <!-- OR -->
    <dependency>
        <groupId>io.github.mymonstercat</groupId>
        <artifactId>rapidocr-ncnn-platform</artifactId>
        <version>0.0.7</version>
    </dependency>
  6. Upgrade GLIBC to 2.28

    main

    If ldd indicates that the required GLIBC version is missing, you can upgrade to 2.28. After installation, verify the supported versions using strings.

    wget http://ftp.gnu.org/gnu/glibc/glibc-2.28.tar.gz
    tar xf glibc-2.28.tar.gz 
    cd glibc-2.28/ && mkdir build && cd build
    ../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin --disable-werror
    make
    make install
    
    # Verify GLIBC versions
    strings /lib64/libc.so.6 | grep GLIBC_
  7. Compile the JNI dynamic library (ONNX)

    main

    To compile the JNI dynamic library using the ONNX backend, follow these steps to prepare the environment and build the project. This process involves cloning the core engine, downloading pre-built dependencies (OpenCV and ONNX Runtime), and running the build script.

    1. Download Dependencies

    • Project Source: Clone https://github.com/RapidAI/RapidOcrOnnx.
    • OpenCV: Download from OpenCVBuilder releases. Extract to a directory named opencv-static. Ensure the directory contains OpenCVWrapperConfig.cmake and platform-specific folders (e.g., macos, windows-x64).
    • ONNX Runtime: Download from OnnxruntimeBuilder releases. Use the static version. Extract to a directory named onnxruntime-static. Ensure the directory contains OnnxRuntimeWrapper.cmake and platform-specific folders.

    2. MacOS Environment Setup

    • Install Xcode >= 12 and Xcode Command Line Tools: xcode-select --install.
    • Install Homebrew and CMake >= 3.19.
    • Install libomp: brew install libomp.
    • Ensure a Java environment is installed.

    3. Code Modification (MacOS Only)

    Before building on macOS, you must update the include paths in the following files: AngleNet.h, CrnnNet.h, DbNet.h, OcrLite.h, and OcrUtils.h.

    Change: #include <onnxruntime/core/session/onnxruntime_cxx_api.h> To: #include <onnxruntime/onnxruntime_cxx_api.h>

    4. Build and Test

    1. Open a terminal in the project root.
    2. Run ./build.sh and follow the prompts. Select 'JNI动态库' (JNI Dynamic Library) when prompted.
    3. The resulting library (e.g., libRapidOcrOnnx.dylib on macOS) can be used in your project. Replace the existing library in your project's resource module with this new file.
    4. To test the build, run ./run-test.sh (ensure you update the image path within the script).
    # 1. Clone the engine
    git clone https://github.com/RapidAI/RapidOcrOnnx
    
    # 2. Build (after setting up opencv-static and onnxruntime-static)
    ./build.sh
    
    # 3. Test
    ./run-test.sh
  8. Update the RapidOCR model

    main

    To use a custom model, you must convert it to the required format using the RapidOCR model conversion process. Follow these steps:

    1. Convert the model: Use the PaddleOCRModelConverter on ModelScope to convert your chosen model.
    2. Deploy the model: Place the converted model file into the directory: src\main\resources\onnx\models.
    3. Update configuration: Modify the PathConstants.MODEL_REC_NAME constant in the source code to match the filename of your new model.
  9. Build Rapid OCR Onnx Lib on Kylin (arm64)

    main

    This guide provides instructions for building the Rapid OCR Onnx library on the Kylin (arm64) operating system (e.g., Kylin V10 SP1 on Phytium D2000/8 CPU).

    Compilation Requirements

    • gcc: > 7
    • g++: > 7
    • cmake: > 3.17

    Environment Setup

    1. Configure Kylin Software Sources

    If the Kylin repository is missing, add the appropriate source to /etc/apt/sources.list. For example, for V10:

    deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.0 main restricted universe multiverse

    2. Install GCC and G++

    You can download .deb packages for offline installation using apt:

    # Download only
    sudo apt install --download-only gcc
    sudo apt install --download-only g++
    
    # Packages are cached in /var/cache/apt/packages
    cd /var/cache/apt/packages
    
    # Install the downloaded packages
    sudo dpkg -i *.deb

    3. Install CMake (Manual Installation)

    Since apt may provide an outdated version (e.g., 3.16), download the tar.gz for Linux aarch64 from the official CMake website.

    # Extract to a directory, e.g., /home/some/soft/cmake
    tar -xzvf cmake-3.29.2-linux-aarch64.tar.gz -C /home/some/soft/
    
    # Add to PATH in ~/.bashrc
    export PATH=$PATH:/home/some/soft/cmake/bin
    source ~/.bashrc

    4. Configure JVM (Required for JNI)

    Download the aarch64 JDK from Oracle. You must set JAVA_HOME to build JNI components.

    # Extract to a directory, e.g., /home/some/soft/jdk
    export JAVA_HOME=/home/some/soft/jdk
    export PATH=$PATH:$JAVA_HOME/bin
    source ~/.bashrc
  10. Upgrade GCC to 9.1.0 on CentOS 7

    main

    If ldd reports missing GLIBCXX symbols, you may need to upgrade GCC.

    ⚠️ WARNING: Upgrading GCC carries significant risks to system stability. Always create a system snapshot or backup before proceeding in production environments.

    1. Download and Prepare

    Download the source, extract it, and use the included script to fetch required dependencies (mpfr, mpc, gmp).

    2. Compile and Install

    Configure the build with --enable-languages=c,c++ and --disable-multilib. Note that compilation can take 1-3 hours.

    After installation, locate the new libstdc++.so.6.0.26 (or higher) in /usr/local/lib64/ and update the system link in /usr/lib64/ to point to it.

    # 1. Download and dependencies
    mkdir temp && cd /temp
    wget http://ftp.gnu.org/gnu/gcc/gcc-9.1.0/gcc-9.1.0.tar.gz
    tar -zxvf gcc-9.1.0.tar.gz
    cd gcc-9.1.0
    ./contrib/download_prerequisites
    
    # 2. Build
    mkdir build && cd build
    ../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
    make
    make install
    
    # 3. Re-link libstdc++
    find / -name "libstdc++.so*"
    # Assuming /usr/local/lib64/libstdc++.so.6.0.26 is found:
    cd /usr/lib64
    cp /usr/local/lib64/libstdc++.so.6.0.26 /usr/lib64/
    rm libstdc++.so.6
    ln -s libstdc++.so.6.0.26 libstdc++.so.6
  11. Manually add Linux dependencies to pom.xml

    main

    Alternatively, instead of using Maven profiles, you can explicitly add the required Linux platform dependency directly to your <dependencies> section in pom.xml. This ensures the Linux native libraries are bundled regardless of the build environment's OS.

    <!-- Add the core rapidocr dependency and the specific linux platform dependency -->
    <dependencies>
        <dependency>
            <groupId>io.github.mymonstercat</groupId>
            <artifactId>rapidocr</artifactId>
            <version>x.x.x</version>
        </dependency>
      
        <dependency>
            <groupId>io.github.mymonstercat</groupId>
            <artifactId>rapidocr-onnx-linux-x86_64</artifactId>
            <version>x.x.x</version>
        </dependency>
    </dependencies>