CnOCR Documentation
repository·master·Indexed 26 days ago
https://github.com/breezedeus/cnocrA 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.
What's inside CnOCR
- 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.
Fine-tune an existing model
masterTo 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_rateto a small value (e.g.,3e-5). - Configure
lr_schedulerwithcos_warmupand appropriate warmup epochs.
{ "learning_rate": 3e-5, "lr_scheduler": { "name": "cos_warmup", "min_lr_mult_factor": 0.01, "warmup_epochs": 2 } }- Set
Access the CnOCR Online Demo via Domestic Mirror
masterFor users in China, a domestic mirror of the Hugging Face demo is available for faster access.
https://hf.qhduan.com/spaces/breezedeus/CnOCR-DemoStart an HTTP API service with `cnocr serve`
masterCnOCR 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.
Configure GPU support for ONNX models
masterBy default, the
onnxruntimepackage is installed for CPU usage. To enable GPU acceleration for ONNX models, you must uninstall the CPU version and installonnxruntime-gpu.pip uninstall onnxruntime pip install onnxruntime-gpuDownload CnOCR Models
masterModels can be downloaded for free from HuggingFace repositories:
- cnstd-cnocr-models
breezedeus/cnocr-ppocr-*breezedeus/cnstd-ppocr-*
Alternatively, you can download them from Baidu Netdisk using the following credentials:
- URL: Baidu Netdisk Link
- Extraction Code:
nocr
Install CnOCR via pip
masterYou can install CnOCR with CPU support using the following command. Note that if you haven't installed
PyTorchorOpenCVbefore, you might encounter common installation issues that can be resolved via standard search engines.$ pip install cnocr[ort-cpu]Install CnOCR
masterInstall 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/simpleAlternatively, pull the pre-installed Docker image:
docker pull breezedeus/cnocr:latestpip install cnocr[ort-cpu]- CPU (ONNX Runtime):
Set up and run the cnocr HTTP service
masterCnOCR provides an HTTP service based on FastAPI.
- Install dependencies: You must install the
serveextra. - Start the service: Use the
cnocr servecommand. Use the-pflag to specify the port. - Endpoint: The service provides an
/ocrendpoint that accepts an image file.
- Install dependencies: You must install the
Select CnOCR recognition models by use case
masterCnOCR 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).generalmodels: 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.
Install CnOCR via Docker
masterYou can pull a pre-configured Docker image from Docker Hub to use CnOCR immediately.
$ docker pull breezedeus/cnocr:latestManual model installation for CnOCR
masterIf the system cannot automatically download the model zip file, follow these steps:
- Download the model zip file from cnstd-cnocr-models or via Baidu Netdisk (extraction code:
nocr). - Place the downloaded zip file into the
~/.cnocr/2.3directory. - The code will handle the subsequent extraction and setup automatically once the file is in place.
- Download the model zip file from cnstd-cnocr-models or via Baidu Netdisk (extraction code: