uni-ocr

repository·main·Indexed 42 days ago

https://github.com/screenpipe/uniocr

A universal OCR engine for Rust providing a unified API for native OS engines (macOS Vision, Windows OCR), Tesseract, and cloud providers like Google Cloud Vision. Version 0.1.5 supports automatic provider detection via OcrProvider::Auto, batch processing, and custom remote OCR endpoints.

Tokens
2.5K
Snippets
8
Records
11
Agent score
53%

What's inside uni-ocr

  1. Quickstart: Perform OCR on an image

    main

    To perform basic OCR, initialize an OcrEngine with OcrProvider::Auto to automatically detect the best available provider on your system, then use recognize_file to extract text from an image path.

    use uniocr::{OcrEngine, OcrProvider};
    use anyhow::Result;
    
    #[tokio::main]
    async fn main() -> Result<()> {
        // auto-detect best available provider
        let engine = OcrEngine::new(OcrProvider::Auto)?;
        
        // perform ocr on an image
        let text = engine.recognize_file("path/to/image.png").await?;
        println!("extracted text: {}", text);
        
        Ok(())
    }
  2. Run project examples

    main

    The repository contains several example programs that you can run using cargo run. These demonstrate different use cases like batch processing and custom options:

    # basic example
    cargo run --example basic
    
    # batch processing
    cargo run --example batch_processing
    
    # custom options
    cargo run --example custom_options
    
    # platform specific
    cargo run --example platform_specific
  3. Install Tesseract system requirements

    main

    If you intend to use OcrProvider::Tesseract, you must install the tesseract-ocr binary on your operating system:

    macOS

    brew install tesseract

    Ubuntu

    apt-get install tesseract-ocr

    Windows

    winget install tesseract
  4. Install uni-ocr in a Rust project

    main

    To use uni-ocr in your Rust project, add it to your Cargo.toml dependencies. You can pull it directly from the GitHub repository:

    [dependencies]
    uni-ocr = { git = "https://github.com/mediar-ai/uniocr.git" }
  5. Configure OcrOptions and Batch Processing

    main

    You can fine-tune OCR behavior using OcrOptions. This allows you to set supported languages, confidence thresholds, and timeouts. Use .with_options(options) on the OcrEngine to apply these settings. For processing multiple images efficiently, use recognize_batch.

    use uni_ocr::{OcrEngine, OcrProvider, OcrOptions};
    
    // configure ocr options
    let options = OcrOptions::default()
        .languages(vec!["eng", "fra"])
        .confidence_threshold(0.8)
        .timeout(std::time::Duration::from_secs(30));
    
    let engine = OcrEngine::new(OcrProvider::Auto)?
        .with_options(options);
    
    // batch processing
    let images = vec!["img1.png", "img2.png", "img3.png"];
    let results = engine.recognize_batch(images).await?;
  6. Configure Credentials for Custom OCR

    main

    When using a custom OCR provider, you must provide a Credentials object to specify the connection details.

    Fields:

    • api_url: The endpoint URL for the OCR service (defaults to http://localhost:8000/ocr).
    • api_key: The bearer token used for authorization.
    • timeout_ms: The request timeout in milliseconds (defaults to 5000).
    // Example of manual Credentials construction
    let credentials = Credentials {
        api_url: "https://your-ocr-service.com/ocr".to_string(),
        api_key: "your_secret_token".to_string(),
        timeout_ms: 10000,
    };
  7. Available OcrProviders

    main

    The OcrProvider enum allows you to specify which engine to use for text recognition. Supported providers include:

    • OcrProvider::Auto: Automatically selects the best available provider for the current platform.
    • OcrProvider::MacOS: Uses the native macOS Vision Kit API.
    • OcrProvider::Windows: Uses the native Windows OCR engine.
    • OcrProvider::Tesseract: Uses the Tesseract OCR engine (requires tesseract-ocr to be installed on the system).
    • OcrProvider::GoogleCloud: Uses Google Cloud Vision (requires credentials).
    // use native macos vision
    let engine = OcrEngine::new(OcrProvider::MacOS)?;
    
    // use windows ocr
    let engine = OcrEngine::new(OcrProvider::Windows)?;
    
    // use tesseract
    let engine = OcrEngine::new(OcrProvider::Tesseract)?;
    
    // use google cloud vision
    // let engine = OcrEngine::new(OcrProvider::GoogleCloud {
    //     credentials: ..., 
    // })?;
  8. Initialize and use the OcrEngine

    main

    The OcrEngine is the primary entry point for performing Optical Character Recognition (OCR) in uniocr. You initialize it by specifying an OcrProvider and can optionally configure it with OcrOptions using a builder-like pattern.

    Supported Providers

    • OcrProvider::Auto: Automatically selects the best available provider for the current platform (macOS uses Apple OCR, Windows uses Microsoft OCR, others use Tesseract).
    • OcrProvider::MacOS: Uses Apple's native OCR (macOS only).
    • OcrProvider::Windows: Uses Microsoft's native OCR (Windows only).
    • OcrProvider::Tesseract: Uses the Tesseract engine.
    • OcrProvider::Custom { credentials }: Uses a custom provider with provided Credentials.

    Recognition Methods

    • recognize_image(&self, image: &DynamicImage): Performs OCR on an in-memory image::DynamicImage.
    • recognize_file(&self, path: &str): Performs OCR on an image file at the specified path.
    • recognize_batch(&self, paths: Vec<&str>): Performs OCR on a list of image file paths sequentially.

    Returns a Result containing a tuple: (String, String, Option<f64>) representing (detected_text, language_code, confidence_score).

    use uniocr::{OcrEngine, OcrProvider, OcrOptions};
    use image::DynamicImage;
    
    #[tokio::main]
    async fn main() -> anyhow::Result<()> {
        // Initialize engine with Auto provider and custom options
        let engine = OcrEngine::new(OcrProvider::Auto)?
            .with_options(OcrOptions::default()
                .languages(vec![Language::English])
                .confidence_threshold(0.8)
            );
    
        // Recognize an image file
        let (text, lang, confidence) = engine.recognize_file("path/to/image.png").await?;
        println!("Text: {}, Lang: {}, Confidence: {:?}", text, lang, confidence);
    
        Ok(())
    }
  9. Perform OCR using perform_ocr_custom

    main

    The perform_ocr_custom function allows you to send a DynamicImage to a custom remote OCR endpoint.

    Request Payload: The function automatically converts the image to a base64-encoded JPEG and sends a JSON payload containing:

    • image: The base64 string of the JPEG image.
    • languages: A list of language strings.

    Authentication: It uses the Authorization: Bearer <api_key> header.

    Returns: A Result containing a tuple of (String, String, Option<f64>) representing:

    1. The extracted text.
    2. The structured data (as a JSON string).
    3. The confidence score (optional).
    // Example usage of perform_ocr_custom
    let result = perform_ocr_custom(
        &image,
        vec![Language::English],
        &credentials,
    ).await?;
    
    let (text, structured_data, confidence) = result;
  10. Configure OcrOptions

    main

    OcrOptions allows you to fine-tune the OCR process. It implements Default, which sets the language to English, a confidence_threshold of 0.0, and a timeout of 30 seconds.

    You can customize these settings using the following builder methods:

    • .languages(langs: Vec<Language>): Sets the list of languages to be used during recognition.
    • .confidence_threshold(threshold: f32): Sets the minimum confidence score required.
    • .timeout(timeout: Duration): Sets the maximum duration to wait for the OCR operation to complete.
    use uniocr::OcrOptions;
    use std::time::Duration;
    
    let options = OcrOptions::default()
        .languages(vec![Language::English, Language::French])
        .confidence_threshold(0.5)
        .timeout(Duration::from_secs(10));
  11. OcrProvider enum variants

    main

    The OcrProvider enum determines which OCR engine is used by the OcrEngine.

    VariantDescription
    AutoSelects macOS (Apple), Windows (Microsoft), or Tesseract based on the host OS.
    MacOSUses Apple's native OCR (requires macOS).
    WindowsUses Microsoft's native OCR (requires Windows).
    TesseractUses the Tesseract OCR engine.
    Custom { credentials }Uses a custom implementation requiring Credentials.