CnOCR Documentation

repository·master·Indexed 26 days ago

https://github.com/breezedeus/cnocr

A Python 3 toolkit for Optical Character Recognition (OCR) supporting Simplified Chinese, Traditional Chinese, English, and numbers. It features pre-trained models for scene images, documents, and single-line text, with support for PyTorch and ONNX backends. The toolkit includes a FastAPI-based HTTP service, CLI tools for prediction, training, evaluation, and ONNX export, as well as visualization utilities for debugging detection and recognition results.

Tokens
13.7K
Snippets
42
Records
84
Agent score
85%

What's inside CnOCR

  1. Overview of CnOCR

    master
    CnOCR is an Optical Character Recognition (OCR) toolkit for Python 3. It is designed to recognize English, numbers, Simplified Chinese, Traditional Chinese (via specific models), and vertical text. The toolkit includes over 20 pre-trained models optimized for various scenarios and supports training custom models. Since version 2.2, it utilizes the CnSTD engine for text detection and positioning, enabling it to recognize scene text in general photography as well as document screenshots.
  2. Fine-tune an existing model

    master

    To fine-tune an existing model instead of training from scratch, you should use a smaller learning rate and configure the lr_scheduler. Avoid over-fitting during fine-tuning.

    Example configuration for fine-tuning:

    • Set learning_rate to a small value (e.g., 3e-5).
    • Configure lr_scheduler with cos_warmup and appropriate warmup epochs.
    {
      "learning_rate": 3e-5,
      "lr_scheduler": {
        "name": "cos_warmup",
        "min_lr_mult_factor": 0.01,
        "warmup_epochs": 2
      }
    }
  3. Start an HTTP API service with `cnocr serve`

    master

    CnOCR provides a FastAPI-based HTTP service. To use this feature, you must first install the service dependencies:

    pip install cnocr[serve]

    Once installed, start the server using cnocr serve.

    Key options:

    • -p, --port: Server port (default: 8501).
    • -H, --host: Server host (default: 0.0.0.0).
    • --reload: Reload the server automatically when code changes.
  4. Configure GPU support for ONNX models

    master

    By default, the onnxruntime package is installed for CPU usage. To enable GPU acceleration for ONNX models, you must uninstall the CPU version and install onnxruntime-gpu.

    pip uninstall onnxruntime
    pip install onnxruntime-gpu
  5. Install CnOCR via pip

    master

    You can install CnOCR with CPU support using the following command. Note that if you haven't installed PyTorch or OpenCV before, you might encounter common installation issues that can be resolved via standard search engines.

    $ pip install cnocr[ort-cpu]
  6. Install CnOCR

    master

    Install CnOCR using pip. Ensure you are using Python 3.8 or later. Choose the installation variant based on your environment:

    • CPU (ONNX Runtime): pip install cnocr[ort-cpu]
    • GPU (ONNX Runtime): pip install cnocr[ort-gpu]
    • Development (for training models): pip install cnocr[dev]

    If installation is slow, use a mirror like Aliyun:

    pip install cnocr[ort-cpu] -i https://mirrors.aliyun.com/pypi/simple

    Alternatively, pull the pre-installed Docker image:

    docker pull breezedeus/cnocr:latest
    pip install cnocr[ort-cpu]
  7. Set up and run the cnocr HTTP service

    master

    CnOCR provides an HTTP service based on FastAPI.

    1. Install dependencies: You must install the serve extra.
    2. Start the service: Use the cnocr serve command. Use the -p flag to specify the port.
    3. Endpoint: The service provides an /ocr endpoint that accepts an image file.
  8. Select CnOCR recognition models by use case

    master

    CnOCR provides several categories of recognition models optimized for different scenarios. Choose a model based on your input image type:

    • scene- models: Optimized for general photos taken by cameras (e.g., scene-densenet_lite_136-gru).
    • doc- models: Optimized for document screenshots or scans (e.g., doc-densenet_lite_136-gru).
    • number- models: Optimized for recognizing only digits (0-9), ideal for bank cards or ID numbers (e.g., number-densenet_lite_136-fc).
    • general models: Standard models for images without a specific bias (e.g., densenet_lite_136-gru).

    Note: For best results, test the model against your specific data as these categories are guidelines.