cnocr 2.3.1__tar.gz → 2.3.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. {cnocr-2.3.1 → cnocr-2.3.2}/PKG-INFO +18 -6
  2. {cnocr-2.3.1 → cnocr-2.3.2}/README.md +17 -5
  3. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/__version__.py +1 -1
  4. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/cli.py +2 -2
  5. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/cn_ocr.py +2 -2
  6. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/ppocr/consts.py +8 -0
  7. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/ppocr/rapid_recognizer.py +85 -14
  8. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/ppocr/utility.py +17 -5
  9. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr.egg-info/PKG-INFO +18 -6
  10. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr.egg-info/requires.txt +2 -2
  11. {cnocr-2.3.1 → cnocr-2.3.2}/setup.py +2 -2
  12. {cnocr-2.3.1 → cnocr-2.3.2}/LICENSE +0 -0
  13. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/__init__.py +0 -0
  14. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/app.py +0 -0
  15. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/classification/__init__.py +0 -0
  16. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/classification/dataset.py +0 -0
  17. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/classification/image_classifier.py +0 -0
  18. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/clf_cli.py +0 -0
  19. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/consts.py +0 -0
  20. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/data_utils/__init__.py +0 -0
  21. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/data_utils/aug.py +0 -0
  22. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/data_utils/block_shuffle.py +0 -0
  23. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/data_utils/transforms.py +0 -0
  24. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/data_utils/utils.py +0 -0
  25. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/dataset.py +0 -0
  26. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/dataset_utils.py +0 -0
  27. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/gradio_app.py +0 -0
  28. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/gradio_app2.py +0 -0
  29. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/label_cn.txt +0 -0
  30. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/label_number.txt +0 -0
  31. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/line_split.py +0 -0
  32. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/lr_scheduler.py +0 -0
  33. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/models/__init__.py +0 -0
  34. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/models/ctc.py +0 -0
  35. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/models/densenet.py +0 -0
  36. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/models/mobilenet.py +0 -0
  37. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/models/ocr_model.py +0 -0
  38. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/ppocr/__init__.py +0 -0
  39. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/ppocr/postprocess/__init__.py +0 -0
  40. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/ppocr/postprocess/rec_postprocess.py +0 -0
  41. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/ppocr/pp_recognizer.py +0 -0
  42. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/ppocr/utils/__init__.py +0 -0
  43. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/ppocr/utils/chinese_cht_dict.txt +0 -0
  44. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/ppocr/utils/en_dict.txt +0 -0
  45. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/ppocr/utils/ppocr_keys_v1.txt +0 -0
  46. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/recognizer.py +0 -0
  47. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/serve.py +0 -0
  48. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/trainer.py +0 -0
  49. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr/utils.py +0 -0
  50. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr.egg-info/SOURCES.txt +0 -0
  51. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr.egg-info/dependency_links.txt +0 -0
  52. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr.egg-info/entry_points.txt +0 -0
  53. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr.egg-info/not-zip-safe +0 -0
  54. {cnocr-2.3.1 → cnocr-2.3.2}/cnocr.egg-info/top_level.txt +0 -0
  55. {cnocr-2.3.1 → cnocr-2.3.2}/setup.cfg +0 -0
  56. {cnocr-2.3.1 → cnocr-2.3.2}/tests/test_cnocr.py +0 -0
  57. {cnocr-2.3.1 → cnocr-2.3.2}/tests/test_dataset.py +0 -0
  58. {cnocr-2.3.1 → cnocr-2.3.2}/tests/test_models.py +0 -0
  59. {cnocr-2.3.1 → cnocr-2.3.2}/tests/test_ppocr.py +0 -0
  60. {cnocr-2.3.1 → cnocr-2.3.2}/tests/test_pytorch.py +0 -0
  61. {cnocr-2.3.1 → cnocr-2.3.2}/tests/test_trainer.py +0 -0
  62. {cnocr-2.3.1 → cnocr-2.3.2}/tests/test_transforms.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cnocr
3
- Version: 2.3.1
3
+ Version: 2.3.2
4
4
  Summary: Python3 package for Chinese/English OCR, with small pretrained models
5
5
  Home-page: https://github.com/breezedeus/cnocr
6
6
  Author: breezedeus
@@ -69,7 +69,15 @@ License-File: LICENSE
69
69
  ---
70
70
  </div>
71
71
 
72
- ### [Update 2024.11.28]:发布 V2.3.1
72
+ ### Update 2025.06.26:发布 V2.3.2
73
+
74
+ 主要变更:
75
+
76
+ * 集成 PPOCRv5 最新版 OCR 模型
77
+ * 新增支持 PP-OCRv5 识别模型:`ch_PP-OCRv5` 和 `ch_PP-OCRv5_server`
78
+
79
+
80
+ ### [Update 2024.11.30]:发布 V2.3.1
73
81
 
74
82
  主要变更:
75
83
 
@@ -420,6 +428,8 @@ print(ocr_out)
420
428
  | db_mobilenet_v3_small | √ | X | cnocr | 7.9 M | 简体中文、繁体中文、英文、数字 | √ |
421
429
  | db_resnet34 | √ | X | cnocr | 86 M | 简体中文、繁体中文、英文、数字 | √ |
422
430
  | db_resnet18 | √ | X | cnocr | 47 M | 简体中文、繁体中文、英文、数字 | √ |
431
+ | ch_PP-OCRv5_det | X | √ | ppocr | 4.6 M | 简体中文、繁体中文、英文、数字 | √ |
432
+ | ch_PP-OCRv5_det_server | X | √ | ppocr | 84 M | 简体中文、繁体中文、英文、数字 | √ |
423
433
  | ch_PP-OCRv4_det | X | √ | ppocr | 4.5 M | 简体中文、繁体中文、英文、数字 | √ |
424
434
  | ch_PP-OCRv4_det_server | X | √ | ppocr | 108 M | 简体中文、繁体中文、英文、数字 | √ |
425
435
  | ch_PP-OCRv3_det | X | √ | ppocr | 2.3 M | 简体中文、繁体中文、英文、数字 | √ |
@@ -453,12 +463,14 @@ print(ocr_out)
453
463
  | **densenet_lite_246-gru_base** 🆕 <br /> ([星球会员](https://t.zsxq.com/FEYZRJQ)专享) | √ | √ | cnocr | 25 M | 简体中文、英文、数字 | X |
454
464
  | **scene-densenet_lite_246-gru_base** 🆕 <br /> ([星球会员](https://t.zsxq.com/FEYZRJQ)专享) | √ | √ | cnocr | 25 M | 简体中文、英文、数字 | X |
455
465
  | **doc-densenet_lite_246-gru_base** 🆕 <br /> ([星球会员](https://t.zsxq.com/FEYZRJQ)专享) | √ | √ | cnocr | 25 M | 简体中文、英文、数字 | X |
456
- | **densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://gf.bilibili.com/item/detail/1104812055)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
457
- | **scene-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://gf.bilibili.com/item/detail/1104815055)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
458
- | **doc-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://gf.bilibili.com/item/detail/1104820055)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
466
+ | **densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://mall.bilibili.com/neul-next/detailuniversal/detail.html?isMerchant=1&page=detailuniversal_detail&saleType=10&itemsId=11884138&loadingShow=1&noTitleBar=1&msource=merchant_share)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
467
+ | **scene-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://mall.bilibili.com/neul-next/detailuniversal/detail.html?isMerchant=1&page=detailuniversal_detail&saleType=10&itemsId=11883935&loadingShow=1&noTitleBar=1&msource=merchant_share)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
468
+ | **doc-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://mall.bilibili.com/neul-next/detailuniversal/detail.html?isMerchant=1&page=detailuniversal_detail&saleType=10&itemsId=11883965&loadingShow=1&noTitleBar=1&msource=merchant_share)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
459
469
  | **number-densenet_lite_136-fc** 🆕 | √ | √ | cnocr | 2.7 M | **纯数字**(仅包含 `0~9` 十个数字) | X |
460
470
  | **number-densenet_lite_136-gru** 🆕 <br /> ([星球会员](https://t.zsxq.com/FEYZRJQ)专享) | √ | √ | cnocr | 5.5 M | **纯数字**(仅包含 `0~9` 十个数字) | X |
461
- | **number-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://gf.bilibili.com/item/detail/1104055055)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 55 M | **纯数字**(仅包含 `0~9` 十个数字) | X |
471
+ | **number-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://mall.bilibili.com/neul-next/detailuniversal/detail.html?isMerchant=1&page=detailuniversal_detail&saleType=10&itemsId=11884155&loadingShow=1&noTitleBar=1&msource=merchant_share)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 55 M | **纯数字**(仅包含 `0~9` 十个数字) | X |
472
+ | ch_PP-OCRv5 | X | √ | ppocr | 16 M | 简体中文、英文、数字 | √ |
473
+ | ch_PP-OCRv5_server | X | √ | ppocr | 81 M | 简体中文、英文、数字 | √ |
462
474
  | ch_PP-OCRv4 | X | √ | ppocr | 10 M | 简体中文、英文、数字 | √ |
463
475
  | ch_PP-OCRv4_server | X | √ | ppocr | 86 M | 简体中文、英文、数字 | √ |
464
476
  | ch_PP-OCRv3 | X | √ | ppocr | 10 M | 简体中文、英文、数字 | √ |
@@ -39,7 +39,15 @@
39
39
  ---
40
40
  </div>
41
41
 
42
- ### [Update 2024.11.28]:发布 V2.3.1
42
+ ### Update 2025.06.26:发布 V2.3.2
43
+
44
+ 主要变更:
45
+
46
+ * 集成 PPOCRv5 最新版 OCR 模型
47
+ * 新增支持 PP-OCRv5 识别模型:`ch_PP-OCRv5` 和 `ch_PP-OCRv5_server`
48
+
49
+
50
+ ### [Update 2024.11.30]:发布 V2.3.1
43
51
 
44
52
  主要变更:
45
53
 
@@ -390,6 +398,8 @@ print(ocr_out)
390
398
  | db_mobilenet_v3_small | √ | X | cnocr | 7.9 M | 简体中文、繁体中文、英文、数字 | √ |
391
399
  | db_resnet34 | √ | X | cnocr | 86 M | 简体中文、繁体中文、英文、数字 | √ |
392
400
  | db_resnet18 | √ | X | cnocr | 47 M | 简体中文、繁体中文、英文、数字 | √ |
401
+ | ch_PP-OCRv5_det | X | √ | ppocr | 4.6 M | 简体中文、繁体中文、英文、数字 | √ |
402
+ | ch_PP-OCRv5_det_server | X | √ | ppocr | 84 M | 简体中文、繁体中文、英文、数字 | √ |
393
403
  | ch_PP-OCRv4_det | X | √ | ppocr | 4.5 M | 简体中文、繁体中文、英文、数字 | √ |
394
404
  | ch_PP-OCRv4_det_server | X | √ | ppocr | 108 M | 简体中文、繁体中文、英文、数字 | √ |
395
405
  | ch_PP-OCRv3_det | X | √ | ppocr | 2.3 M | 简体中文、繁体中文、英文、数字 | √ |
@@ -423,12 +433,14 @@ print(ocr_out)
423
433
  | **densenet_lite_246-gru_base** 🆕 <br /> ([星球会员](https://t.zsxq.com/FEYZRJQ)专享) | √ | √ | cnocr | 25 M | 简体中文、英文、数字 | X |
424
434
  | **scene-densenet_lite_246-gru_base** 🆕 <br /> ([星球会员](https://t.zsxq.com/FEYZRJQ)专享) | √ | √ | cnocr | 25 M | 简体中文、英文、数字 | X |
425
435
  | **doc-densenet_lite_246-gru_base** 🆕 <br /> ([星球会员](https://t.zsxq.com/FEYZRJQ)专享) | √ | √ | cnocr | 25 M | 简体中文、英文、数字 | X |
426
- | **densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://gf.bilibili.com/item/detail/1104812055)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
427
- | **scene-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://gf.bilibili.com/item/detail/1104815055)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
428
- | **doc-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://gf.bilibili.com/item/detail/1104820055)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
436
+ | **densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://mall.bilibili.com/neul-next/detailuniversal/detail.html?isMerchant=1&page=detailuniversal_detail&saleType=10&itemsId=11884138&loadingShow=1&noTitleBar=1&msource=merchant_share)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
437
+ | **scene-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://mall.bilibili.com/neul-next/detailuniversal/detail.html?isMerchant=1&page=detailuniversal_detail&saleType=10&itemsId=11883935&loadingShow=1&noTitleBar=1&msource=merchant_share)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
438
+ | **doc-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://mall.bilibili.com/neul-next/detailuniversal/detail.html?isMerchant=1&page=detailuniversal_detail&saleType=10&itemsId=11883965&loadingShow=1&noTitleBar=1&msource=merchant_share)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
429
439
  | **number-densenet_lite_136-fc** 🆕 | √ | √ | cnocr | 2.7 M | **纯数字**(仅包含 `0~9` 十个数字) | X |
430
440
  | **number-densenet_lite_136-gru** 🆕 <br /> ([星球会员](https://t.zsxq.com/FEYZRJQ)专享) | √ | √ | cnocr | 5.5 M | **纯数字**(仅包含 `0~9` 十个数字) | X |
431
- | **number-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://gf.bilibili.com/item/detail/1104055055)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 55 M | **纯数字**(仅包含 `0~9` 十个数字) | X |
441
+ | **number-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://mall.bilibili.com/neul-next/detailuniversal/detail.html?isMerchant=1&page=detailuniversal_detail&saleType=10&itemsId=11884155&loadingShow=1&noTitleBar=1&msource=merchant_share)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 55 M | **纯数字**(仅包含 `0~9` 十个数字) | X |
442
+ | ch_PP-OCRv5 | X | √ | ppocr | 16 M | 简体中文、英文、数字 | √ |
443
+ | ch_PP-OCRv5_server | X | √ | ppocr | 81 M | 简体中文、英文、数字 | √ |
432
444
  | ch_PP-OCRv4 | X | √ | ppocr | 10 M | 简体中文、英文、数字 | √ |
433
445
  | ch_PP-OCRv4_server | X | √ | ppocr | 86 M | 简体中文、英文、数字 | √ |
434
446
  | ch_PP-OCRv3 | X | √ | ppocr | 10 M | 简体中文、英文、数字 | √ |
@@ -17,4 +17,4 @@
17
17
  # specific language governing permissions and limitations
18
18
  # under the License.
19
19
 
20
- __version__ = '2.3.1'
20
+ __version__ = '2.3.2'
@@ -215,8 +215,8 @@ def visualize_example(example, fp_prefix):
215
215
  '-d',
216
216
  '--det-model-name',
217
217
  type=str,
218
- default='ch_PP-OCRv4_det',
219
- help='检测模型名称。默认值为 ch_PP-OCRv4_det',
218
+ default='ch_PP-OCRv5_det',
219
+ help='检测模型名称。默认值为 ch_PP-OCRv5_det',
220
220
  )
221
221
  @click.option(
222
222
  '--det-model-backend',
@@ -64,7 +64,7 @@ class CnOcr(object):
64
64
  self,
65
65
  rec_model_name: str = 'densenet_lite_136-gru',
66
66
  *,
67
- det_model_name: str = 'ch_PP-OCRv4_det',
67
+ det_model_name: str = 'ch_PP-OCRv5_det',
68
68
  cand_alphabet: Optional[Union[Collection, str]] = None,
69
69
  context: str = 'cpu', # ['cpu', 'gpu', 'cuda']
70
70
  rec_model_fp: Optional[str] = None,
@@ -83,7 +83,7 @@ class CnOcr(object):
83
83
 
84
84
  Args:
85
85
  rec_model_name (str): 识别模型名称。默认为 `densenet_lite_136-gru`
86
- det_model_name (str): 检测模型名称。默认为 `ch_PP-OCRv4_det`
86
+ det_model_name (str): 检测模型名称。默认为 `ch_PP-OCRv5_det`
87
87
  cand_alphabet (Optional[Union[Collection, str]]): 待识别字符所在的候选集合。默认为 `None`,表示不限定识别字符范围
88
88
  context (str): 'cpu', or 'gpu'。表明预测时是使用CPU还是GPU。默认为 `cpu`。
89
89
  此参数仅在 `model_backend=='pytorch'` 时有效。
@@ -73,6 +73,14 @@ MODEL_LABELS_FILE_DICT = {
73
73
  "recognizer": "RapidRecognizer",
74
74
  "repo": "breezedeus/cnocr-ppocr-ch_PP-OCRv4_server",
75
75
  },
76
+ ("ch_PP-OCRv5", "onnx"): {
77
+ "recognizer": "RapidRecognizer",
78
+ "repo": "breezedeus/cnocr-ppocr-ch_PP-OCRv5",
79
+ },
80
+ ("ch_PP-OCRv5_server", "onnx"): {
81
+ "recognizer": "RapidRecognizer",
82
+ "repo": "breezedeus/cnocr-ppocr-ch_PP-OCRv5_server",
83
+ },
76
84
  }
77
85
 
78
86
  PP_SPACE = "ppocr"
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- # Copyright (C) 2022-2024, [Breezedeus](https://github.com/breezedeus).
2
+ # Copyright (C) 2022-2025, [Breezedeus](https://github.com/breezedeus).
3
3
  # Licensed to the Apache Software Foundation (ASF) under one
4
4
  # or more contributor license agreements.
5
5
 
@@ -9,7 +9,9 @@ from typing import Union, Optional, List, Tuple
9
9
  from pathlib import Path
10
10
 
11
11
  import numpy as np
12
- from rapidocr_onnxruntime.ch_ppocr_rec.text_recognize import TextRecognizer
12
+ from rapidocr import EngineType, LangRec, ModelType, OCRVersion
13
+ from rapidocr.utils.typings import TaskType
14
+ from rapidocr.ch_ppocr_rec import TextRecognizer, TextRecInput
13
15
  from cnstd.utils import prepare_model_files
14
16
 
15
17
  from ..utils import data_dir, read_img
@@ -21,10 +23,66 @@ from ..consts import MODEL_VERSION, AVAILABLE_MODELS
21
23
  logger = logging.getLogger(__name__)
22
24
 
23
25
 
26
+ class Config(dict):
27
+ DEFAULT_CFG = {
28
+ "engine_type": EngineType.ONNXRUNTIME,
29
+ "lang_type": LangRec.CH,
30
+ "model_type": ModelType.MOBILE,
31
+ "ocr_version": OCRVersion.PPOCRV5,
32
+ "task_type": TaskType.REC,
33
+ "model_path": None,
34
+ "model_dir": None,
35
+ "rec_keys_path": None,
36
+ "rec_img_shape": [3, 48, 320],
37
+ "rec_batch_num": 6,
38
+ "engine_cfg": {
39
+ "intra_op_num_threads": -1,
40
+ "inter_op_num_threads": -1,
41
+ "enable_cpu_mem_arena": False,
42
+ "cpu_ep_cfg": {"arena_extend_strategy": "kSameAsRequested"},
43
+ "use_cuda": False,
44
+ "cuda_ep_cfg": {
45
+ "device_id": 0,
46
+ "arena_extend_strategy": "kNextPowerOfTwo",
47
+ "cudnn_conv_algo_search": "EXHAUSTIVE",
48
+ "do_copy_in_default_stream": True,
49
+ },
50
+ "use_dml": False,
51
+ "dm_ep_cfg": None,
52
+ "use_cann": False,
53
+ "cann_ep_cfg": {
54
+ "device_id": 0,
55
+ "arena_extend_strategy": "kNextPowerOfTwo",
56
+ "npu_mem_limit": 21474836480,
57
+ "op_select_impl_mode": "high_performance",
58
+ "optypelist_for_implmode": "Gelu",
59
+ "enable_cann_graph": True,
60
+ },
61
+ },
62
+ }
63
+
64
+ def __init__(self, *args, **kwargs):
65
+ super().__init__()
66
+ data = dict(*args, **kwargs)
67
+ for k, v in data.items():
68
+ if isinstance(v, dict):
69
+ v = Config(v)
70
+ self[k] = v
71
+
72
+ def __getattr__(self, name):
73
+ try:
74
+ return self[name]
75
+ except KeyError:
76
+ raise AttributeError(name)
77
+
78
+ def __setattr__(self, name, value):
79
+ self[name] = value
80
+
81
+
24
82
  class RapidRecognizer(Recognizer):
25
83
  def __init__(
26
84
  self,
27
- model_name: str = "ch_PP-OCRv3",
85
+ model_name: str = "ch_PP-OCRv5",
28
86
  *,
29
87
  model_fp: Optional[str] = None,
30
88
  root: Union[str, Path] = data_dir(),
@@ -36,11 +94,11 @@ class RapidRecognizer(Recognizer):
36
94
  基于 rapidocr_onnxruntime 的文本识别器。
37
95
 
38
96
  Args:
39
- model_name (str): 模型名称。默认为 `ch_PP-OCRv3`
97
+ model_name (str): 模型名称。默认为 `ch_PP-OCRv5`
40
98
  model_fp (Optional[str]): 如果不使用系统自带的模型,可以通过此参数直接指定所使用的模型文件('.onnx' 文件)
41
99
  root (Union[str, Path]): 模型文件所在的根目录
42
100
  context (str): 使用的设备。默认为 `cpu`,可选 `gpu`
43
- rec_image_shape (str): 输入图片尺寸,无需更改使用默认值即可。默认值:`"3, 32, 320"`
101
+ rec_image_shape (str): 输入图片尺寸,无需更改使用默认值即可。默认值:`"3, 48, 320"`
44
102
  **kwargs: 其他参数
45
103
  """
46
104
  self.rec_image_shape = [int(v) for v in rec_image_shape.split(",")]
@@ -50,12 +108,17 @@ class RapidRecognizer(Recognizer):
50
108
 
51
109
  self._assert_and_prepare_model_files(model_fp, root)
52
110
 
53
- config = {
54
- "use_cuda": use_gpu,
55
- "rec_img_shape": self.rec_image_shape,
56
- "rec_batch_num": 6,
57
- "model_path": self._model_fp,
58
- }
111
+ config = Config.DEFAULT_CFG
112
+ config["engine_cfg"]["use_cuda"] = use_gpu
113
+ if "engine_cfg" in kwargs:
114
+ config["engine_cfg"].update(kwargs["engine_cfg"])
115
+ config["rec_img_shape"] = self.rec_image_shape
116
+ config["model_path"] = self._model_fp
117
+ # 从 model_name 中获取 model_type 和 ocr_version
118
+ config["model_type"] = ModelType.SERVER if "server" in model_name else ModelType.MOBILE
119
+ config["ocr_version"] = OCRVersion.PPOCRV5 if "v5" in model_name else OCRVersion.PPOCRV4
120
+
121
+ config = Config(config)
59
122
  self.recognizer = TextRecognizer(config)
60
123
 
61
124
  def _assert_and_prepare_model_files(self, model_fp, root):
@@ -85,7 +148,10 @@ class RapidRecognizer(Recognizer):
85
148
  logger.info("use model: %s" % self._model_fp)
86
149
 
87
150
  def recognize(
88
- self, img_list: List[Union[str, Path, np.ndarray]], batch_size: int = 6
151
+ self,
152
+ img_list: List[Union[str, Path, np.ndarray]],
153
+ batch_size: int = 6,
154
+ return_word_box: bool = False,
89
155
  ) -> List[Tuple[str, float]]:
90
156
  """
91
157
  识别图片中的文字。
@@ -113,8 +179,13 @@ class RapidRecognizer(Recognizer):
113
179
  img = img[..., ::-1] # RGB to BGR
114
180
  img_data_list.append(img)
115
181
 
116
- results, _ = self.recognizer(img_data_list)
117
- return results
182
+ rec_input = TextRecInput(img=img_data_list, return_word_box=return_word_box)
183
+ try:
184
+ results = self.recognizer(rec_input)
185
+ return [(txt, score) for txt, score in zip(results.txts, results.scores)]
186
+ except Exception as e:
187
+ logger.error(f"Error recognizing image: {e}")
188
+ return []
118
189
 
119
190
  def recognize_one_line(
120
191
  self, img: Union[str, Path, np.ndarray]
@@ -21,7 +21,6 @@
21
21
  import argparse
22
22
  import os
23
23
  import sys
24
- import imghdr
25
24
  import math
26
25
  import logging
27
26
 
@@ -510,15 +509,28 @@ def get_image_file_list(img_file):
510
509
  raise Exception("not found any img file in {}".format(img_file))
511
510
 
512
511
  img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'GIF'}
513
- if os.path.isfile(img_file) and imghdr.what(img_file) in img_end:
514
- imgs_lists.append(img_file)
512
+
513
+ if os.path.isfile(img_file):
514
+ try:
515
+ with Image.open(img_file) as img:
516
+ if img.format and img.format.lower() in [ext.lower() for ext in img_end]:
517
+ imgs_lists.append(img_file)
518
+ except Exception:
519
+ pass
515
520
  elif os.path.isdir(img_file):
516
521
  for single_file in os.listdir(img_file):
517
522
  file_path = os.path.join(img_file, single_file)
518
- if os.path.isfile(file_path) and imghdr.what(file_path) in img_end:
519
- imgs_lists.append(file_path)
523
+ if os.path.isfile(file_path):
524
+ try:
525
+ with Image.open(file_path) as img:
526
+ if img.format and img.format.lower() in [ext.lower() for ext in img_end]:
527
+ imgs_lists.append(file_path)
528
+ except Exception:
529
+ pass
530
+
520
531
  if len(imgs_lists) == 0:
521
532
  raise Exception("not found any img file in {}".format(img_file))
533
+
522
534
  imgs_lists = sorted(imgs_lists)
523
535
  return imgs_lists
524
536
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cnocr
3
- Version: 2.3.1
3
+ Version: 2.3.2
4
4
  Summary: Python3 package for Chinese/English OCR, with small pretrained models
5
5
  Home-page: https://github.com/breezedeus/cnocr
6
6
  Author: breezedeus
@@ -69,7 +69,15 @@ License-File: LICENSE
69
69
  ---
70
70
  </div>
71
71
 
72
- ### [Update 2024.11.28]:发布 V2.3.1
72
+ ### Update 2025.06.26:发布 V2.3.2
73
+
74
+ 主要变更:
75
+
76
+ * 集成 PPOCRv5 最新版 OCR 模型
77
+ * 新增支持 PP-OCRv5 识别模型:`ch_PP-OCRv5` 和 `ch_PP-OCRv5_server`
78
+
79
+
80
+ ### [Update 2024.11.30]:发布 V2.3.1
73
81
 
74
82
  主要变更:
75
83
 
@@ -420,6 +428,8 @@ print(ocr_out)
420
428
  | db_mobilenet_v3_small | √ | X | cnocr | 7.9 M | 简体中文、繁体中文、英文、数字 | √ |
421
429
  | db_resnet34 | √ | X | cnocr | 86 M | 简体中文、繁体中文、英文、数字 | √ |
422
430
  | db_resnet18 | √ | X | cnocr | 47 M | 简体中文、繁体中文、英文、数字 | √ |
431
+ | ch_PP-OCRv5_det | X | √ | ppocr | 4.6 M | 简体中文、繁体中文、英文、数字 | √ |
432
+ | ch_PP-OCRv5_det_server | X | √ | ppocr | 84 M | 简体中文、繁体中文、英文、数字 | √ |
423
433
  | ch_PP-OCRv4_det | X | √ | ppocr | 4.5 M | 简体中文、繁体中文、英文、数字 | √ |
424
434
  | ch_PP-OCRv4_det_server | X | √ | ppocr | 108 M | 简体中文、繁体中文、英文、数字 | √ |
425
435
  | ch_PP-OCRv3_det | X | √ | ppocr | 2.3 M | 简体中文、繁体中文、英文、数字 | √ |
@@ -453,12 +463,14 @@ print(ocr_out)
453
463
  | **densenet_lite_246-gru_base** 🆕 <br /> ([星球会员](https://t.zsxq.com/FEYZRJQ)专享) | √ | √ | cnocr | 25 M | 简体中文、英文、数字 | X |
454
464
  | **scene-densenet_lite_246-gru_base** 🆕 <br /> ([星球会员](https://t.zsxq.com/FEYZRJQ)专享) | √ | √ | cnocr | 25 M | 简体中文、英文、数字 | X |
455
465
  | **doc-densenet_lite_246-gru_base** 🆕 <br /> ([星球会员](https://t.zsxq.com/FEYZRJQ)专享) | √ | √ | cnocr | 25 M | 简体中文、英文、数字 | X |
456
- | **densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://gf.bilibili.com/item/detail/1104812055)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
457
- | **scene-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://gf.bilibili.com/item/detail/1104815055)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
458
- | **doc-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://gf.bilibili.com/item/detail/1104820055)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
466
+ | **densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://mall.bilibili.com/neul-next/detailuniversal/detail.html?isMerchant=1&page=detailuniversal_detail&saleType=10&itemsId=11884138&loadingShow=1&noTitleBar=1&msource=merchant_share)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
467
+ | **scene-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://mall.bilibili.com/neul-next/detailuniversal/detail.html?isMerchant=1&page=detailuniversal_detail&saleType=10&itemsId=11883935&loadingShow=1&noTitleBar=1&msource=merchant_share)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
468
+ | **doc-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://mall.bilibili.com/neul-next/detailuniversal/detail.html?isMerchant=1&page=detailuniversal_detail&saleType=10&itemsId=11883965&loadingShow=1&noTitleBar=1&msource=merchant_share)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 82 M | 简体中文、英文、数字 | X |
459
469
  | **number-densenet_lite_136-fc** 🆕 | √ | √ | cnocr | 2.7 M | **纯数字**(仅包含 `0~9` 十个数字) | X |
460
470
  | **number-densenet_lite_136-gru** 🆕 <br /> ([星球会员](https://t.zsxq.com/FEYZRJQ)专享) | √ | √ | cnocr | 5.5 M | **纯数字**(仅包含 `0~9` 十个数字) | X |
461
- | **number-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://gf.bilibili.com/item/detail/1104055055)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 55 M | **纯数字**(仅包含 `0~9` 十个数字) | X |
471
+ | **number-densenet_lite_666-gru_large** 🆕 <br />(购买链接:[B站](https://mall.bilibili.com/neul-next/detailuniversal/detail.html?isMerchant=1&page=detailuniversal_detail&saleType=10&itemsId=11884155&loadingShow=1&noTitleBar=1&msource=merchant_share)、[Lemon Squeezy](https://ocr.lemonsqueezy.com/)) | √ | √ | cnocr | 55 M | **纯数字**(仅包含 `0~9` 十个数字) | X |
472
+ | ch_PP-OCRv5 | X | √ | ppocr | 16 M | 简体中文、英文、数字 | √ |
473
+ | ch_PP-OCRv5_server | X | √ | ppocr | 81 M | 简体中文、英文、数字 | √ |
462
474
  | ch_PP-OCRv4 | X | √ | ppocr | 10 M | 简体中文、英文、数字 | √ |
463
475
  | ch_PP-OCRv4_server | X | √ | ppocr | 86 M | 简体中文、英文、数字 | √ |
464
476
  | ch_PP-OCRv3 | X | √ | ppocr | 10 M | 简体中文、英文、数字 | √ |
@@ -8,8 +8,8 @@ wandb
8
8
  torchmetrics
9
9
  pillow>=5.3.0
10
10
  onnx
11
- cnstd>=1.2.5.1
12
- rapidocr_onnxruntime<1.4
11
+ cnstd>=1.2.6
12
+ rapidocr>=3.0
13
13
 
14
14
  [dev]
15
15
  albumentations
@@ -47,8 +47,8 @@ required = [
47
47
  "torchmetrics",
48
48
  "pillow>=5.3.0",
49
49
  "onnx",
50
- "cnstd>=1.2.5.1",
51
- "rapidocr_onnxruntime<1.4",
50
+ "cnstd>=1.2.6",
51
+ "rapidocr>=3.0",
52
52
  ]
53
53
  extras_require = {
54
54
  "ort-cpu": ["onnxruntime"],
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes