Skip to main content

中文版说明请见中文README。

cnocr

A python package for Chinese OCR with available trained models. So it can be used directly after installed.

The accuracy of the current crnn model is about 98.7%.

The project originates from our own (爱因互动 Ein+) internal needs. Thanks for the internal supports.

Changes

Most of the codes are adapted from crnn-mxnet-chinese-text-recognition. Much thanks to the author.

Some changes are:

  • use raw MXNet CTC Loss instead of WarpCTC Loss. No more complicated installation.
  • public pre-trained model for anyone. No more a-few-days training.
  • add online predict function and script. Easy to use.

Installation

pip install cnocr

Please use Python3 (3.4, 3.5, 3.6 should work). Python2 is not tested.

Usage

Predict

from cnocr import CnOcr
ocr = CnOcr()
res = ocr.ocr('examples/multi-line_cn1.png')
print("Predicted Chars:", res)

When you run the previous codes, the model files will be downloaded automatically from Dropbox to ~/.cnocr. The zip file will be extracted and you can find the resulting model files in ~/.cnocr/models by default. In case the automatic download can't perform well, you can download the zip file manually from Baidu NetDisk with extraction code pg26, and put the zip file to ~/.cnocr. The code will do else.

Try the predict command for examples/multi-line_cn1.png:

examples/multi-line_cn1.png

python scripts/cnocr_predict.py --file examples/multi-line_cn1.png

You will get:

Predicted Chars: [['网', '络', '支', '付', '并', '无', '本', '质', '的', '区', '别', ',', '因', '为'], ['每', '一', '个', '手', '机', '号', '码', '和', '邮', '件', '地', '址', '背', '后'], ['都', '会', '对', '应', '着', '一', '个', '账', '户', '一', '一', '这', '个', '账'], ['户', '可', '以', '是', '信', '用', '卡', '账', '户', '、', '借', '记', '卡', '账'], ['户', ',', '也', '包', '括', '邮', '局', '汇', '款', '、', '手', '机', '代'], ['收', '、', '电', '话', '代', '收', '、', '预', '付', '费', '卡', '和', '点', '卡'], ['等', '多', '种', '形', '式', '。']]

Predict for Single-line-characters Image

If you know your image includes only one single line characters, you can use function Cnocr.ocr_for_single_line() instead of Cnocr.ocr(). Cnocr.ocr_for_single_line() should be more efficient.

from cnocr import CnOcr
ocr = CnOcr()
res = ocr.ocr_for_single_line('examples/rand_cn1.png')
print("Predicted Chars:", res)

With file examples/multi-line_cn1.png:

examples/rand_cn1.png

you will get:

Predicted Chars: ['笠', '淡', '嘿', '骅', '谧', '鼎', '皋', '姚', '歼', '蠢', '驼', '耳', '胬', '挝', '涯', '狗', '蒽', '子', '犷']

(No NECESSARY) Train

You can use the package without any train. But if you really really want to train your own models, follow this:

python scripts/cnocr_train.py --cpu 2 --num_proc 4 --loss ctc --dataset cn_ocr

Future Work

  • support multi-line-characters recognition
  • Support space recognition
  • Bugfixes
  • Add Tests
  • Maybe use no symbol to rewrite the model
  • Try other models such as DenseNet, ResNet

Metadata

Release files for cnocr 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cnocr 0.2.0
File Size Uploaded
cnocr-0.2.0.tar.gz 19.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cnocr 0.2.0
File Interpreter ABI Platform
cnocr-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 55.0 kB

Release files / cnocr-0.2.0.tar.gz

Download URL cnocr-0.2.0.tar.gz
Size 19.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7f736b33a29cf7ccfd72e1c27e49fc68bfd9e2129480c08218f4f815a0b20f14
BLAKE2b-256 checksum
How to use checksums
1ad72156d29de187f00ec27551c8d6bd798f2b1c4e001f82a21e0c8fc2b1c489
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.9.2 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.23.3 CPython/3.6.5

Release files / cnocr-0.2.0-py3-none-any.whl

Download URL cnocr-0.2.0-py3-none-any.whl
Size 35.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dbc50f9c3bf5c594a666bd09ebd99f01d3d2d2dbc48011bc611c53fa13d205e4
BLAKE2b-256 checksum
How to use checksums
49b7a9d383d87e892721683042bcb18128eae7435bc5e379f7f13c3d1591c64e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.9.2 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.23.3 CPython/3.6.5

Release history Release notifications | RSS feed

2.3.3

2 release files

2.3.2

2 release files

2.3.1

2 release files

2.3

2 release files

2.2.4

2 release files

2.2.3

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

This release

0.2.0 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page