Skip to content

UmiOCR EN

皮卡超人 edited this page Dec 23, 2025 · 2 revisions

OCR Recognition

Language: 中文 (Chinese) | English

WodToolKit integrates UmiOCR, providing convenient image text recognition functionality. UmiOCR is an open-source OCR recognition service that supports multiple languages and output formats.

Feature Overview

The WodToolKit.src.UmiOCR namespace provides two core classes:

  • OCR: image text recognition (/api/ocr)
  • Doc: document/PDF recognition (/api/doc)

Main capabilities:

  • Image text recognition
  • Document/PDF recognition and generation of searchable PDF, TXT, JSONL, etc.
  • Support for multiple recognition languages (Chinese, English, Chinese-English mixed, etc.)
  • Support for multiple output formats (text, json, jsonl)
  • Automatic angle detection
  • Ignore area settings
  • Custom UmiOCR service address

Prerequisites

Before using UmiOCR functionality, you need to start the UmiOCR service. UmiOCR is an independent OCR service program that runs on http://127.0.0.1:1224 by default.

Installing UmiOCR

  1. Visit the UmiOCR official website to download and install
  2. Start the UmiOCR service
  3. Ensure the service is running on the default port 1224 (or use a custom address)

Basic Usage

Creating Instances

using WodToolKit.src.UmiOCR.Ocr;
using WodToolKit.src.UmiOCR.Doc;

// Image OCR (/api/ocr)
var ocr = new OCR();                    // Default http://127.0.0.1:1224
var customOcr = new OCR("http://192.168.1.100:1224");

// Document OCR (/api/doc)
var doc = new Doc();                    // Default http://127.0.0.1:1224

Basic Image OCR

// Simplest usage
string result = ocr.Ocr("image.jpg");
Console.WriteLine(result);

Parameter Description

The Ocr method supports the following parameters:

Parameter Type Default Description
file string Required Image file path
language string "简体中文" Recognition language, options: Simplified Chinese, Traditional Chinese, English, Japanese, etc.
format string "text" Output format, options: text (plain text), json, jsonl
parser string "none" Post-processing parser, options: none, merge_line, merge_line_v2, etc.
angle bool false Whether to automatically detect image angle
ignoreArea List<int[][]> null Ignore area array, each item is [[top-left x,y],[bottom-right x,y]]

Usage Examples

Specifying Recognition Language

// Recognize Simplified Chinese
string result = umiOcr.Ocr("image.jpg", language: "简体中文");

// Recognize English
string result = umiOcr.Ocr("image.jpg", language: "英文");

// Recognize Chinese-English mixed
string result = umiOcr.Ocr("image.jpg", language: "简体中文+英文");

Specifying Output Format

// Output plain text (default)
string textResult = umiOcr.Ocr("image.jpg", format: "text");

// Output JSON format
string jsonResult = umiOcr.Ocr("image.jpg", format: "json");

// Output JSONL format
string jsonlResult = umiOcr.Ocr("image.jpg", format: "jsonl");

Enabling Angle Detection

// Automatically detect image angle and rotate
string result = umiOcr.Ocr("image.jpg", angle: true);

Setting Ignore Areas

Ignore areas are used to specify regions in the image that do not need recognition. Each area is defined by top-left and bottom-right coordinates.

using System.Collections.Generic;

// Define ignore areas
var ignoreAreas = new List<int[][]>
{
    // First ignore area: from (10, 20) to (100, 200)
    new int[][] { new int[] { 10, 20 }, new int[] { 100, 200 } },
    
    // Second ignore area: from (150, 250) to (300, 400)
    new int[][] { new int[] { 150, 250 }, new int[] { 300, 400 } }
};

string result = umiOcr.Ocr("image.jpg", ignoreArea: ignoreAreas);

Using Post-Processing Parser

// No post-processing (default)
string result1 = umiOcr.Ocr("image.jpg", parser: "none");

// Use line merge parser
string result2 = umiOcr.Ocr("image.jpg", parser: "merge_line");

// Use line merge parser v2
string result3 = umiOcr.Ocr("image.jpg", parser: "merge_line_v2");

Complete Parameter Example

var ignoreAreas = new List<int[][]>
{
    new int[][] { new int[] { 10, 20 }, new int[] { 100, 200 } }
};

string result = umiOcr.Ocr(
    file: "image.jpg",
    language: "简体中文",
    format: "json",
    parser: "merge_line",
    angle: true,
    ignoreArea: ignoreAreas
);

Error Handling

try
{
    string result = umiOcr.Ocr("image.jpg");
    Console.WriteLine($"Recognition successful: {result}");
}
catch (Exception ex)
{
    Console.WriteLine($"OCR recognition failed: {ex.Message}");
}

API Reference

UmiOCR Constructor

public UmiOCR(string? Url = "http://127.0.0.1:1224")

Parameters:

  • Url (string?, optional): UmiOCR service address, defaults to "http://127.0.0.1:1224"

Ocr Method

public string Ocr(
    string file, 
    string language = "简体中文", 
    string format = "text", 
    string parser = "none", 
    bool angle = false, 
    List<int[][]> ignoreArea = null
)

Parameters:

  • file (string): Image file path
  • language (string): Recognition language, defaults to "简体中文"
  • format (string): Output format, defaults to "text"
  • parser (string): Post-processing parser, defaults to "none"
  • angle (bool): Whether to automatically detect image angle, defaults to false
  • ignoreArea (List<int[][]>): Ignore area array, defaults to null

Returns:

  • string: OCR recognition result string

Exceptions:

  • Throws Exception if OCR recognition fails, with error message containing the failure reason

Notes

  1. Service Requirement: Ensure UmiOCR service is running before use
  2. Image Formats: Supports common image formats (jpg, png, bmp, etc.)
  3. File Paths: Supports both absolute and relative paths
  4. Performance: OCR recognition is a network request and may take some time, recommended to use in async environments
  5. Error Handling: Recommended to use try-catch to catch possible exceptions

Use Cases

  • Image text extraction
  • Document scanning recognition
  • Screenshot text recognition
  • Batch image OCR processing
  • Form data extraction

Related Links

Clone this wiki locally