From 20c4cda78e1730b388cc3da181af47f6a39e20d6 Mon Sep 17 00:00:00 2001 From: breezedeus Date: Sun, 15 May 2022 00:17:42 +0800 Subject: [PATCH] update docs --- cnocr/cn_ocr.py | 9 +++-- docs/command.md | 73 +++++++++++++++++------------------ docs/install.md | 13 +++++-- docs/models.md | 34 ++++++++-------- docs/train.md | 30 +++++++-------- docs/usage.md | 100 ++++++++++++++++++------------------------------ 6 files changed, 123 insertions(+), 136 deletions(-) diff --git a/cnocr/cn_ocr.py b/cnocr/cn_ocr.py index 3a39a02..06321d2 100644 --- a/cnocr/cn_ocr.py +++ b/cnocr/cn_ocr.py @@ -75,13 +75,16 @@ class CnOcr(object): Args: model_name (str): 模型名称。默认为 `densenet_lite_136-fc` cand_alphabet (Optional[Union[Collection, str]]): 待识别字符所在的候选集合。默认为 `None`,表示不限定识别字符范围 - context (str): 'cpu', or 'gpu'。表明预测时是使用CPU还是GPU。默认为 `cpu` + context (str): 'cpu', or 'gpu'。表明预测时是使用CPU还是GPU。默认为 `cpu`。 + 此参数仅在 `model_backend=='pytorch'` 时有效。 model_fp (Optional[str]): 如果不使用系统自带的模型,可以通过此参数直接指定所使用的模型文件('.ckpt' 文件) - model_backend (str): 'pytorch', or 'onnx'。表明预测时是使用是使用 PyTorch 模型,还是使用 ONNX 模型。默认为 `pytorch` + model_backend (str): 'pytorch', or 'onnx'。表明预测时是使用 PyTorch 版本模型,还是使用 ONNX 版本模型。 + 同样的模型,ONNX 版本的预测速度一般是 PyTorch 版本的2倍左右。默认为 'onnx'。 root (Union[str, Path]): 模型文件所在的根目录。 Linux/Mac下默认值为 `~/.cnocr`,表示模型文件所处文件夹类似 `~/.cnocr/2.1/densenet_lite_136-fc`。 Windows下默认值为 `C:/Users//AppData/Roaming/cnocr`。 - vocab_fp (Union[str, Path]): 字符集合的文件路径,即 `label_cn.txt` 文件路径 + vocab_fp (Union[str, Path]): 字符集合的文件路径,即 `label_cn.txt` 文件路径。 + 若训练的自有模型更改了字符集,看通过此参数传入新的字符集文件路径。 **kwargs: 目前未被使用。 Examples: diff --git a/docs/command.md b/docs/command.md index a90b34d..606b076 100644 --- a/docs/command.md +++ b/docs/command.md @@ -2,18 +2,16 @@ **cnocr** 包含了几个命令行工具,安装 **cnocr** 后即可使用。 - - ## 预测单个文件或文件夹中所有图片 使用命令 **`cnocr predict`** 预测单个文件或文件夹中所有图片,以下是使用说明: - - ```bash -(venv) ➜ cnocr git:(dev) ✗ cnocr predict -h +> cnocr predict -h Usage: cnocr predict [OPTIONS] + 模型预测 + Options: -m, --model-name TEXT 模型名称。默认值为 densenet_lite_136-fc -p, --pretrained-model-fp TEXT 使用训练好的模型。默认为 `None`,表示使用系统自带的预训练模型 @@ -27,31 +25,24 @@ Options: -h, --help Show this message and exit. ``` - - 例如可以使用以下命令对图片 `docs/examples/rand_cn1.png` 进行文字识别: ```bash cnstd predict -i docs/examples/rand_cn1.png -s ``` - - 具体使用也可参考文件 [Makefile](https://github.com/breezedeus/cnocr/blob/master/Makefile) 。 - - - ## 模型评估 使用命令 **`cnocr evaluate`** 在指定的数据集上评估模型效果,以下是使用说明: - - ```bash -(venv) ➜ cnocr git:(dev) ✗ cnocr evaluate -h +> cnocr evaluate -h Usage: cnocr evaluate [OPTIONS] + 评估模型效果 + Options: -m, --model-name TEXT 模型名称。默认值为 densenet_lite_136-fc -p, --pretrained-model-fp TEXT 使用训练好的模型。默认为 `None`,表示使用系统自带的预训练模型 @@ -62,36 +53,32 @@ Options: <以空格分割的labels>` --img-folder TEXT 图片所在文件夹,相对于索引文件中记录的图片位置 [required] - --batch-size INTEGER batch size. 默认值:`128` + --batch-size INTEGER batch size. 默认值:128 -o, --output-dir TEXT 存放评估结果的文件夹。默认值:`eval_results` -v, --verbose whether to print details to screen -h, --help Show this message and exit. ``` - - 例如可以使用以下命令评估 `data/test/dev.tsv` 中指定的所有样本: ```bash cnocr evaluate -i data/test/dev.tsv --image-folder data/images ``` - - 具体使用也可参考文件 [Makefile](https://github.com/breezedeus/cnocr/blob/master/Makefile) 。 - - ## 模型训练 使用命令 **`cnocr train`** 训练文本检测模型,以下是使用说明: ```bash -(venv) ➜ cnocr git:(dev) ✗ cnocr train -h +> cnocr train -h Usage: cnocr train [OPTIONS] + 训练模型 + Options: - -m, --model-name TEXT 模型名称。默认值为 densenet_lite_136-fc + -m, --model-name TEXT 模型名称。默认值为 `densenet_lite_136-fc` -i, --index-dir TEXT 索引文件所在的文件夹,会读取文件夹中的 train.tsv 和 dev.tsv 文件 [required] @@ -100,38 +87,30 @@ Options: [required] -r, --resume-from-checkpoint TEXT - 恢复此前中断的训练状态,继续训练。默认为 `None` - -p, --pretrained-model-fp TEXT 导入的训练好的模型,作为初始模型。优先级低于"--restore-training- - fp",当传入"--restore-training-fp"时,此传入失效。默认为 - `None` + 恢复此前中断的训练状态,继续训练。所以文件中应该包含训练状态。默认为 `None` + -p, --pretrained-model-fp TEXT 导入的训练好的模型,作为模型初始值。优先级低于"--resume-from- + checkpoint",当传入"--resume-from- + checkpoint"时,此传入失效。默认为 `None` -h, --help Show this message and exit. ``` - - 例如可以使用以下命令进行训练: ```bash cnocr train -m densenet_lite_136-fc --index-dir data/test --train-config-fp docs/examples/train_config.json ``` - - 训练数据的格式见文件夹 [data/test](https://github.com/breezedeus/cnocr/blob/master/data/test) 中的 [train.tsv](https://github.com/breezedeus/cnocr/blob/master/data/test/train.tsv) 和 [dev.tsv](https://github.com/breezedeus/cnocr/blob/master/data/test/dev.tsv) 文件。 - - 具体使用也可参考文件 [Makefile](https://github.com/breezedeus/cnocr/blob/master/Makefile) 。 - - ## 模型转存 训练好的模型会存储训练状态,使用命令 **`cnocr resave`** 去掉与预测无关的数据,降低模型大小。 ```bash -(venv) ➜ cnocr git:(pytorch) ✗ cnocr resave -h +> cnocr resave -h Usage: cnocr resave [OPTIONS] 训练好的模型会存储训练状态,使用此命令去掉预测时无关的数据,降低模型大小 @@ -144,3 +123,23 @@ Options: +## PyTorch 模型导出为 ONNX 模型 + +把训练好的模型导出为 ONNX 格式。 当前无法导出 `*-gru` 模型, 具体说明见:[Exporting GRU RNN to ONNX - PyTorch Forums](https://discuss.pytorch.org/t/exporting-gru-rnn-to-onnx/27244) 。后续版本会修复此问题。 + +```bash +> cnocr export-onnx -h +Usage: cnocr export-onnx [OPTIONS] + + 把训练好的模型导出为 ONNX 格式。 当前无法导出 `*-gru` 模型, + 具体说明见:https://discuss.pytorch.org/t/exporting-gru-rnn-to-onnx/27244 , + 后续版本会修复此问题。 + +Options: + -m, --model-name TEXT 模型名称。默认值为 `densenet_lite_136-fc` + -i, --input-model-fp TEXT 输入的模型文件路径。 默认为 `None`,表示使用系统自带的预训练模型 + -o, --output-model-fp TEXT 输出的模型文件路径(.onnx) [required] + -h, --help Show this message and exit. +``` + + diff --git a/docs/install.md b/docs/install.md index bde2f85..40f7923 100644 --- a/docs/install.md +++ b/docs/install.md @@ -6,16 +6,23 @@ pip install cnocr ``` - - 安装速度慢的话,可以指定国内的安装源,如使用豆瓣源: ```bash pip install cnocr -i https://pypi.doubanio.com/simple ``` +> 注意:请使用 **Python3**(3.6以及之后版本应该都行),没测过Python2下是否ok。 -> 注意:请使用 **Python3**(3.6以及之后版本应该都行),没测过Python2下是否ok。 + +### GPU 环境使用 ONNX 模型 + +默认情况下安装的 **ONNX** 包是 **`onnxruntime`**,它只能在 `CPU` 上运行。如果需要在 `GPU` 环境使用 **ONNX** 模型,需要卸载此包,然后安装包 **`onnxruntime-gpu`** 。 + +```bash +pip uninstall onnxruntime +pip install onnxruntime-gpu +``` diff --git a/docs/models.md b/docs/models.md index b9118da..ac057f3 100644 --- a/docs/models.md +++ b/docs/models.md @@ -3,24 +3,28 @@ cnocr的ocr模型可以分为两阶段:第一阶段是获得ocr图片的局部编码向量,第二部分是对局部编码向量进行序列学习,获得序列编码向量。目前的PyTorch版本的两个阶段分别包含以下模型: 1. 局部编码模型(emb model) - * **`densenet_lite_`**:一个微型的`densenet`网络;其中的``表示模型中每个block包含的层数。 - * **`densenet`**:一个小型的`densenet`网络; + * **`densenet_lite_`**:一个微型的`densenet`网络;其中的``表示模型中每个block包含的层数。 + * **`densenet`**:一个小型的`densenet`网络; 2. 序列编码模型(seq model) - * **`fc`**:两层的全连接网络; - * **`gru`**:一层的GRU网络; - * **`lstm`**:一层的LSTM网络。 + * **`fc`**:两层的全连接网络; + * **`gru`**:一层的GRU网络; + * **`lstm`**:一层的LSTM网络。 +cnocr **V2.1** 目前包含以下可直接使用的模型,训练好的模型都放在 **[cnstd-cnocr-models](https://github.com/breezedeus/cnstd-cnocr-models)** 项目中,可免费下载使用: + +| Name | PyTorch 版本 | ONNX 版本 | 参数规模 | 模型文件大小 | 准确度 | 平均推断耗时(毫秒/图) | +| ----------------------- | ---------- | ------- | ----- | ------ | ------ | ------------ | +| densenet\_lite\_114-fc | √ | √ | 1.3 M | 4.9 M | 0.9274 | 9.229 | +| densenet\_lite\_124-fc | √ | √ | 1.3 M | 5.1 M | 0.9429 | 10.112 | +| densenet\_lite\_134-fc | √ | √ | 1.4 M | 5.4 M | 0.954 | 10.843 | +| densenet\_lite\_136-fc | √ | √ | 1.5M | 5.9 M | 0.9631 | 11.499 | +| densenet\_lite\_134-gru | √ | X | 2.9 M | 11 M | 0.9738 | 17.042 | +| densenet\_lite\_136-gru | √ | X | 3.1 M | 12 M | 0.9756 | 17.725 | -cnocr **V2.1** 目前包含以下可直接使用的模型,训练好的模型都放在 **[cnstd-cnocr-models](https://github.com/breezedeus/cnstd-cnocr-models)** 项目中,可免费下载使用: -| Name | 参数规模 | 模型文件大小 | 准确度 | 平均推断耗时(毫秒/图) | -| --- | --- | --- | --- | --- | -| densenet\_lite\_114-fc | 1.3 M | 4.9 M | 0.9274 | 9.229 | -| densenet\_lite\_124-fc | 1.3 M | 5.1 M | 0.9429 | 10.112 | -| densenet\_lite\_134-fc | 1.4 M | 5.4 M | 0.954 | 10.843 | -| densenet\_lite\_136-fc | 1.5M | 5.9 M | 0.9631 | 11.499 | -| densenet\_lite\_134-gru | 2.9 M | 11 M | 0.9738 | 17.042 | -| densenet\_lite\_136-gru | 3.1 M | 12 M | 0.9756 | 17.725 | +一些说明: -> 模型名称是由局部编码模型和序列编码模型名称拼接而成,以符合"-"分割。 +1. 模型名称是由**局部编码**模型和**序列编码**模型名称拼接而成,以符合"-"分割。 +2. 列 **`PyTorch 版本`** 为 `√` 表示此模型支持 `model_backend=='pytorch'`;列 **`ONNX 版本`** 为 `√` 表示此模型支持 `model_backend=='onnx'`;取值为 `X` 则表示不支持对应的取值。 +3. `平均耗时` 是针对 `PyTorch 版本` 获得的,**`ONNX 版本` 耗时大致是 `PyTorch 版本` 的一半。** diff --git a/docs/train.md b/docs/train.md index fa33eca..22225cb 100644 --- a/docs/train.md +++ b/docs/train.md @@ -2,17 +2,18 @@ 自带模型基于 `500+万` 的文字图片训练而成。 - ## 训练命令 [命令行工具](command.md) 介绍了训练命令。使用命令 **`cnocr train`** 训练文本检测模型,以下是使用说明: ```bash -(venv) ➜ cnocr git:(dev) ✗ cnocr train -h +> cnocr train -h Usage: cnocr train [OPTIONS] + 训练模型 + Options: - -m, --model-name TEXT 模型名称。默认值为 densenet_lite_136-fc + -m, --model-name TEXT 模型名称。默认值为 `densenet_lite_136-fc` -i, --index-dir TEXT 索引文件所在的文件夹,会读取文件夹中的 train.tsv 和 dev.tsv 文件 [required] @@ -21,31 +22,24 @@ Options: [required] -r, --resume-from-checkpoint TEXT - 恢复此前中断的训练状态,继续训练。默认为 `None` - -p, --pretrained-model-fp TEXT 导入的训练好的模型,作为初始模型。优先级低于"--restore-training- - fp",当传入"--restore-training-fp"时,此传入失效。默认为 - `None` + 恢复此前中断的训练状态,继续训练。所以文件中应该包含训练状态。默认为 `None` + -p, --pretrained-model-fp TEXT 导入的训练好的模型,作为模型初始值。优先级低于"--resume-from- + checkpoint",当传入"--resume-from- + checkpoint"时,此传入失效。默认为 `None` -h, --help Show this message and exit. ``` - - 例如可以使用以下命令进行训练: ```bash -cnocr train -m densenet_lite_136-fc --index-dir data/test --train-config-fp docs/examples/train_config.json +> cnocr train -m densenet_lite_136-fc --index-dir data/test --train-config-fp docs/examples/train_config.json ``` - 训练数据的格式见文件夹 [data/test](https://github.com/breezedeus/cnocr/blob/master/data/test) 中的 [train.tsv](https://github.com/breezedeus/cnocr/blob/master/data/test/train.tsv) 和 [dev.tsv](https://github.com/breezedeus/cnocr/blob/master/data/test/dev.tsv) 文件。 - - 具体使用也可参考文件 [Makefile](https://github.com/breezedeus/cnocr/blob/master/Makefile) 。 - - # 模型精调 如果需要在已有模型的基础上精调模型,需要把训练配置中的学习率设置的较小,`lr_scheduler`的设置可参考以下: @@ -59,6 +53,10 @@ cnocr train -m densenet_lite_136-fc --index-dir data/test --train-config-fp docs }, ``` +> 注:需要尽量避免过度精调! + -> 注:需要尽量避免过度精调! +# 详细训练教程和训练过程作者答疑 + +[**模型训练详细教程**](https://articles.zsxq.com/id_u6b4u0wrf46e.html) 见作者的 **知识星球** [CnOCR/CnSTD私享群](https://t.zsxq.com/FEYZRJQ) ,加入私享群后作者也会尽力解答训练过程中遇到的问题。此外,私享群中作者每月提供两次免费特有数据的训练服务。**抱歉的是,私享群不是免费的。** diff --git a/docs/usage.md b/docs/usage.md index 6387bc8..fb7f821 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -1,6 +1,7 @@ # 使用方法 ## 模型文件自动下载 + 首次使用cnocr时,系统会**自动下载** zip格式的模型压缩文件,并存于 `~/.cnocr`目录(Windows下默认路径为 `C:\Users\\AppData\Roaming\cnocr`)。 下载后的zip文件代码会自动对其解压,然后把解压后的模型相关目录放于`~/.cnocr/2.1`目录中。 @@ -27,6 +28,7 @@ print("Predicted Chars:", res) ``` 或: + ```python from cnocr.utils import read_img from cnocr import CnOcr @@ -67,27 +69,24 @@ res = ocr.ocr_for_single_line(img) print("Predicted Chars:", res) ``` - ## 效果示例 -| 图片 | OCR结果 | -| ------------------------------------------------------------ | ------------------------------------------------------------ | -| ![examples/helloworld.jpg](./examples/helloworld.jpg) | Hello world!你好世界 | -| ![examples/chn-00199989.jpg](./examples/chn-00199989.jpg) | 铑泡胭释邑疫反隽寥缔 | -| ![examples/chn-00199980.jpg](./examples/chn-00199980.jpg) | 拇箬遭才柄腾戮胖惬炫 | -| ![examples/chn-00199984.jpg](./examples/chn-00199984.jpg) | 寿猿嗅髓孢刀谎弓供捣 | -| ![examples/chn-00199985.jpg](./examples/chn-00199985.jpg) | 马靼蘑熨距额猬要藕萼 | -| ![examples/chn-00199981.jpg](./examples/chn-00199981.jpg) | 掉江悟厉励.谌查门蠕坑 | -| ![examples/00199975.jpg](./examples/00199975.jpg) | nd-chips fructed ast | -| ![examples/00199978.jpg](./examples/00199978.jpg) | zouna unpayably Raqu | -| ![examples/00199979.jpg](./examples/00199979.jpg) | ape fissioning Senat | -| ![examples/00199971.jpg](./examples/00199971.jpg) | ling oughtlins near | -| ![examples/multi-line_cn1.png](./examples/multi-line_cn1.png) | 网络支付并无本质的区别,因为
每一个手机号码和邮件地址背后
都会对应着一个账户--这个账
户可以是信用卡账户、借记卡账
户,也包括邮局汇款、手机代
收、电话代收、预付费卡和点卡
等多种形式。 | -| ![examples/multi-line_cn2.png](./examples/multi-line_cn2.png) | 当然,在媒介越来越多的情形下,
意味着传播方式的变化。过去主流
的是大众传播,现在互动性和定制
性带来了新的挑战——如何让品牌
与消费者更加互动。 | +| 图片 | OCR结果 | +| ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| ![examples/helloworld.jpg](./examples/helloworld.jpg) | Hello world!你好世界 | +| ![examples/chn-00199989.jpg](./examples/chn-00199989.jpg) | 铑泡胭释邑疫反隽寥缔 | +| ![examples/chn-00199980.jpg](./examples/chn-00199980.jpg) | 拇箬遭才柄腾戮胖惬炫 | +| ![examples/chn-00199984.jpg](./examples/chn-00199984.jpg) | 寿猿嗅髓孢刀谎弓供捣 | +| ![examples/chn-00199985.jpg](./examples/chn-00199985.jpg) | 马靼蘑熨距额猬要藕萼 | +| ![examples/chn-00199981.jpg](./examples/chn-00199981.jpg) | 掉江悟厉励.谌查门蠕坑 | +| ![examples/00199975.jpg](./examples/00199975.jpg) | nd-chips fructed ast | +| ![examples/00199978.jpg](./examples/00199978.jpg) | zouna unpayably Raqu | +| ![examples/00199979.jpg](./examples/00199979.jpg) | ape fissioning Senat | +| ![examples/00199971.jpg](./examples/00199971.jpg) | ling oughtlins near | +| ![examples/multi-line_cn1.png](./examples/multi-line_cn1.png) | 网络支付并无本质的区别,因为
每一个手机号码和邮件地址背后
都会对应着一个账户--这个账
户可以是信用卡账户、借记卡账
户,也包括邮局汇款、手机代
收、电话代收、预付费卡和点卡
等多种形式。 | +| ![examples/multi-line_cn2.png](./examples/multi-line_cn2.png) | 当然,在媒介越来越多的情形下,
意味着传播方式的变化。过去主流
的是大众传播,现在互动性和定制
性带来了新的挑战——如何让品牌
与消费者更加互动。 | | ![examples/multi-line_en_white.png](./examples/multi-line_en_white.png) | This chapter is currently only available
in this web version. ebook and print will follow.
Convolutional neural networks learn abstract
features and concepts from raw image pixels. Feature
Visualization visualizes the learned features
by activation maximization. Network Dissection labels
neural network units (e.g. channels) with human concepts. | -| ![examples/multi-line_en_black.png](./examples/multi-line_en_black.png) | transforms the image many times. First, the image
goes through many convolutional layers. In those
convolutional layers, the network learns new
and increasingly complex features in its layers. Then the
transformed image information goes through
the fully connected layers and turns into a classification
or prediction. | - - +| ![examples/multi-line_en_black.png](./examples/multi-line_en_black.png) | transforms the image many times. First, the image
goes through many convolutional layers. In those
convolutional layers, the network learns new
and increasingly complex features in its layers. Then the
transformed image information goes through
the fully connected layers and turns into a classification
or prediction. | ## 详细使用说明 @@ -97,14 +96,16 @@ print("Predicted Chars:", res) class CnOcr(object): def __init__( self, - model_name: str = 'densenet_lite_136-fc' + model_name: str = 'densenet_lite_136-fc', *, cand_alphabet: Optional[Union[Collection, str]] = None, context: str = 'cpu', # ['cpu', 'gpu', 'cuda'] model_fp: Optional[str] = None, + model_backend: str = 'onnx', # ['pytorch', 'onnx'] root: Union[str, Path] = data_dir(), + vocab_fp: Union[str, Path] = VOCAB_FP, **kwargs, - ): + ) ``` 其中的几个参数含义如下: @@ -113,46 +114,38 @@ class CnOcr(object): * `cand_alphabet`: 待识别字符所在的候选集合。默认为 `None`,表示不限定识别字符范围。取值可以是字符串,如 `"0123456789"`,或者字符列表,如 `["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]`。 - * `cand_alphabet`也可以初始化后通过类函数 `CnOcr.set_cand_alphabet(cand_alphabet)` 进行设置。这样同一个实例也可以指定不同的`cand_alphabet`进行识别。 - -* `context`:预测使用的机器资源,可取值为字符串`cpu`、`gpu`、`cuda:0`等。 + * `cand_alphabet`也可以初始化后通过类函数 `CnOcr.set_cand_alphabet(cand_alphabet)` 进行设置。这样同一个实例也可以指定不同的`cand_alphabet`进行识别。 + +* `context`:预测使用的机器资源,可取值为字符串`cpu`、`gpu`、`cuda:0`等。默认为 `cpu`。此参数仅在 `model_backend=='pytorch'` 时有效。 * `model_fp`: 如果不使用系统自带的模型,可以通过此参数直接指定所使用的模型文件(`.ckpt` 文件)。 -* `root`: 模型文件所在的根目录。 +* `model_backend`:'pytorch', or 'onnx'。表明预测时是使用 `PyTorch` 版本模型,还是使用 `ONNX` 版本模型。 **同样的模型,ONNX 版本的预测速度一般是 PyTorch 版本的 2倍左右。** 默认为 'onnx'。 - * Linux/Mac下默认值为 `~/.cnocr`,表示模型文件所处文件夹类似 `~/.cnocr/2.1/densenet_lite_136-fc`。 - * Windows下默认值为 `C:\Users\\AppData\Roaming\cnocr`。 +* `root`: 模型文件所在的根目录。 + + * Linux/Mac下默认值为 `~/.cnocr`,表示模型文件所处文件夹类似 `~/.cnocr/2.1/densenet_lite_136-fc`。 + * Windows下默认值为 `C:\Users\\AppData\Roaming\cnocr`。 - +* `vocab_fp`:字符集合的文件路径,即 `label_cn.txt` 文件路径。若训练的自有模型更改了字符集,看通过此参数传入新的字符集文件路径。 每个参数都有默认取值,所以可以不传入任何参数值进行初始化:`ocr = CnOcr()`。 - --- - - 类`CnOcr`主要包含三个函数,下面分别说明。 - - ### 1. 函数`CnOcr.ocr(img_fp)` 函数`CnOcr.ocr(img_fp)`可以对包含多行文字(或单行)的图片进行文字识别。 - - **函数说明**: - 输入参数 `img_fp`: 可以是需要识别的图片文件路径(如下例);或者是已经从图片文件中读入的数组,类型可以为 `torch.Tensor` 或 `np.ndarray`,取值应该是`[0,255]`的整数,维数应该是 `[height, width]` (灰度图片)或者 `[height, width, channel]`,`channel` 可以等于`1`(灰度图片)或者`3`(`RGB`格式的彩色图片)。 - 返回值:为一个嵌套的`list`,其中的每个元素存储了对一行文字的识别结果,其中也包含了识别概率值。类似这样`[(['第', '一', '行'], 0.80), (['第', '二', '行'], 0.75), (['第', '三', '行'], 0.9)]`,其中的数字为对应的识别概率值。 - - **调用示例**: - ```python from cnocr import CnOcr @@ -162,6 +155,7 @@ print("Predicted Chars:", res) ``` 或: + ```python from cnocr.utils import read_img from cnocr import CnOcr @@ -173,29 +167,23 @@ res = ocr.ocr(img) print("Predicted Chars:", res) ``` - - 上面使用的图片文件 [docs/examples/multi-line_cn1.png](./examples/multi-line_cn1.png)内容如下: ![examples/multi-line_cn1.png](./examples/multi-line_cn1.png) - - 上面预测代码段的返回结果如下: ```bash Predicted Chars: [ - (['网', '络', '支', '付', '并', '无', '本', '质', '的', '区', '别', ',', '因', '为'], 0.8677546381950378), - (['每', '一', '个', '手', '机', '号', '码', '和', '邮', '件', '地', '址', '背', '后'], 0.6706454157829285), - (['都', '会', '对', '应', '着', '一', '个', '账', '户', '一', '一', '这', '个', '账'], 0.5052655935287476), - (['户', '可', '以', '是', '信', '用', '卡', '账', '户', '、', '借', '记', '卡', '账'], 0.7785991430282593), - (['户', ',', '也', '包', '括', '邮', '局', '汇', '款', '、', '手', '机', '代'], 0.37458470463752747), - (['收', '、', '电', '话', '代', '收', '、', '预', '付', '费', '卡', '和', '点', '卡'], 0.7326119542121887), - (['等', '多', '种', '形', '式', '。'], 0.14462216198444366)] + (['网', '络', '支', '付', '并', '无', '本', '质', '的', '区', '别', ',', '因', '为'], 0.8677546381950378), + (['每', '一', '个', '手', '机', '号', '码', '和', '邮', '件', '地', '址', '背', '后'], 0.6706454157829285), + (['都', '会', '对', '应', '着', '一', '个', '账', '户', '一', '一', '这', '个', '账'], 0.5052655935287476), + (['户', '可', '以', '是', '信', '用', '卡', '账', '户', '、', '借', '记', '卡', '账'], 0.7785991430282593), + (['户', ',', '也', '包', '括', '邮', '局', '汇', '款', '、', '手', '机', '代'], 0.37458470463752747), + (['收', '、', '电', '话', '代', '收', '、', '预', '付', '费', '卡', '和', '点', '卡'], 0.7326119542121887), + (['等', '多', '种', '形', '式', '。'], 0.14462216198444366)] ``` - - ### 2. 函数`CnOcr.ocr_for_single_line(img_fp)` 如果明确知道要预测的图片中只包含了单行文字,可以使用函数`CnOcr.ocr_for_single_line(img_fp)`进行识别。和 `CnOcr.ocr()`相比,`CnOcr.ocr_for_single_line()`结果可靠性更强,因为它不需要做额外的分行处理。 @@ -205,8 +193,6 @@ Predicted Chars: [ - 输入参数 `img_fp`: 可以是需要识别的图片文件路径(如下例);或者是已经从图片文件中读入的数组,类型可以为 `torch.Tensor` 或 `np.ndarray`,取值应该是`[0,255]`的整数,维数应该是 `[height, width]` (灰度图片)或者 `[height, width, channel]`,`channel` 可以等于`1`(灰度图片)或者`3`(`RGB`格式的彩色图片)。 - 返回值:为一个`tuple`,其中存储了对一行文字的识别结果,也包含了识别概率值。类似这样`(['第', '一', '行'], 0.80)`,其中的数字为对应的识别概率值。 - - **调用示例**: ```python @@ -230,7 +216,6 @@ res = ocr.ocr_for_single_line(img) print("Predicted Chars:", res) ``` - 对图片文件 [docs/examples/rand_cn1.png](./examples/rand_cn1.png): ![examples/rand_cn1.png](./examples/rand_cn1.png) @@ -241,22 +226,16 @@ print("Predicted Chars:", res) Predicted Chars: (['笠', '淡', '嘿', '骅', '谧', '鼎', '皋', '姚', '歼', '蠢', '驼', '耳', '胬', '挝', '涯', '狗', '蒽', '了', '狞'], 0.7832438349723816) ``` - - ### 3. 函数`CnOcr.ocr_for_single_lines(img_list, batch_size=1)` 函数`CnOcr.ocr_for_single_lines(img_list)`可以**对多个单行文字图片进行批量预测**。函数`CnOcr.ocr(img_fp)`和`CnOcr.ocr_for_single_line(img_fp)`内部其实都是调用的函数`CnOcr.ocr_for_single_lines(img_list)`。 - - **函数说明**: - 输入参数` img_list`: 为一个`list`;其中每个元素可以是需要识别的图片文件路径(如下例);或者是已经从图片文件中读入的数组,类型可以为 `torch.Tensor` 或 `np.ndarray`,取值应该是`[0,255]`的整数,维数应该是 `[height, width]` (灰度图片)或者 `[height, width, channel]`,`channel` 可以等于`1`(灰度图片)或者`3`(`RGB`格式的彩色图片)。 - 输入参数 `batch_size`: 待处理图片很多时,需要分批处理,每批图片的数量由此参数指定。默认为 `1`。 - 返回值:为一个嵌套的`list`,其中的每个元素存储了对一行文字的识别结果,其中也包含了识别概率值。类似这样`[(['第', '一', '行'], 0.80), (['第', '二', '行'], 0.75), (['第', '三', '行'], 0.9)]`,其中的数字为对应的识别概率值。 - - **调用示例**: ```python @@ -274,7 +253,4 @@ res = ocr.ocr_for_single_lines(line_img_list) print("Predicted Chars:", res) ``` - - 更详细的使用方法,可参考 [tests/test_cnocr.py](https://github.com/breezedeus/cnocr/blob/master/tests/test_cnocr.py) 中提供的测试用例。 - -- GitLab