{"id":16568407,"url":"https://github.com/breezedeus/cnstd","last_synced_at":"2025-10-04T01:44:16.328Z","repository":{"id":39167279,"uuid":"263859918","full_name":"breezedeus/CnSTD","owner":"breezedeus","description":"CnSTD: 基于 PyTorch/MXNet 的 中文/英文 场景文字检测（Scene Text Detection）、数学公式检测（Mathematical Formula Detection, MFD）、篇章分析（Layout Analysis）的Python3 包","archived":false,"fork":false,"pushed_at":"2025-06-27T00:02:53.000Z","size":25993,"stargazers_count":761,"open_issues_count":17,"forks_count":112,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-10-04T01:44:13.586Z","etag":null,"topics":["deep-learning","math-formula-detection","object-detection","ocr","python","pytorch","scene-text-detection","text-detection"],"latest_commit_sha":null,"homepage":"https://www.breezedeus.com/article/cnocr","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/breezedeus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2020-05-14T08:36:30.000Z","updated_at":"2025-10-03T12:43:56.000Z","dependencies_parsed_at":"2024-10-30T14:01:58.024Z","dependency_job_id":"4f06c289-f88e-4e56-8a98-434321e67853","html_url":"https://github.com/breezedeus/CnSTD","commit_stats":{"total_commits":272,"total_committers":2,"mean_commits":136.0,"dds":0.003676470588235281,"last_synced_commit":"47eae63e2ec7a812fb23f366f40a13813a7de200"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"purl":"pkg:github/breezedeus/CnSTD","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/breezedeus%2FCnSTD","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/breezedeus%2FCnSTD/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/breezedeus%2FCnSTD/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/breezedeus%2FCnSTD/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/breezedeus","download_url":"https://codeload.github.com/breezedeus/CnSTD/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/breezedeus%2FCnSTD/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278254470,"owners_count":25956598,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-03T02:00:06.070Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","math-formula-detection","object-detection","ocr","python","pytorch","scene-text-detection","text-detection"],"created_at":"2024-10-11T21:09:24.624Z","updated_at":"2025-10-04T01:44:16.321Z","avatar_url":"https://github.com/breezedeus.png","language":"Python","readme":"\u003cdiv align=\"center\"\u003e\n\t\u003cimg src=\"./docs/logo.png\" width=\"250px\"/\u003e\n  \u003cdiv\u003e\u0026nbsp;\u003c/div\u003e\n\n[![Downloads](https://static.pepy.tech/personalized-badge/cnstd?period=total\u0026units=international_system\u0026left_color=grey\u0026right_color=orange\u0026left_text=Downloads)](https://pepy.tech/project/cnstd)\n[![Visitors](https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fgithub.com%2Fbreezedeus%2FCnSTD\u0026label=Visitors\u0026countColor=%23f5c791\u0026style=flat\u0026labelStyle=none)](https://visitorbadge.io/status?path=https%3A%2F%2Fgithub.com%2Fbreezedeus%2FCnSTD)\n[![license](https://img.shields.io/github/license/breezedeus/cnstd)](./LICENSE)\n[![PyPI version](https://badge.fury.io/py/cnstd.svg)](https://badge.fury.io/py/cnstd)\n[![forks](https://img.shields.io/github/forks/breezedeus/cnstd)](https://img.shields.io/github/forks/breezedeus/cnstd)\n[![stars](https://img.shields.io/github/stars/breezedeus/cnstd)](https://github.com/breezedeus/cnocr)\n![last-releast](https://img.shields.io/github/release-date/breezedeus/cnstd?style=plastic)\n![last-commit](https://img.shields.io/github/last-commit/breezedeus/cnstd)\n[![Twitter](https://img.shields.io/twitter/url?url=https%3A%2F%2Ftwitter.com%2Fbreezedeus)](https://twitter.com/breezedeus)\n\n\u003c/div\u003e\n\n\u003cdiv align=\"center\"\u003e\n\n[English](./README_en.md) | 中文\n\n\u003c/div\u003e\n\n\n# CnSTD\n## Update 2025.06.25：发布 V1.2.6\n\n主要变更：\n\n* 基于 RapidOCR 集成 PPOCRv5 最新版文本检测功能，提供更快的推理速度\n  * 新增支持 PP-OCRv5 检测模型：`ch_PP-OCRv5_det` 和 `ch_PP-OCRv5_det_server`\n* 修复部分已知 bug\n\n\n## Update 2024.11.24：发布 V1.2.5\n\n主要变更：\n\n* 基于 RapidOCR 集成 PPOCRv4 最新版文本检测功能，提供更快的推理速度\n  * 新增支持 PP-OCRv4 检测模型，包括标准版和服务器版\n* 优化模型下载功能，支持从国内镜像下载模型文件\n\n## Update 2024.06.16：发布 V1.2.4\n\n主要变更：\n\n* 支持基于 Ultralytics 的 YOLO Detector。\n\n\n## Update 2023.06.30：发布 V1.2.3\n\n主要变更：\n\n* 基于新标注的数据，重新训练了 **MFD YoloV7** 模型，目前新模型已部署到 [P2T网页版](https://p2t.behye.com) 。具体说明见：[Pix2Text (P2T) 新版公式检测模型 | Breezedeus.com](https://www.breezedeus.com/article/p2t-mfd-20230613) 。\n* 之前的 MFD YoloV7 模型已开放给星球会员下载，具体说明见：[P2T YoloV7 数学公式检测模型开放给星球会员下载 | Breezedeus.com](https://www.breezedeus.com/article/p2t-yolov7-for-zsxq-20230619) 。\n* 增加了一些Label Studio相关的脚本，见 [scripts](scripts) 。如：利用 CnSTD 自带的 MFD 模型对目录中的图片进行公式检测后生成可导入到Label Studio中的JSON文件；以及，Label Studio标注后把导出的JSON文件转换成训练 MFD 模型所需的数据格式。注意，MFD 模型的训练代码在 [yolov7](https://github.com/breezedeus/yolov7) （`dev` branch）中。\n\n了解更多：[RELEASE.md](./RELEASE.md) 。\n\n---\n\n\n\n**CnSTD** 是 **Python 3** 下的**场景文字检测**（**Scene Text Detection**，简称**STD**）工具包，支持**中文**、**英文**等语言的文字检测，自带了多个训练好的检测模型，安装后即可直接使用。**CnSTD** 自 **V1.2.1** 版本开始，加入了**数学公式检测**（**Mathematical Formula Detection**，简称**MFD**）模型，并提供训练好的模型可直接用于检测图片中包含的数学公式（**行内公式** `embedding` 与**独立行公式** `isolated` ）。\n\n欢迎扫码加入微信交流群：\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://huggingface.co/datasets/breezedeus/cnocr-wx-qr-code/resolve/main/wx-qr-code.JPG\" alt=\"微信群二维码\" width=\"300px\"/\u003e\n\u003c/div\u003e\n\n作者也维护 **知识星球** [**CnOCR/CnSTD/P2T私享群**](https://t.zsxq.com/FEYZRJQ)，欢迎加入。**知识星球私享群**会陆续发布一些CnOCR/CnSTD/P2T相关的私有资料，包括**更详细的训练教程**，**未公开的模型**，使用过程中遇到的难题解答等。本群也会发布OCR/STD相关的最新研究资料。\n\n自 **V1.0.0** 版本开始，**CnSTD** 从之前基于 MXNet 实现转为基于 **PyTorch** 实现。新模型的训练合并了  **ICPR MTWI 2018**、**ICDAR RCTW-17** 和 **ICDAR2019-LSVT** 三个数据集，包括了 **`46447`** 个训练样本，和 **`1534`** 个测试样本。\n\n相较于之前版本， 新版本的变化主要包括：\n\n* 加入了对 [**PaddleOCR**](https://github.com/PaddlePaddle/PaddleOCR) 检测模型的支持；\n* 部分调整了检测结果中 `box` 的表达方式，统一为 `4` 个点的坐标值；  \n* 修复了已知bugs。\n\n如需要识别文本框中的文字，可以结合 **OCR** 工具包 **[cnocr](https://github.com/breezedeus/cnocr)** 一起使用。\n\n\n## 示例\n\n### 场景文字检测（STD）\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"./docs/cases.png\" alt=\"STD效果\" width=\"700px\"/\u003e\n\u003c/div\u003e\n\n### 数学公式检测（MFD）\n\nMFD 模型检测图片中包含的数学公式，其中行内的公式检测为 `embedding` 类别，独立行的公式检测为 `isolated`。模型训练使用了英文 [IBEM](https://zenodo.org/record/4757865) 和中文 [CnMFD_Dataset](https://github.com/breezedeus/CnMFD_Dataset) 两个数据集。\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"./examples/mfd/out-zh4.jpg\" alt=\"中文MFD效果\" width=\"700px\"/\u003e\n\u003c/div\u003e  \n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"./examples/mfd/out-zh5.jpg\" alt=\"中文MFD效果\" width=\"700px\"/\u003e\n\u003c/div\u003e\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"./examples/mfd/out-en2.jpg\" alt=\"英文MFD效果\" width=\"700px\"/\u003e\n\u003c/div\u003e \n\n\n### 版面分析（Layout Analysis）\n\n版面分析模型识别图片中的不同排版元素。模型训练使用的是 [CDLA](https://github.com/buptlihang/CDLA) 数据集。可识别以下10中版面元素：\n\n|正文|标题|图片|图片标题|表格|表格标题|页眉|页脚|注释|公式|\n|---|---|---|---|---|---|---|---|---|---|\n|Text|Title|Figure|Figure caption|Table|Table caption|Header|Footer|Reference|Equation|\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"./examples/layout/out-zh.jpg\" alt=\"版面分析效果\" width=\"700px\"/\u003e\n\u003c/div\u003e  \n\n\n## 安装\n\n嗯，顺利的话很简单（bless）。\n\n```bash\npip install cnstd\n```\n\n如果需要使用 ONNX 模型（`model_backend=onnx`），请使用以下命令安装：\n\n* CPU环境使用 ONNX 模型：\n  ```bash\n  pip install cnstd[ort-cpu]\n  ```\n* GPU环境使用 ONNX 模型：\n  ```bash\n  pip install cnstd[ort-gpu]\n  ```\n  * 注意：如果当前环境已经安装了 `onnxruntime` 包，请先手动卸载（`pip uninstall onnxruntime`）后再运行上面的命令。\n\n安装速度慢的话，可以指定国内的安装源，如使用豆瓣源：\n\n```bash\npip install cnstd -i https://mirrors.aliyun.com/pypi/simple\n```\n\n【注意】：\n\n* 请使用 **Python3** (3.6以及之后版本应该都行)，没测过Python2下是否ok。\n* 依赖 **opencv**，所以可能需要额外安装opencv。\n\n## 已有STD模型\n\nCnSTD 从 **V1.2** 开始，可直接使用的模型包含两类：1）CnSTD 自己训练的模型，通常会包含 PyTorch 和 ONNX 版本；2）从其他ocr引擎搬运过来的训练好的外部模型，ONNX化后用于 CnSTD 中。\n\n直接使用的模型都放在 [**cnstd-cnocr-models**](https://huggingface.co/breezedeus/cnstd-cnocr-models) 项目中，可免费下载使用。\n\n### 1. CnSTD 自己训练的模型\n\n当前版本（Since **V1.1.0**）的文字检测模型使用的是 [**DBNet**](https://github.com/MhLiao/DB)，相较于 V0.1 使用的 [PSENet](https://github.com/whai362/PSENet) 模型， DBNet 的检测耗时几乎下降了一个量级，同时检测精度也得到了极大的提升。\n\n目前包含以下已训练好的模型：\n\n| 模型名称                       | 参数规模      | 模型文件大小    | 测试集精度（IoU） | 平均推断耗时\u003cbr /\u003e（秒/张） | 下载方式                                                      |\n| -------------------------- | --------- | --------- | ---------- | ----------------- | --------------------------------------------------------- |\n| db_resnet34                | 22.5 M    | 86 M      | **0.7322** | 3.11              | 自动                                                        |\n| db_resnet18                | 12.3 M    | 47 M      | 0.7294     | 1.93              | 自动                                                        |\n| db_mobilenet_v3            | 4.2 M     | 16 M      | **0.7269** | 1.76              | 自动                                                        |\n| db_mobilenet_v3_small      | 2.0 M     | 7.9 M     | 0.7054     | 1.24              | 自动                                                        |\n| db_shufflenet_v2           | 4.7 M     | 18 M      | 0.7238     | 1.73              | 自动                                                        |\n| **db_shufflenet_v2_small** | 3.0 M     | 12 M      | 0.7190     | 1.29              | 自动                                                        |\n\n\u003e 上表耗时基于本地 Mac 获得，绝对值无太大参考价值，相对值可供参考。IoU的计算方式经过调整，仅相对值可供参考。\n\n相对于两个基于 **ResNet** 的模型，基于 **MobileNet** 和 **ShuffleNet** 的模型体积更小，速度更快，建议在轻量级场景使用。\n\n### 2. 外部模型\n\n以下模型是 [**PaddleOCR**](https://github.com/PaddlePaddle/PaddleOCR) 中模型的 **ONNX** 版本，所以不会依赖 **PaddlePaddle** 相关工具包，故而也不支持基于这些模型在自己的领域数据上继续精调模型。这些模型支持检测**竖排文字**。\n\n| `model_name`    | PyTorch 版本 | ONNX 版本 | 支持检测的语言    | 模型文件大小 |\n| --------------- | ---------- | ------- | ---------- | ------ |\n| ch_PP-OCRv5_det | X          | √       | 简体中问、英文、数字 | 4.6 M  |\n| ch_PP-OCRv5_det_server | X          | √       | 简体中问、英文、数字 | 84 M  |\n| ch_PP-OCRv4_det | X          | √       | 简体中问、英文、数字 | 4.5 M  |\n| ch_PP-OCRv4_det_server | X          | √       | 简体中问、英文、数字 | 108 M  |\n| ch_PP-OCRv3_det | X          | √       | 简体中问、英文、数字 | 2.3 M  |\n| en_PP-OCRv3_det | X          | √       | **英文**、数字  | 2.3 M  |\n| ch_PP-OCRv2_det | X          | √       | 简体中问、英文、数字 | 2.2 M  |\n\n\n更多模型可参考 [PaddleOCR/models_list.md](https://github.com/PaddlePaddle/PaddleOCR/blob/release%2F2.5/doc/doc_ch/models_list.md) 。如有其他外语（如日、韩等）检测需求，可在 **知识星球** [**CnOCR/CnSTD私享群**](https://t.zsxq.com/FEYZRJQ) 中向作者提出建议。\n\n## 使用方法\n\n首次使用 **CnSTD** 时，系统会自动下载zip格式的模型压缩文件，并存放于 `~/.cnstd`目录（Windows下默认路径为 `C:\\Users\\\u003cusername\u003e\\AppData\\Roaming\\cnstd`）。下载速度超快。下载后的zip文件代码会自动对其解压，然后把解压后的模型相关目录放于`~/.cnstd/1.2`目录中。\n\n如果系统无法自动成功下载zip文件，则需要手动从 [百度云盘](https://pan.baidu.com/s/1zDMzArCDrrXHWL0AWxwYQQ?pwd=nstd)（提取码为 `nstd`）下载对应的zip文件并把它存放于 `~/.cnstd/1.2`（Windows下为 `C:\\Users\\\u003cusername\u003e\\AppData\\Roaming\\cnstd\\1.2`）目录中。模型也可从 **[cnstd-cnocr-models](https://huggingface.co/breezedeus/cnstd-cnocr-models)** 中下载。放置好zip文件后，后面的事代码就会自动执行了。\n\n### 场景文字检测（STD）\n\n使用类 `CnStd` 进行场景文字的检测。类 `CnStd` 的初始化函数如下：\n\n```python\nclass CnStd(object):\n    \"\"\"\n    场景文字检测器（Scene Text Detection）。虽然名字中有个\"Cn\"（Chinese），但其实也可以轻松识别英文的。\n    \"\"\"\n\n    def __init__(\n        self,\n        model_name: str = 'ch_PP-OCRv5_det',\n        *,\n        auto_rotate_whole_image: bool = False,\n        rotated_bbox: bool = True,\n        context: str = 'cpu',\n        model_fp: Optional[str] = None,\n        model_backend: str = 'onnx',  # ['pytorch', 'onnx']\n        root: Union[str, Path] = data_dir(),\n        use_angle_clf: bool = False,\n        angle_clf_configs: Optional[dict] = None,\n        **kwargs,\n    ):\n```\n\n其中的几个参数含义如下：\n\n* `model_name`:  模型名称，即前面模型表格第一列中的值。默认为 **ch_PP-OCRv5_det** 。\n\n* `auto_rotate_whole_image`:  是否自动对整张图片进行旋转调整。默认为`False`。\n\n* `rotated_bbox`:  是否支持检测带角度的文本框；默认为 `True`，表示支持；取值为 `False` 时，只检测水平或垂直的文本。\n\n* `context`：预测使用的机器资源，可取值为字符串`cpu`、`gpu`、`cuda:0`。\n\n* `model_fp`:  如果不使用系统自带的模型，可以通过此参数直接指定所使用的模型文件（`.ckpt`文件）。\n\n* `model_backend` (str): 'pytorch', or 'onnx'。表明预测时是使用 PyTorch 版本模型，还是使用 ONNX 版本模型。  同样的模型，ONNX 版本的预测速度一般是 PyTorch 版本的2倍左右。默认为 `onnx`。\n\n* `root`: 模型文件所在的根目录。\n  \n  * Linux/Mac下默认值为 `~/.cnstd`，表示模型文件所处文件夹类似 `~/.cnstd/1.2/db_shufflenet_v2_small`。\n  * Windows下默认值为 `C:\\Users\\\u003cusername\u003e\\AppData\\Roaming\\cnstd`。\n\n* `use_angle_clf` (bool): 对于检测出的文本框，是否使用角度分类模型进行调整（检测出的文本框可能会存在倒转180度的情况）。默认为 `False`\n\n* `angle_clf_configs` (dict): 角度分类模型对应的参数取值，主要包含以下值：\n  \n  - `model_name`: 模型名称。默认为 'ch_ppocr_mobile_v2.0_cls'\n  - `model_fp`: 如果不使用系统自带的模型，可以通过此参数直接指定所使用的模型文件（'.onnx' 文件）。默认为 `None`。具体可参考类 `AngleClassifier` 的说明\n\n每个参数都有默认取值，所以可以不传入任何参数值进行初始化：`std = CnStd()`。\n\n文本检测使用类`CnOcr`的函数 **`detect()`**，以下是详细说明：\n\n#### 类函数`CnStd.detect()`\n\n```python\n    def detect(\n        self,\n        img_list: Union[\n            str,\n            Path,\n            Image.Image,\n            np.ndarray,\n            List[Union[str, Path, Image.Image, np.ndarray]],\n        ],\n        resized_shape: Union[int, Tuple[int, int]] = (768, 768),\n        preserve_aspect_ratio: bool = True,\n        min_box_size: int = 8,\n        box_score_thresh: float = 0.3,\n        batch_size: int = 20,\n        **kwargs,\n    ) -\u003e Union[Dict[str, Any], List[Dict[str, Any]]]:\n```\n\n**函数说明**：\n\n函数输入参数包括：\n\n- `img_list`: 支持对单个图片或者多个图片（列表）的检测。每个值可以是图片路径，或者已经读取进来 `PIL.Image.Image` 或 `np.ndarray`,  格式应该是 `RGB` 3 通道，shape: `(height, width, 3)`, 取值范围：`[0, 255]`。\n\n- `resized_shape`: `int` or `tuple`, `tuple` 含义为 `(height, width)`, `int` 则表示高宽都为此值；  \n   检测前，先把原始图片resize到接近此大小（只是接近，未必相等）。默认为 `(768, 768)`。\n  \n  \u003e **Note** **（注意）**\n  \u003e 这个取值对检测结果的影响较大，可以针对自己的应用多尝试几组值，再选出最优值。例如 `(512, 768)`, `(768, 768)`, `(768, 1024)`等。\n\n- `preserve_aspect_ratio`: 对原始图片 resize 时是否保持高宽比不变。默认为 `True`。\n\n- `min_box_size`: 过滤掉高度或者宽度小于此值的文本框。默认为 `8`，也即高或者宽小于 `8` 的文本框会被过滤去掉。\n\n- `box_score_thresh`: 过滤掉得分低于此值的文本框。默认为 `0.3`。\n\n- `batch_size`: 待处理图片很多时，需要分批处理，每批图片的数量由此参数指定。默认为 `20`。\n\n- `kwargs`: 保留参数，目前未被使用。\n\n函数输出类型为`list`，其中每个元素是一个字典，对应一张图片的检测结果。字典中包含以下 `keys`：\n\n- `rotated_angle`: `float`, 整张图片旋转的角度。只有 `auto_rotate_whole_image==True` 才可能非 `0`。\n\n- `detected_texts`: `list`, 每个元素存储了检测出的一个框的信息，使用词典记录，包括以下几个值：\n  \n  - `box`：检测出的文字对应的矩形框；`np.ndarray`, shape: `(4, 2)`，对应 box 4个点的坐标值 `(x, y)`;\n  \n  - `score`：得分；`float` 类型；分数越高表示越可靠；\n  \n  - `cropped_img`：对应 \"box\" 中的图片patch（`RGB`格式），会把倾斜的图片旋转为水平。`np.ndarray`类型，`shape: (height, width, 3)`,  取值范围：`[0, 255]`；\n  \n  - 示例:\n    \n    ```python\n      [{'box': array([[416,  77],\n                      [486,  13],\n                      [800, 325],\n                      [730, 390]], dtype=int32),\n        'score': 1.0, \n        'cropped_img': array([[[25, 20, 24],\n                               [26, 21, 25],\n                               [25, 20, 24],\n                              ...,\n                               [11, 11, 13],\n                               [11, 11, 13],\n                               [11, 11, 13]]], dtype=uint8)},\n       ...\n      ]\n    ```\n\n#### 调用示例\n\n```python\nfrom cnstd import CnStd\nstd = CnStd()\nbox_info_list = std.detect('examples/taobao.jpg')\n```\n\n或：\n\n```python\nfrom PIL import Image\nfrom cnstd import CnStd\n\nstd = CnStd()\nimg_fp = 'examples/taobao.jpg'\nimg = Image.open(img_fp)\nbox_infos = std.detect(img)\n```\n\n### 识别检测框中的文字（OCR）\n\n上面示例识别结果中\"cropped_img\"对应的值可以直接交由 **[cnocr](https://github.com/breezedeus/cnocr)** 中的 **`CnOcr`** 进行文字识别。如上例可以结合  **`CnOcr`** 进行文字识别：\n\n```python\nfrom cnstd import CnStd\nfrom cnocr import CnOcr\n\nstd = CnStd()\ncn_ocr = CnOcr()\n\nbox_infos = std.detect('examples/taobao.jpg')\n\nfor box_info in box_infos['detected_texts']:\n    cropped_img = box_info['cropped_img']\n    ocr_res = cn_ocr.ocr_for_single_line(cropped_img)\n    print('ocr result: %s' % str(ocr_res))\n```\n\n注：运行上面示例需要先安装  **[cnocr](https://github.com/breezedeus/cnocr)** ：\n\n```bash\npip install cnocr\n```\n\n\n\n### 数学公式检测（MFD）与 版面分析（Layout Analysis）\n\n数学公式检测（MFD）与 版面分析（Layout Analysis）都是检测图片中感兴趣的元素，它们使用的都是基于YOLOv7的检测架构，在CnSTD都来源于相同的类 `LayoutAnalyzer`，差别只是训练模型使用的数据不同。\n\n\u003e 这两个模型的训练代码在 [yolov7](https://github.com/breezedeus/yolov7) 中（Forked from [WongKinYiu/yolov7](https://github.com/WongKinYiu/yolov7)，感谢原作者。）\n\n\n\n类 `LayoutAnalyzer` 的初始化函数如下：\n\n```python\nclass LayoutAnalyzer(object):\n    def __init__(\n        self,\n        model_name: str = 'mfd',  # 'layout' or 'mfd'\n        *,\n        model_type: str = 'yolov7_tiny',  # 当前支持 [`yolov7_tiny`, `yolov7`]'\n        model_backend: str = 'pytorch',\n        model_categories: Optional[List[str]] = None,\n        model_fp: Optional[str] = None,\n        model_arch_yaml: Optional[str] = None,\n        root: Union[str, Path] = data_dir(),\n        device: str = 'cpu',\n        **kwargs,\n    )\n```\n\n其中的参数含义如下：\n\n- `model_name`: 字符串类型，表示模型类型。可选值：'mfd' 表示数学公式检测；'layout' 表示版面分析。默认值：'mfd'\n\n- `model_type`: 字符串类型，表示模型类型。当前支持 'yolov7_tiny' 和 'yolov7'；默认值：'yolov7_tiny'。'yolov7' 模型暂不开源，当前仅开放给星球会员下载，具体说明见：[P2T YoloV7 数学公式检测模型开放给星球会员下载 | Breezedeus.com](https://www.breezedeus.com/article/p2t-yolov7-for-zsxq-20230619) 。\n\n- `model_backend`: 字符串类型，表示backend。当前仅支持: 'pytorch'；默认值：'pytorch'\n\n- `model_categories`: 模型的检测类别名称。默认值：None，表示基于 `model_name` 自动决定\n\n- `model_fp`: 字符串类型，表示模型文件的路径。默认值：`None`，表示使用默认的文件路径\n\n- `model_arch_yaml`: 架构文件路径，例如 'yolov7-mfd.yaml'；默认值为 None，表示将自动选择。\n\n- `root`: 字符串或`Path`类型，表示模型文件所在的根目录。\n  - Linux/Mac下默认值为 `~/.cnstd`，表示模型文件所处文件夹类似 `~/.cnstd/1.2/analysis`\n  - Windows下默认值为 `C:/Users/\u003cusername\u003e/AppData/Roaming/cnstd`。\n  \n- `device`: 字符串类型，表示运行模型的设备，可选值：'cpu' 或 'gpu'；默认值：'cpu'\n\n- `**kwargs`: 额外的参数。\n\n\n\n\n#### 类函数`LayoutAnalyzer.analyze()`\n\n对指定图片（列表）进行版面分析。\n\n```python\ndef analyze(\n    self,\n    img_list: Union[\n        str,\n        Path,\n        Image.Image,\n        np.ndarray,\n        List[Union[str, Path, Image.Image, np.ndarray]],\n    ],\n    resized_shape: Union[int, Tuple[int, int]] = 700,\n    box_margin: int = 2,\n    conf_threshold: float = 0.25,\n    iou_threshold: float = 0.45,\n) -\u003e Union[List[Dict[str, Any]], List[List[Dict[str, Any]]]]:\n```\n\n\n\n**函数说明**：\n\n函数输入参数包括：\n\n* `img_list` (str or list): 待识别图片或图片列表；如果是 `np.ndarray`，则应该是shape为 `[H, W, 3]` 的 RGB 格式数组\n* `resized_shape` (int or tuple): (H, W); 把图片resize到此大小再做分析；默认值为 `700`\n* `box_margin` (int): 对识别出的内容框往外扩展的像素大小；默认值为 `2`\n* `conf_threshold` (float): 分数阈值；默认值为 `0.25`\n* `iou_threshold` (float): IOU阈值；默认值为 `0.45`\n* `**kwargs`: 额外的参数。\n\n函数输出结果为一个`list`（如果 `img_list` 为 `list`，返回为两层嵌套的 `list`，其中每个元素为对应图片的检测结果），其中每个元素表示识别出的版面中的一个元素，包含以下信息：\n\n* type: 版面元素对应的类型；可选值来自：`self.categories` ;\n* box: 版面元素对应的矩形框；`np.ndarray`, shape: (4, 2)，对应 box 4个点的坐标值 `(x, y)` ;\n* score: 得分，越高表示越可信 。\n\n\n\n\n#### 调用示例\n\n```python\nfrom cnstd import LayoutAnalyzer\nimg_fp = 'examples/mfd/zh5.jpg'\nanalyzer = LayoutAnalyzer('mfd')\nout = analyzer.analyze(img_fp, resized_shape=700)\nprint(out)\n```\n\n\n\n\n\n### 脚本使用\n\n**cnstd** 包含了几个命令行工具，安装 **cnstd** 后即可使用。\n\n#### STD 预测单个文件或文件夹中所有图片\n\n使用命令 **`cnstd predict`** 预测单个文件或文件夹中所有图片，以下是使用说明：\n\n```bash\n(venv) ➜  cnstd git:(master) ✗ cnstd predict -h\nUsage: cnstd predict [OPTIONS]\n\n  预测单个文件，或者指定目录下的所有图片\n\nOptions:\n  -m, --model-name [ch_PP-OCRv2_det|ch_PP-OCRv3_det|ch_PP-OCRv4_det|ch_PP-OCRv4_det_server|ch_PP-OCRv5_det|ch_PP-OCRv5_det_server|db_mobilenet_v3|db_mobilenet_v3_small|db_resnet18|db_resnet34|db_shufflenet_v2|db_shufflenet_v2_small|en_PP-OCRv3_det]\n                                  模型名称。默认值为 db_shufflenet_v2_small\n  -b, --model-backend [pytorch|onnx]\n                                  模型类型。默认值为 `onnx`\n  -p, --pretrained-model-fp TEXT  使用训练好的模型。默认为 `None`，表示使用系统自带的预训练模型\n  -r, --rotated-bbox              是否检测带角度（非水平和垂直）的文本框\n  --resized-shape TEXT            格式：\"height,width\";\n                                  预测时把图片resize到此大小再进行预测。两个值都需要是32的倍数。默认为\n                                  `768,768`\n  --box-score-thresh FLOAT        检测结果只保留分数大于此值的文本框。默认值为 `0.3`\n  --preserve-aspect-ratio BOOLEAN\n                                  resize时是否保留图片原始比例。默认值为 `True`\n  --context TEXT                  使用cpu还是 `gpu` 运行代码，也可指定为特定gpu，如`cuda:0`。默认为\n                                  `cpu`\n  -i, --img-file-or-dir TEXT      输入图片的文件路径或者指定的文件夹\n  -o, --output-dir TEXT           检测结果存放的文件夹。默认为 `./predictions`\n  -h, --help                      Show this message and exit.\n```\n\n例如可以使用以下命令对图片 `examples/taobao.jpg`进行检测，并把检测结果存放在目录 `outputs`中：\n\n```bash\ncnstd predict -i examples/taobao.jpg -o outputs\n```\n\n具体使用也可参考文件 [Makefile](./Makefile) 。\n\n\n\n#### MFD or Layout Analysis 预测单个文件\n\n使用命令 **`cnstd analyze`** 获得单个文件的 MFD 或者 Layout Analysis 结果，以下是使用说明：\n\n```bash\n(venv) ➜  cnstd git:(master) ✗ cnstd analyze -h\nUsage: cnstd analyze [OPTIONS]\n\n  对给定图片进行 MFD 或者 版面分析。\n\nOptions:\n  -m, --model-name TEXT           模型类型。`mfd` 表示数学公式检测，`layout`\n                                  表示版面分析；默认为：`mfd`\n  -t, --model-type TEXT           模型类型。当前支持 [`yolov7_tiny`, `yolov7`]\n  -b, --model-backend [pytorch|onnx]\n                                  模型后端架构。当前仅支持 `pytorch`\n  -c, --model-categories TEXT     模型的检测类别名称（\",\"分割）。默认值：None，表示基于 `model_name`\n                                  自动决定\n  -p, --model-fp TEXT             使用训练好的模型。默认为 `None`，表示使用系统自带的预训练模型\n  -y, --model-arch-yaml TEXT      模型的配置文件路径\n  --device TEXT                   cuda device, i.e. 0 or 0,1,2,3 or cpu\n  -i, --img-fp TEXT               待分析的图片路径或图片目录\n  -o, --output-fp TEXT            分析结果输出的图片路径。默认为 `None`，会存储在当前文件夹，文件名称为输入文件名称\n                                  前面增加`out-`；如输入文件名为 `img.jpg`, 输出文件名即为 `out-\n                                  img.jpg`；如果输入为目录，则此路径也应该是一个目录，会将输出文件存储在此目录下\n  --resized-shape INTEGER         分析时把图片resize到此大小再进行。默认为 `608`\n  --conf-thresh FLOAT             Confidence Threshold。默认值为 `0.25`\n  --iou-thresh FLOAT              IOU threshold for NMS。默认值为 `0.45`\n  -h, --help                      Show this message and exit.\n```\n\n例如可以使用以下命令对图片 `examples/mfd/zh.jpg` 进行 MFD，并把检测结果存放在文件 `out-zh.jpg` 中：\n\n```bash\n(venv) ➜  cnstd analyze -m mfd --conf-thresh 0.25 --resized-shape 800 -i examples/mfd/zh.jpg -o out-zh.jpg\n```\n\n具体使用也可参考文件 [Makefile](./Makefile) 。\n\n#### 模型训练\n\n使用命令 **`cnstd train`**  训练文本检测模型，以下是使用说明：\n\n```bash\n(venv) ➜  cnstd git:(master) ✗ cnstd train -h\nUsage: cnstd train [OPTIONS]\n\n  训练文本检测模型\n\nOptions:\n  -m, --model-name [db_resnet50|db_resnet34|db_resnet18|db_mobilenet_v3|db_mobilenet_v3_small|db_shufflenet_v2|db_shufflenet_v2_small|db_shufflenet_v2_tiny]\n                                  模型名称。默认值为 `db_shufflenet_v2_small`\n  -i, --index-dir TEXT            索引文件所在的文件夹，会读取文件夹中的 `train.tsv` 和 `dev.tsv` 文件\n                                  [required]\n\n  --train-config-fp TEXT          训练使用的json配置文件  [required]\n  -r, --resume-from-checkpoint TEXT\n                                  恢复此前中断的训练状态，继续训练\n  -p, --pretrained-model-fp TEXT  导入的训练好的模型，作为初始模型。优先级低于 \"--restore-training-\n                                  fp\"，当传入\"--restore-training-fp\"时，此传入失效\n\n  -h, --help                      Show this message and exit.\n```\n\n具体使用可参考文件 [Makefile](./Makefile) 。\n\n#### 模型转存\n\n训练好的模型会存储训练状态，使用命令 **`cnstd resave`**  去掉与预测无关的数据，降低模型大小。\n\n```bash\n(venv) ➜  cnstd git:(master) ✗ cnstd resave -h\nUsage: cnstd resave [OPTIONS]\n\n  训练好的模型会存储训练状态，使用此命令去掉预测时无关的数据，降低模型大小\n\nOptions:\n  -i, --input-model-fp TEXT   输入的模型文件路径  [required]\n  -o, --output-model-fp TEXT  输出的模型文件路径  [required]\n  -h, --help                  Show this message and exit.\n```\n\n## 未来工作\n\n* [x] 进一步精简模型结构，降低模型大小\n* [x] PSENet速度上还是比较慢，尝试更快的STD算法\n* [x] 加入更多的训练数据\n* [x] 加入对外部模型的支持\n* [x] 加入数学公式检测（MFD）与 版面分析（Layout Analysis）模型\n* [ ] 加入对文档结构与表格的检测\n\n\n\n## 给作者来杯咖啡\n\n开源不易，如果此项目对您有帮助，可以考虑 [给作者来杯咖啡 ☕️](https://cnocr.readthedocs.io/zh/latest/buymeacoffee/) 。\n\n---\n\n官方代码库：[https://github.com/breezedeus/cnstd](https://github.com/breezedeus/cnstd)。","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbreezedeus%2Fcnstd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbreezedeus%2Fcnstd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbreezedeus%2Fcnstd/lists"}