openocr-python 0.0.2__py3-none-any.whl

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 (323) hide show
  1. openocr/__init__.py +11 -0
  2. openocr/configs/det/dbnet/repvit_db.yml +173 -0
  3. openocr/configs/rec/abinet/resnet45_trans_abinet_lang.yml +94 -0
  4. openocr/configs/rec/abinet/resnet45_trans_abinet_wo_lang.yml +93 -0
  5. openocr/configs/rec/abinet/svtrv2_abinet_lang.yml +130 -0
  6. openocr/configs/rec/abinet/svtrv2_abinet_wo_lang.yml +128 -0
  7. openocr/configs/rec/aster/resnet31_lstm_aster_tps_on.yml +93 -0
  8. openocr/configs/rec/aster/svtrv2_aster.yml +127 -0
  9. openocr/configs/rec/aster/svtrv2_aster_tps_on.yml +102 -0
  10. openocr/configs/rec/autostr/autostr_lstm_aster_tps_on.yml +95 -0
  11. openocr/configs/rec/busnet/svtrv2_busnet.yml +135 -0
  12. openocr/configs/rec/busnet/svtrv2_busnet_pretraining.yml +134 -0
  13. openocr/configs/rec/busnet/vit_busnet.yml +104 -0
  14. openocr/configs/rec/busnet/vit_busnet_pretraining.yml +104 -0
  15. openocr/configs/rec/cam/convnextv2_cam_tps_on.yml +118 -0
  16. openocr/configs/rec/cam/convnextv2_tiny_cam_tps_on.yml +118 -0
  17. openocr/configs/rec/cam/svtrv2_cam_tps_on.yml +123 -0
  18. openocr/configs/rec/cdistnet/resnet45_trans_cdistnet.yml +93 -0
  19. openocr/configs/rec/cdistnet/svtrv2_cdistnet.yml +139 -0
  20. openocr/configs/rec/cppd/svtr_base_cppd.yml +123 -0
  21. openocr/configs/rec/cppd/svtr_base_cppd_ch.yml +126 -0
  22. openocr/configs/rec/cppd/svtr_base_cppd_h8.yml +123 -0
  23. openocr/configs/rec/cppd/svtr_base_cppd_syn.yml +124 -0
  24. openocr/configs/rec/cppd/svtrv2_cppd.yml +150 -0
  25. openocr/configs/rec/dan/resnet45_fpn_dan.yml +98 -0
  26. openocr/configs/rec/dan/svtrv2_dan.yml +130 -0
  27. openocr/configs/rec/focalsvtr/focalsvtr_ctc.yml +137 -0
  28. openocr/configs/rec/gtc/svtrv2_lnconv_nrtr_gtc.yml +168 -0
  29. openocr/configs/rec/gtc/svtrv2_lnconv_smtr_gtc_long_infer.yml +151 -0
  30. openocr/configs/rec/gtc/svtrv2_lnconv_smtr_gtc_smtr_long.yml +150 -0
  31. openocr/configs/rec/gtc/svtrv2_lnconv_smtr_gtc_stream.yml +152 -0
  32. openocr/configs/rec/igtr/svtr_base_ds_igtr.yml +157 -0
  33. openocr/configs/rec/lister/focalsvtr_lister_wo_fem_maxratio12.yml +133 -0
  34. openocr/configs/rec/lister/svtrv2_lister_wo_fem_maxratio12.yml +138 -0
  35. openocr/configs/rec/lpv/svtr_base_lpv.yml +124 -0
  36. openocr/configs/rec/lpv/svtr_base_lpv_wo_glrm.yml +123 -0
  37. openocr/configs/rec/lpv/svtrv2_lpv.yml +147 -0
  38. openocr/configs/rec/lpv/svtrv2_lpv_wo_glrm.yml +146 -0
  39. openocr/configs/rec/maerec/vit_nrtr.yml +116 -0
  40. openocr/configs/rec/matrn/resnet45_trans_matrn.yml +95 -0
  41. openocr/configs/rec/matrn/svtrv2_matrn.yml +130 -0
  42. openocr/configs/rec/mgpstr/svtrv2_mgpstr_only_char.yml +140 -0
  43. openocr/configs/rec/mgpstr/vit_base_mgpstr_only_char.yml +111 -0
  44. openocr/configs/rec/mgpstr/vit_large_mgpstr_only_char.yml +110 -0
  45. openocr/configs/rec/mgpstr/vit_mgpstr.yml +110 -0
  46. openocr/configs/rec/mgpstr/vit_mgpstr_only_char.yml +110 -0
  47. openocr/configs/rec/moran/resnet31_lstm_moran.yml +92 -0
  48. openocr/configs/rec/nrtr/focalsvtr_nrtr_maxraio12.yml +145 -0
  49. openocr/configs/rec/nrtr/nrtr.yml +107 -0
  50. openocr/configs/rec/nrtr/svtr_base_nrtr.yml +118 -0
  51. openocr/configs/rec/nrtr/svtr_base_nrtr_syn.yml +119 -0
  52. openocr/configs/rec/nrtr/svtrv2_nrtr.yml +146 -0
  53. openocr/configs/rec/ote/svtr_base_h8_ote.yml +117 -0
  54. openocr/configs/rec/ote/svtr_base_ote.yml +116 -0
  55. openocr/configs/rec/parseq/focalsvtr_parseq_maxratio12.yml +140 -0
  56. openocr/configs/rec/parseq/svrtv2_parseq.yml +136 -0
  57. openocr/configs/rec/parseq/vit_parseq.yml +100 -0
  58. openocr/configs/rec/robustscanner/resnet31_robustscanner.yml +102 -0
  59. openocr/configs/rec/robustscanner/svtrv2_robustscanner.yml +134 -0
  60. openocr/configs/rec/sar/resnet31_lstm_sar.yml +94 -0
  61. openocr/configs/rec/sar/svtrv2_sar.yml +128 -0
  62. openocr/configs/rec/seed/resnet31_lstm_seed_tps_on.yml +96 -0
  63. openocr/configs/rec/smtr/focalsvtr_smtr.yml +150 -0
  64. openocr/configs/rec/smtr/focalsvtr_smtr_long.yml +133 -0
  65. openocr/configs/rec/smtr/svtrv2_smtr.yml +150 -0
  66. openocr/configs/rec/smtr/svtrv2_smtr_bi.yml +136 -0
  67. openocr/configs/rec/srn/resnet50_fpn_srn.yml +97 -0
  68. openocr/configs/rec/srn/svtrv2_srn.yml +131 -0
  69. openocr/configs/rec/svtrs/convnextv2_ctc.yml +105 -0
  70. openocr/configs/rec/svtrs/convnextv2_h8_ctc.yml +105 -0
  71. openocr/configs/rec/svtrs/convnextv2_h8_rctc.yml +106 -0
  72. openocr/configs/rec/svtrs/convnextv2_rctc.yml +106 -0
  73. openocr/configs/rec/svtrs/convnextv2_tiny_h8_ctc.yml +105 -0
  74. openocr/configs/rec/svtrs/convnextv2_tiny_h8_rctc.yml +106 -0
  75. openocr/configs/rec/svtrs/crnn_ctc.yml +99 -0
  76. openocr/configs/rec/svtrs/crnn_ctc_long.yml +116 -0
  77. openocr/configs/rec/svtrs/focalnet_base_ctc.yml +108 -0
  78. openocr/configs/rec/svtrs/focalnet_base_rctc.yml +109 -0
  79. openocr/configs/rec/svtrs/focalsvtr_ctc.yml +106 -0
  80. openocr/configs/rec/svtrs/focalsvtr_rctc.yml +107 -0
  81. openocr/configs/rec/svtrs/resnet45_trans_ctc.yml +103 -0
  82. openocr/configs/rec/svtrs/resnet45_trans_rctc.yml +104 -0
  83. openocr/configs/rec/svtrs/svtr_base_ctc.yml +110 -0
  84. openocr/configs/rec/svtrs/svtr_base_rctc.yml +111 -0
  85. openocr/configs/rec/svtrs/svtrnet_ctc_syn.yml +111 -0
  86. openocr/configs/rec/svtrs/vit_ctc.yml +103 -0
  87. openocr/configs/rec/svtrs/vit_rctc.yml +103 -0
  88. openocr/configs/rec/svtrv2/repsvtr_ch.yml +121 -0
  89. openocr/configs/rec/svtrv2/svtrv2_ch.yml +133 -0
  90. openocr/configs/rec/svtrv2/svtrv2_ctc.yml +136 -0
  91. openocr/configs/rec/svtrv2/svtrv2_rctc.yml +135 -0
  92. openocr/configs/rec/svtrv2/svtrv2_small_rctc.yml +135 -0
  93. openocr/configs/rec/svtrv2/svtrv2_smtr_gtc_rctc.yml +162 -0
  94. openocr/configs/rec/svtrv2/svtrv2_smtr_gtc_rctc_ch.yml +153 -0
  95. openocr/configs/rec/svtrv2/svtrv2_tiny_rctc.yml +135 -0
  96. openocr/configs/rec/visionlan/resnet45_trans_visionlan_LA.yml +103 -0
  97. openocr/configs/rec/visionlan/resnet45_trans_visionlan_LF_1.yml +102 -0
  98. openocr/configs/rec/visionlan/resnet45_trans_visionlan_LF_2.yml +103 -0
  99. openocr/configs/rec/visionlan/svtrv2_visionlan_LA.yml +112 -0
  100. openocr/configs/rec/visionlan/svtrv2_visionlan_LF_1.yml +111 -0
  101. openocr/configs/rec/visionlan/svtrv2_visionlan_LF_2.yml +112 -0
  102. openocr/demo_gradio.py +128 -0
  103. openocr/opendet/modeling/__init__.py +11 -0
  104. openocr/opendet/modeling/backbones/__init__.py +14 -0
  105. openocr/opendet/modeling/backbones/repvit.py +340 -0
  106. openocr/opendet/modeling/base_detector.py +69 -0
  107. openocr/opendet/modeling/heads/__init__.py +14 -0
  108. openocr/opendet/modeling/heads/db_head.py +73 -0
  109. openocr/opendet/modeling/necks/__init__.py +14 -0
  110. openocr/opendet/modeling/necks/db_fpn.py +609 -0
  111. openocr/opendet/postprocess/__init__.py +18 -0
  112. openocr/opendet/postprocess/db_postprocess.py +273 -0
  113. openocr/opendet/preprocess/__init__.py +154 -0
  114. openocr/opendet/preprocess/crop_resize.py +121 -0
  115. openocr/opendet/preprocess/db_resize_for_test.py +135 -0
  116. openocr/openrec/losses/__init__.py +62 -0
  117. openocr/openrec/losses/abinet_loss.py +42 -0
  118. openocr/openrec/losses/ar_loss.py +23 -0
  119. openocr/openrec/losses/cam_loss.py +48 -0
  120. openocr/openrec/losses/cdistnet_loss.py +34 -0
  121. openocr/openrec/losses/ce_loss.py +68 -0
  122. openocr/openrec/losses/cppd_loss.py +77 -0
  123. openocr/openrec/losses/ctc_loss.py +33 -0
  124. openocr/openrec/losses/igtr_loss.py +12 -0
  125. openocr/openrec/losses/lister_loss.py +14 -0
  126. openocr/openrec/losses/lpv_loss.py +30 -0
  127. openocr/openrec/losses/mgp_loss.py +34 -0
  128. openocr/openrec/losses/parseq_loss.py +12 -0
  129. openocr/openrec/losses/robustscanner_loss.py +20 -0
  130. openocr/openrec/losses/seed_loss.py +46 -0
  131. openocr/openrec/losses/smtr_loss.py +12 -0
  132. openocr/openrec/losses/srn_loss.py +40 -0
  133. openocr/openrec/losses/visionlan_loss.py +58 -0
  134. openocr/openrec/metrics/__init__.py +19 -0
  135. openocr/openrec/metrics/rec_metric.py +270 -0
  136. openocr/openrec/metrics/rec_metric_gtc.py +58 -0
  137. openocr/openrec/metrics/rec_metric_long.py +142 -0
  138. openocr/openrec/metrics/rec_metric_mgp.py +93 -0
  139. openocr/openrec/modeling/__init__.py +11 -0
  140. openocr/openrec/modeling/base_recognizer.py +69 -0
  141. openocr/openrec/modeling/common.py +238 -0
  142. openocr/openrec/modeling/decoders/__init__.py +109 -0
  143. openocr/openrec/modeling/decoders/abinet_decoder.py +283 -0
  144. openocr/openrec/modeling/decoders/aster_decoder.py +170 -0
  145. openocr/openrec/modeling/decoders/bus_decoder.py +133 -0
  146. openocr/openrec/modeling/decoders/cam_decoder.py +43 -0
  147. openocr/openrec/modeling/decoders/cdistnet_decoder.py +334 -0
  148. openocr/openrec/modeling/decoders/cppd_decoder.py +393 -0
  149. openocr/openrec/modeling/decoders/ctc_decoder.py +203 -0
  150. openocr/openrec/modeling/decoders/dan_decoder.py +203 -0
  151. openocr/openrec/modeling/decoders/igtr_decoder.py +815 -0
  152. openocr/openrec/modeling/decoders/lister_decoder.py +535 -0
  153. openocr/openrec/modeling/decoders/lpv_decoder.py +119 -0
  154. openocr/openrec/modeling/decoders/matrn_decoder.py +236 -0
  155. openocr/openrec/modeling/decoders/mgp_decoder.py +99 -0
  156. openocr/openrec/modeling/decoders/nrtr_decoder.py +439 -0
  157. openocr/openrec/modeling/decoders/ote_decoder.py +205 -0
  158. openocr/openrec/modeling/decoders/parseq_decoder.py +504 -0
  159. openocr/openrec/modeling/decoders/rctc_decoder.py +70 -0
  160. openocr/openrec/modeling/decoders/robustscanner_decoder.py +749 -0
  161. openocr/openrec/modeling/decoders/sar_decoder.py +236 -0
  162. openocr/openrec/modeling/decoders/smtr_decoder.py +621 -0
  163. openocr/openrec/modeling/decoders/smtr_decoder_nattn.py +521 -0
  164. openocr/openrec/modeling/decoders/srn_decoder.py +283 -0
  165. openocr/openrec/modeling/decoders/visionlan_decoder.py +321 -0
  166. openocr/openrec/modeling/encoders/__init__.py +39 -0
  167. openocr/openrec/modeling/encoders/autostr_encoder.py +327 -0
  168. openocr/openrec/modeling/encoders/cam_encoder.py +760 -0
  169. openocr/openrec/modeling/encoders/convnextv2.py +213 -0
  170. openocr/openrec/modeling/encoders/focalsvtr.py +631 -0
  171. openocr/openrec/modeling/encoders/nrtr_encoder.py +28 -0
  172. openocr/openrec/modeling/encoders/rec_hgnet.py +346 -0
  173. openocr/openrec/modeling/encoders/rec_lcnetv3.py +488 -0
  174. openocr/openrec/modeling/encoders/rec_mobilenet_v3.py +132 -0
  175. openocr/openrec/modeling/encoders/rec_mv1_enhance.py +254 -0
  176. openocr/openrec/modeling/encoders/rec_nrtr_mtb.py +37 -0
  177. openocr/openrec/modeling/encoders/rec_resnet_31.py +213 -0
  178. openocr/openrec/modeling/encoders/rec_resnet_45.py +183 -0
  179. openocr/openrec/modeling/encoders/rec_resnet_fpn.py +216 -0
  180. openocr/openrec/modeling/encoders/rec_resnet_vd.py +252 -0
  181. openocr/openrec/modeling/encoders/repvit.py +338 -0
  182. openocr/openrec/modeling/encoders/resnet31_rnn.py +123 -0
  183. openocr/openrec/modeling/encoders/svtrnet.py +574 -0
  184. openocr/openrec/modeling/encoders/svtrnet2dpos.py +616 -0
  185. openocr/openrec/modeling/encoders/svtrv2.py +470 -0
  186. openocr/openrec/modeling/encoders/svtrv2_lnconv.py +503 -0
  187. openocr/openrec/modeling/encoders/svtrv2_lnconv_two33.py +517 -0
  188. openocr/openrec/modeling/encoders/vit.py +120 -0
  189. openocr/openrec/modeling/transforms/__init__.py +15 -0
  190. openocr/openrec/modeling/transforms/aster_tps.py +262 -0
  191. openocr/openrec/modeling/transforms/moran.py +136 -0
  192. openocr/openrec/modeling/transforms/tps.py +246 -0
  193. openocr/openrec/optimizer/__init__.py +73 -0
  194. openocr/openrec/optimizer/lr.py +227 -0
  195. openocr/openrec/postprocess/__init__.py +72 -0
  196. openocr/openrec/postprocess/abinet_postprocess.py +37 -0
  197. openocr/openrec/postprocess/ar_postprocess.py +63 -0
  198. openocr/openrec/postprocess/ce_postprocess.py +43 -0
  199. openocr/openrec/postprocess/char_postprocess.py +108 -0
  200. openocr/openrec/postprocess/cppd_postprocess.py +42 -0
  201. openocr/openrec/postprocess/ctc_postprocess.py +119 -0
  202. openocr/openrec/postprocess/igtr_postprocess.py +100 -0
  203. openocr/openrec/postprocess/lister_postprocess.py +59 -0
  204. openocr/openrec/postprocess/mgp_postprocess.py +143 -0
  205. openocr/openrec/postprocess/nrtr_postprocess.py +75 -0
  206. openocr/openrec/postprocess/smtr_postprocess.py +73 -0
  207. openocr/openrec/postprocess/srn_postprocess.py +80 -0
  208. openocr/openrec/postprocess/visionlan_postprocess.py +81 -0
  209. openocr/openrec/preprocess/__init__.py +173 -0
  210. openocr/openrec/preprocess/abinet_aug.py +473 -0
  211. openocr/openrec/preprocess/abinet_label_encode.py +36 -0
  212. openocr/openrec/preprocess/ar_label_encode.py +36 -0
  213. openocr/openrec/preprocess/auto_augment.py +1012 -0
  214. openocr/openrec/preprocess/cam_label_encode.py +141 -0
  215. openocr/openrec/preprocess/ce_label_encode.py +116 -0
  216. openocr/openrec/preprocess/char_label_encode.py +36 -0
  217. openocr/openrec/preprocess/cppd_label_encode.py +173 -0
  218. openocr/openrec/preprocess/ctc_label_encode.py +124 -0
  219. openocr/openrec/preprocess/ep_label_encode.py +38 -0
  220. openocr/openrec/preprocess/igtr_label_encode.py +360 -0
  221. openocr/openrec/preprocess/mgp_label_encode.py +95 -0
  222. openocr/openrec/preprocess/parseq_aug.py +150 -0
  223. openocr/openrec/preprocess/rec_aug.py +211 -0
  224. openocr/openrec/preprocess/resize.py +534 -0
  225. openocr/openrec/preprocess/smtr_label_encode.py +125 -0
  226. openocr/openrec/preprocess/srn_label_encode.py +37 -0
  227. openocr/openrec/preprocess/visionlan_label_encode.py +67 -0
  228. openocr/tools/create_lmdb_dataset.py +118 -0
  229. openocr/tools/data/__init__.py +94 -0
  230. openocr/tools/data/collate_fn.py +100 -0
  231. openocr/tools/data/lmdb_dataset.py +142 -0
  232. openocr/tools/data/lmdb_dataset_test.py +166 -0
  233. openocr/tools/data/multi_scale_sampler.py +177 -0
  234. openocr/tools/data/ratio_dataset.py +217 -0
  235. openocr/tools/data/ratio_dataset_test.py +273 -0
  236. openocr/tools/data/ratio_dataset_tvresize.py +213 -0
  237. openocr/tools/data/ratio_dataset_tvresize_test.py +276 -0
  238. openocr/tools/data/ratio_sampler.py +190 -0
  239. openocr/tools/data/simple_dataset.py +263 -0
  240. openocr/tools/data/strlmdb_dataset.py +143 -0
  241. openocr/tools/engine/__init__.py +5 -0
  242. openocr/tools/engine/config.py +158 -0
  243. openocr/tools/engine/trainer.py +621 -0
  244. openocr/tools/eval_rec.py +41 -0
  245. openocr/tools/eval_rec_all_ch.py +184 -0
  246. openocr/tools/eval_rec_all_en.py +206 -0
  247. openocr/tools/eval_rec_all_long.py +119 -0
  248. openocr/tools/eval_rec_all_long_simple.py +122 -0
  249. openocr/tools/export_rec.py +118 -0
  250. openocr/tools/infer/onnx_engine.py +65 -0
  251. openocr/tools/infer/predict_rec.py +140 -0
  252. openocr/tools/infer/utility.py +234 -0
  253. openocr/tools/infer_det.py +449 -0
  254. openocr/tools/infer_e2e.py +462 -0
  255. openocr/tools/infer_e2e_parallel.py +184 -0
  256. openocr/tools/infer_rec.py +371 -0
  257. openocr/tools/train_rec.py +37 -0
  258. openocr/tools/utility.py +45 -0
  259. openocr/tools/utils/EN_symbol_dict.txt +94 -0
  260. openocr/tools/utils/__init__.py +0 -0
  261. openocr/tools/utils/ckpt.py +87 -0
  262. openocr/tools/utils/dict/ar_dict.txt +117 -0
  263. openocr/tools/utils/dict/arabic_dict.txt +161 -0
  264. openocr/tools/utils/dict/be_dict.txt +145 -0
  265. openocr/tools/utils/dict/bg_dict.txt +140 -0
  266. openocr/tools/utils/dict/chinese_cht_dict.txt +8421 -0
  267. openocr/tools/utils/dict/cyrillic_dict.txt +163 -0
  268. openocr/tools/utils/dict/devanagari_dict.txt +167 -0
  269. openocr/tools/utils/dict/en_dict.txt +63 -0
  270. openocr/tools/utils/dict/fa_dict.txt +136 -0
  271. openocr/tools/utils/dict/french_dict.txt +136 -0
  272. openocr/tools/utils/dict/german_dict.txt +143 -0
  273. openocr/tools/utils/dict/hi_dict.txt +162 -0
  274. openocr/tools/utils/dict/it_dict.txt +118 -0
  275. openocr/tools/utils/dict/japan_dict.txt +4399 -0
  276. openocr/tools/utils/dict/ka_dict.txt +153 -0
  277. openocr/tools/utils/dict/kie_dict/xfund_class_list.txt +4 -0
  278. openocr/tools/utils/dict/korean_dict.txt +3688 -0
  279. openocr/tools/utils/dict/latex_symbol_dict.txt +111 -0
  280. openocr/tools/utils/dict/latin_dict.txt +185 -0
  281. openocr/tools/utils/dict/layout_dict/layout_cdla_dict.txt +10 -0
  282. openocr/tools/utils/dict/layout_dict/layout_publaynet_dict.txt +5 -0
  283. openocr/tools/utils/dict/layout_dict/layout_table_dict.txt +1 -0
  284. openocr/tools/utils/dict/mr_dict.txt +153 -0
  285. openocr/tools/utils/dict/ne_dict.txt +153 -0
  286. openocr/tools/utils/dict/oc_dict.txt +96 -0
  287. openocr/tools/utils/dict/pu_dict.txt +130 -0
  288. openocr/tools/utils/dict/rs_dict.txt +91 -0
  289. openocr/tools/utils/dict/rsc_dict.txt +134 -0
  290. openocr/tools/utils/dict/ru_dict.txt +125 -0
  291. openocr/tools/utils/dict/spin_dict.txt +68 -0
  292. openocr/tools/utils/dict/ta_dict.txt +128 -0
  293. openocr/tools/utils/dict/table_dict.txt +277 -0
  294. openocr/tools/utils/dict/table_master_structure_dict.txt +39 -0
  295. openocr/tools/utils/dict/table_structure_dict.txt +28 -0
  296. openocr/tools/utils/dict/table_structure_dict_ch.txt +48 -0
  297. openocr/tools/utils/dict/te_dict.txt +151 -0
  298. openocr/tools/utils/dict/ug_dict.txt +114 -0
  299. openocr/tools/utils/dict/uk_dict.txt +142 -0
  300. openocr/tools/utils/dict/ur_dict.txt +137 -0
  301. openocr/tools/utils/dict/xi_dict.txt +110 -0
  302. openocr/tools/utils/dict90.txt +90 -0
  303. openocr/tools/utils/e2e_metric/Deteval.py +802 -0
  304. openocr/tools/utils/e2e_metric/polygon_fast.py +70 -0
  305. openocr/tools/utils/e2e_utils/extract_batchsize.py +86 -0
  306. openocr/tools/utils/e2e_utils/extract_textpoint_fast.py +479 -0
  307. openocr/tools/utils/e2e_utils/extract_textpoint_slow.py +582 -0
  308. openocr/tools/utils/e2e_utils/pgnet_pp_utils.py +159 -0
  309. openocr/tools/utils/e2e_utils/visual.py +152 -0
  310. openocr/tools/utils/en_dict.txt +95 -0
  311. openocr/tools/utils/gen_label.py +68 -0
  312. openocr/tools/utils/ic15_dict.txt +36 -0
  313. openocr/tools/utils/logging.py +56 -0
  314. openocr/tools/utils/poly_nms.py +132 -0
  315. openocr/tools/utils/ppocr_keys_v1.txt +6623 -0
  316. openocr/tools/utils/stats.py +58 -0
  317. openocr/tools/utils/utility.py +165 -0
  318. openocr/tools/utils/visual.py +117 -0
  319. openocr_python-0.0.2.dist-info/LICENCE +201 -0
  320. openocr_python-0.0.2.dist-info/METADATA +98 -0
  321. openocr_python-0.0.2.dist-info/RECORD +323 -0
  322. openocr_python-0.0.2.dist-info/WHEEL +5 -0
  323. openocr_python-0.0.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,70 @@
1
+ import numpy as np
2
+ from shapely.geometry import Polygon
3
+ """
4
+ :param det_x: [1, N] Xs of detection's vertices
5
+ :param det_y: [1, N] Ys of detection's vertices
6
+ :param gt_x: [1, N] Xs of groundtruth's vertices
7
+ :param gt_y: [1, N] Ys of groundtruth's vertices
8
+
9
+ ##############
10
+ All the calculation of 'AREA' in this script is handled by:
11
+ 1) First generating a binary mask with the polygon area filled up with 1's
12
+ 2) Summing up all the 1's
13
+ """
14
+
15
+
16
+ def area(x, y):
17
+ polygon = Polygon(np.stack([x, y], axis=1))
18
+ return float(polygon.area)
19
+
20
+
21
+ def approx_area_of_intersection(det_x, det_y, gt_x, gt_y):
22
+ """
23
+ This helper determine if both polygons are intersecting with each others with an approximation method.
24
+ Area of intersection represented by the minimum bounding rectangular [xmin, ymin, xmax, ymax]
25
+ """
26
+ det_ymax = np.max(det_y)
27
+ det_xmax = np.max(det_x)
28
+ det_ymin = np.min(det_y)
29
+ det_xmin = np.min(det_x)
30
+
31
+ gt_ymax = np.max(gt_y)
32
+ gt_xmax = np.max(gt_x)
33
+ gt_ymin = np.min(gt_y)
34
+ gt_xmin = np.min(gt_x)
35
+
36
+ all_min_ymax = np.minimum(det_ymax, gt_ymax)
37
+ all_max_ymin = np.maximum(det_ymin, gt_ymin)
38
+
39
+ intersect_heights = np.maximum(0.0, (all_min_ymax - all_max_ymin))
40
+
41
+ all_min_xmax = np.minimum(det_xmax, gt_xmax)
42
+ all_max_xmin = np.maximum(det_xmin, gt_xmin)
43
+ intersect_widths = np.maximum(0.0, (all_min_xmax - all_max_xmin))
44
+
45
+ return intersect_heights * intersect_widths
46
+
47
+
48
+ def area_of_intersection(det_x, det_y, gt_x, gt_y):
49
+ p1 = Polygon(np.stack([det_x, det_y], axis=1)).buffer(0)
50
+ p2 = Polygon(np.stack([gt_x, gt_y], axis=1)).buffer(0)
51
+ return float(p1.intersection(p2).area)
52
+
53
+
54
+ def area_of_union(det_x, det_y, gt_x, gt_y):
55
+ p1 = Polygon(np.stack([det_x, det_y], axis=1)).buffer(0)
56
+ p2 = Polygon(np.stack([gt_x, gt_y], axis=1)).buffer(0)
57
+ return float(p1.union(p2).area)
58
+
59
+
60
+ def iou(det_x, det_y, gt_x, gt_y):
61
+ return area_of_intersection(det_x, det_y, gt_x, gt_y) / (
62
+ area_of_union(det_x, det_y, gt_x, gt_y) + 1.0)
63
+
64
+
65
+ def iod(det_x, det_y, gt_x, gt_y):
66
+ """
67
+ This helper determine the fraction of intersection area over detection area
68
+ """
69
+ return area_of_intersection(det_x, det_y, gt_x, gt_y) / (
70
+ area(det_x, det_y) + 1.0)
@@ -0,0 +1,86 @@
1
+ import torch
2
+ import numpy as np
3
+ import copy
4
+
5
+
6
+ def org_tcl_rois(batch_size, pos_lists, pos_masks, label_lists, tcl_bs):
7
+ """ """
8
+ pos_lists_, pos_masks_, label_lists_ = [], [], []
9
+ img_bs = batch_size
10
+ ngpu = int(batch_size / img_bs)
11
+ img_ids = np.array(pos_lists, dtype=np.int32)[:, 0, 0].copy()
12
+ pos_lists_split, pos_masks_split, label_lists_split = [], [], []
13
+ for i in range(ngpu):
14
+ pos_lists_split.append([])
15
+ pos_masks_split.append([])
16
+ label_lists_split.append([])
17
+
18
+ for i in range(img_ids.shape[0]):
19
+ img_id = img_ids[i]
20
+ gpu_id = int(img_id / img_bs)
21
+ img_id = img_id % img_bs
22
+ pos_list = pos_lists[i].copy()
23
+ pos_list[:, 0] = img_id
24
+ pos_lists_split[gpu_id].append(pos_list)
25
+ pos_masks_split[gpu_id].append(pos_masks[i].copy())
26
+ label_lists_split[gpu_id].append(copy.deepcopy(label_lists[i]))
27
+ # repeat or delete
28
+ for i in range(ngpu):
29
+ vp_len = len(pos_lists_split[i])
30
+ if vp_len <= tcl_bs:
31
+ for j in range(0, tcl_bs - vp_len):
32
+ pos_list = pos_lists_split[i][j].copy()
33
+ pos_lists_split[i].append(pos_list)
34
+ pos_mask = pos_masks_split[i][j].copy()
35
+ pos_masks_split[i].append(pos_mask)
36
+ label_list = copy.deepcopy(label_lists_split[i][j])
37
+ label_lists_split[i].append(label_list)
38
+ else:
39
+ for j in range(0, vp_len - tcl_bs):
40
+ c_len = len(pos_lists_split[i])
41
+ pop_id = np.random.permutation(c_len)[0]
42
+ pos_lists_split[i].pop(pop_id)
43
+ pos_masks_split[i].pop(pop_id)
44
+ label_lists_split[i].pop(pop_id)
45
+ # merge
46
+ for i in range(ngpu):
47
+ pos_lists_.extend(pos_lists_split[i])
48
+ pos_masks_.extend(pos_masks_split[i])
49
+ label_lists_.extend(label_lists_split[i])
50
+ return pos_lists_, pos_masks_, label_lists_
51
+
52
+
53
+ def pre_process(label_list, pos_list, pos_mask, max_text_length, max_text_nums,
54
+ pad_num, tcl_bs):
55
+ label_list = label_list.numpy()
56
+ batch, _, _, _ = label_list.shape
57
+ pos_list = pos_list.numpy()
58
+ pos_mask = pos_mask.numpy()
59
+ pos_list_t = []
60
+ pos_mask_t = []
61
+ label_list_t = []
62
+ for i in range(batch):
63
+ for j in range(max_text_nums):
64
+ if pos_mask[i, j].any():
65
+ pos_list_t.append(pos_list[i][j])
66
+ pos_mask_t.append(pos_mask[i][j])
67
+ label_list_t.append(label_list[i][j])
68
+ pos_list, pos_mask, label_list = org_tcl_rois(batch, pos_list_t, pos_mask_t,
69
+ label_list_t, tcl_bs)
70
+ label = []
71
+ tt = [l.tolist() for l in label_list]
72
+ for i in range(tcl_bs):
73
+ k = 0
74
+ for j in range(max_text_length):
75
+ if tt[i][j][0] != pad_num:
76
+ k += 1
77
+ else:
78
+ break
79
+ label.append(k)
80
+ label = torch.tensor(label)
81
+ label = label.long()
82
+ pos_list = torch.tensor(pos_list)
83
+ pos_mask = torch.tensor(pos_mask)
84
+ label_list = torch.squeeze(torch.tensor(label_list), dim=2)
85
+ label_list = label_list.int()
86
+ return pos_list, pos_mask, label_list, label
@@ -0,0 +1,479 @@
1
+ import cv2
2
+ import math
3
+
4
+ import numpy as np
5
+ from itertools import groupby
6
+ from skimage.morphology._skeletonize import thin
7
+
8
+
9
+ def get_dict(character_dict_path):
10
+ character_str = ""
11
+ with open(character_dict_path, "rb") as fin:
12
+ lines = fin.readlines()
13
+ for line in lines:
14
+ line = line.decode("utf-8").strip("\n").strip("\r\n")
15
+ character_str += line
16
+ dict_character = list(character_str)
17
+ return dict_character
18
+
19
+
20
+ def softmax(logits):
21
+ """
22
+ logits: N x d
23
+ """
24
+ max_value = np.max(logits, axis=1, keepdims=True)
25
+ exp = np.exp(logits - max_value)
26
+ exp_sum = np.sum(exp, axis=1, keepdims=True)
27
+ dist = exp / exp_sum
28
+ return dist
29
+
30
+
31
+ def get_keep_pos_idxs(labels, remove_blank=None):
32
+ """
33
+ Remove duplicate and get pos idxs of keep items.
34
+ The value of keep_blank should be [None, 95].
35
+ """
36
+ duplicate_len_list = []
37
+ keep_pos_idx_list = []
38
+ keep_char_idx_list = []
39
+ for k, v_ in groupby(labels):
40
+ current_len = len(list(v_))
41
+ if k != remove_blank:
42
+ current_idx = int(sum(duplicate_len_list) + current_len // 2)
43
+ keep_pos_idx_list.append(current_idx)
44
+ keep_char_idx_list.append(k)
45
+ duplicate_len_list.append(current_len)
46
+ return keep_char_idx_list, keep_pos_idx_list
47
+
48
+
49
+ def remove_blank(labels, blank=0):
50
+ new_labels = [x for x in labels if x != blank]
51
+ return new_labels
52
+
53
+
54
+ def insert_blank(labels, blank=0):
55
+ new_labels = [blank]
56
+ for l in labels:
57
+ new_labels += [l, blank]
58
+ return new_labels
59
+
60
+
61
+ def ctc_greedy_decoder(probs_seq, blank=95, keep_blank_in_idxs=True):
62
+ """
63
+ CTC greedy (best path) decoder.
64
+ """
65
+ raw_str = np.argmax(np.array(probs_seq), axis=1)
66
+ remove_blank_in_pos = None if keep_blank_in_idxs else blank
67
+ dedup_str, keep_idx_list = get_keep_pos_idxs(
68
+ raw_str, remove_blank=remove_blank_in_pos)
69
+ dst_str = remove_blank(dedup_str, blank=blank)
70
+ return dst_str, keep_idx_list
71
+
72
+
73
+ def instance_ctc_greedy_decoder(gather_info,
74
+ logits_map,
75
+ pts_num=4,
76
+ point_gather_mode=None):
77
+ _, _, C = logits_map.shape
78
+ if point_gather_mode == "align":
79
+ insert_num = 0
80
+ gather_info = np.array(gather_info)
81
+ length = len(gather_info) - 1
82
+ for index in range(length):
83
+ stride_y = np.abs(gather_info[index + insert_num][0] - gather_info[
84
+ index + 1 + insert_num][0])
85
+ stride_x = np.abs(gather_info[index + insert_num][1] - gather_info[
86
+ index + 1 + insert_num][1])
87
+ max_points = int(max(stride_x, stride_y))
88
+ stride = (gather_info[index + insert_num] -
89
+ gather_info[index + 1 + insert_num]) / (max_points)
90
+ insert_num_temp = max_points - 1
91
+
92
+ for i in range(int(insert_num_temp)):
93
+ insert_value = gather_info[index + insert_num] - (i + 1
94
+ ) * stride
95
+ insert_index = index + i + 1 + insert_num
96
+ gather_info = np.insert(
97
+ gather_info, insert_index, insert_value, axis=0)
98
+ insert_num += insert_num_temp
99
+ gather_info = gather_info.tolist()
100
+ else:
101
+ pass
102
+ ys, xs = zip(*gather_info)
103
+ logits_seq = logits_map[list(ys), list(xs)]
104
+ probs_seq = logits_seq
105
+ labels = np.argmax(probs_seq, axis=1)
106
+ dst_str = [k for k, v_ in groupby(labels) if k != C - 1]
107
+ detal = len(gather_info) // (pts_num - 1)
108
+ keep_idx_list = [0] + [detal * (i + 1) for i in range(pts_num - 2)] + [-1]
109
+ keep_gather_list = [gather_info[idx] for idx in keep_idx_list]
110
+ return dst_str, keep_gather_list
111
+
112
+
113
+ def ctc_decoder_for_image(gather_info_list,
114
+ logits_map,
115
+ Lexicon_Table,
116
+ pts_num=6,
117
+ point_gather_mode=None):
118
+ """
119
+ CTC decoder using multiple processes.
120
+ """
121
+ decoder_str = []
122
+ decoder_xys = []
123
+ for gather_info in gather_info_list:
124
+ if len(gather_info) < pts_num:
125
+ continue
126
+ dst_str, xys_list = instance_ctc_greedy_decoder(
127
+ gather_info,
128
+ logits_map,
129
+ pts_num=pts_num,
130
+ point_gather_mode=point_gather_mode, )
131
+ dst_str_readable = "".join([Lexicon_Table[idx] for idx in dst_str])
132
+ if len(dst_str_readable) < 2:
133
+ continue
134
+ decoder_str.append(dst_str_readable)
135
+ decoder_xys.append(xys_list)
136
+ return decoder_str, decoder_xys
137
+
138
+
139
+ def sort_with_direction(pos_list, f_direction):
140
+ """
141
+ f_direction: h x w x 2
142
+ pos_list: [[y, x], [y, x], [y, x] ...]
143
+ """
144
+
145
+ def sort_part_with_direction(pos_list, point_direction):
146
+ pos_list = np.array(pos_list).reshape(-1, 2)
147
+ point_direction = np.array(point_direction).reshape(-1, 2)
148
+ average_direction = np.mean(point_direction, axis=0, keepdims=True)
149
+ pos_proj_leng = np.sum(pos_list * average_direction, axis=1)
150
+ sorted_list = pos_list[np.argsort(pos_proj_leng)].tolist()
151
+ sorted_direction = point_direction[np.argsort(pos_proj_leng)].tolist()
152
+ return sorted_list, sorted_direction
153
+
154
+ pos_list = np.array(pos_list).reshape(-1, 2)
155
+ point_direction = f_direction[pos_list[:, 0], pos_list[:, 1]] # x, y
156
+ point_direction = point_direction[:, ::-1] # x, y -> y, x
157
+ sorted_point, sorted_direction = sort_part_with_direction(pos_list,
158
+ point_direction)
159
+
160
+ point_num = len(sorted_point)
161
+ if point_num >= 16:
162
+ middle_num = point_num // 2
163
+ first_part_point = sorted_point[:middle_num]
164
+ first_point_direction = sorted_direction[:middle_num]
165
+ sorted_fist_part_point, sorted_fist_part_direction = sort_part_with_direction(
166
+ first_part_point, first_point_direction)
167
+
168
+ last_part_point = sorted_point[middle_num:]
169
+ last_point_direction = sorted_direction[middle_num:]
170
+ sorted_last_part_point, sorted_last_part_direction = sort_part_with_direction(
171
+ last_part_point, last_point_direction)
172
+ sorted_point = sorted_fist_part_point + sorted_last_part_point
173
+ sorted_direction = sorted_fist_part_direction + sorted_last_part_direction
174
+
175
+ return sorted_point, np.array(sorted_direction)
176
+
177
+
178
+ def add_id(pos_list, image_id=0):
179
+ """
180
+ Add id for gather feature, for inference.
181
+ """
182
+ new_list = []
183
+ for item in pos_list:
184
+ new_list.append((image_id, item[0], item[1]))
185
+ return new_list
186
+
187
+
188
+ def sort_and_expand_with_direction(pos_list, f_direction):
189
+ """
190
+ f_direction: h x w x 2
191
+ pos_list: [[y, x], [y, x], [y, x] ...]
192
+ """
193
+ h, w, _ = f_direction.shape
194
+ sorted_list, point_direction = sort_with_direction(pos_list, f_direction)
195
+
196
+ point_num = len(sorted_list)
197
+ sub_direction_len = max(point_num // 3, 2)
198
+ left_direction = point_direction[:sub_direction_len, :]
199
+ right_dirction = point_direction[point_num - sub_direction_len:, :]
200
+
201
+ left_average_direction = -np.mean(left_direction, axis=0, keepdims=True)
202
+ left_average_len = np.linalg.norm(left_average_direction)
203
+ left_start = np.array(sorted_list[0])
204
+ left_step = left_average_direction / (left_average_len + 1e-6)
205
+
206
+ right_average_direction = np.mean(right_dirction, axis=0, keepdims=True)
207
+ right_average_len = np.linalg.norm(right_average_direction)
208
+ right_step = right_average_direction / (right_average_len + 1e-6)
209
+ right_start = np.array(sorted_list[-1])
210
+
211
+ append_num = max(
212
+ int((left_average_len + right_average_len) / 2.0 * 0.15), 1)
213
+ left_list = []
214
+ right_list = []
215
+ for i in range(append_num):
216
+ ly, lx = (np.round(left_start + left_step * (i + 1)).flatten()
217
+ .astype("int32").tolist())
218
+ if ly < h and lx < w and (ly, lx) not in left_list:
219
+ left_list.append((ly, lx))
220
+ ry, rx = (np.round(right_start + right_step * (i + 1)).flatten()
221
+ .astype("int32").tolist())
222
+ if ry < h and rx < w and (ry, rx) not in right_list:
223
+ right_list.append((ry, rx))
224
+
225
+ all_list = left_list[::-1] + sorted_list + right_list
226
+ return all_list
227
+
228
+
229
+ def sort_and_expand_with_direction_v2(pos_list, f_direction, binary_tcl_map):
230
+ """
231
+ f_direction: h x w x 2
232
+ pos_list: [[y, x], [y, x], [y, x] ...]
233
+ binary_tcl_map: h x w
234
+ """
235
+ h, w, _ = f_direction.shape
236
+ sorted_list, point_direction = sort_with_direction(pos_list, f_direction)
237
+
238
+ point_num = len(sorted_list)
239
+ sub_direction_len = max(point_num // 3, 2)
240
+ left_direction = point_direction[:sub_direction_len, :]
241
+ right_dirction = point_direction[point_num - sub_direction_len:, :]
242
+
243
+ left_average_direction = -np.mean(left_direction, axis=0, keepdims=True)
244
+ left_average_len = np.linalg.norm(left_average_direction)
245
+ left_start = np.array(sorted_list[0])
246
+ left_step = left_average_direction / (left_average_len + 1e-6)
247
+
248
+ right_average_direction = np.mean(right_dirction, axis=0, keepdims=True)
249
+ right_average_len = np.linalg.norm(right_average_direction)
250
+ right_step = right_average_direction / (right_average_len + 1e-6)
251
+ right_start = np.array(sorted_list[-1])
252
+
253
+ append_num = max(
254
+ int((left_average_len + right_average_len) / 2.0 * 0.15), 1)
255
+ max_append_num = 2 * append_num
256
+
257
+ left_list = []
258
+ right_list = []
259
+ for i in range(max_append_num):
260
+ ly, lx = (np.round(left_start + left_step * (i + 1)).flatten()
261
+ .astype("int32").tolist())
262
+ if ly < h and lx < w and (ly, lx) not in left_list:
263
+ if binary_tcl_map[ly, lx] > 0.5:
264
+ left_list.append((ly, lx))
265
+ else:
266
+ break
267
+
268
+ for i in range(max_append_num):
269
+ ry, rx = (np.round(right_start + right_step * (i + 1)).flatten()
270
+ .astype("int32").tolist())
271
+ if ry < h and rx < w and (ry, rx) not in right_list:
272
+ if binary_tcl_map[ry, rx] > 0.5:
273
+ right_list.append((ry, rx))
274
+ else:
275
+ break
276
+
277
+ all_list = left_list[::-1] + sorted_list + right_list
278
+ return all_list
279
+
280
+
281
+ def point_pair2poly(point_pair_list):
282
+ """
283
+ Transfer vertical point_pairs into poly point in clockwise.
284
+ """
285
+ point_num = len(point_pair_list) * 2
286
+ point_list = [0] * point_num
287
+ for idx, point_pair in enumerate(point_pair_list):
288
+ point_list[idx] = point_pair[0]
289
+ point_list[point_num - 1 - idx] = point_pair[1]
290
+ return np.array(point_list).reshape(-1, 2)
291
+
292
+
293
+ def shrink_quad_along_width(quad, begin_width_ratio=0.0, end_width_ratio=1.0):
294
+ ratio_pair = np.array(
295
+ [[begin_width_ratio], [end_width_ratio]], dtype=np.float32)
296
+ p0_1 = quad[0] + (quad[1] - quad[0]) * ratio_pair
297
+ p3_2 = quad[3] + (quad[2] - quad[3]) * ratio_pair
298
+ return np.array([p0_1[0], p0_1[1], p3_2[1], p3_2[0]])
299
+
300
+
301
+ def expand_poly_along_width(poly, shrink_ratio_of_width=0.3):
302
+ """
303
+ expand poly along width.
304
+ """
305
+ point_num = poly.shape[0]
306
+ left_quad = np.array(
307
+ [poly[0], poly[1], poly[-2], poly[-1]], dtype=np.float32)
308
+ left_ratio = (-shrink_ratio_of_width *
309
+ np.linalg.norm(left_quad[0] - left_quad[3]) /
310
+ (np.linalg.norm(left_quad[0] - left_quad[1]) + 1e-6))
311
+ left_quad_expand = shrink_quad_along_width(left_quad, left_ratio, 1.0)
312
+ right_quad = np.array(
313
+ [
314
+ poly[point_num // 2 - 2],
315
+ poly[point_num // 2 - 1],
316
+ poly[point_num // 2],
317
+ poly[point_num // 2 + 1],
318
+ ],
319
+ dtype=np.float32, )
320
+ right_ratio = 1.0 + shrink_ratio_of_width * np.linalg.norm(right_quad[
321
+ 0] - right_quad[3]) / (np.linalg.norm(right_quad[0] - right_quad[1]) +
322
+ 1e-6)
323
+ right_quad_expand = shrink_quad_along_width(right_quad, 0.0, right_ratio)
324
+ poly[0] = left_quad_expand[0]
325
+ poly[-1] = left_quad_expand[-1]
326
+ poly[point_num // 2 - 1] = right_quad_expand[1]
327
+ poly[point_num // 2] = right_quad_expand[2]
328
+ return poly
329
+
330
+
331
+ def restore_poly(instance_yxs_list, seq_strs, p_border, ratio_w, ratio_h, src_w,
332
+ src_h, valid_set):
333
+ poly_list = []
334
+ keep_str_list = []
335
+ for yx_center_line, keep_str in zip(instance_yxs_list, seq_strs):
336
+ if len(keep_str) < 2:
337
+ print("--> too short, {}".format(keep_str))
338
+ continue
339
+
340
+ offset_expand = 1.0
341
+ if valid_set == "totaltext":
342
+ offset_expand = 1.2
343
+
344
+ point_pair_list = []
345
+ for y, x in yx_center_line:
346
+ offset = p_border[:, y, x].reshape(2, 2) * offset_expand
347
+ ori_yx = np.array([y, x], dtype=np.float32)
348
+ point_pair = ((ori_yx + offset)[:, ::-1] * 4.0 /
349
+ np.array([ratio_w, ratio_h]).reshape(-1, 2))
350
+ point_pair_list.append(point_pair)
351
+
352
+ detected_poly = point_pair2poly(point_pair_list)
353
+ detected_poly = expand_poly_along_width(
354
+ detected_poly, shrink_ratio_of_width=0.2)
355
+ detected_poly[:, 0] = np.clip(detected_poly[:, 0], a_min=0, a_max=src_w)
356
+ detected_poly[:, 1] = np.clip(detected_poly[:, 1], a_min=0, a_max=src_h)
357
+
358
+ keep_str_list.append(keep_str)
359
+ if valid_set == "partvgg":
360
+ middle_point = len(detected_poly) // 2
361
+ detected_poly = detected_poly[
362
+ [0, middle_point - 1, middle_point, -1], :]
363
+ poly_list.append(detected_poly)
364
+ elif valid_set == "totaltext":
365
+ poly_list.append(detected_poly)
366
+ else:
367
+ print("--> Not supported format.")
368
+ exit(-1)
369
+ return poly_list, keep_str_list
370
+
371
+
372
+ def generate_pivot_list_fast(
373
+ p_score,
374
+ p_char_maps,
375
+ f_direction,
376
+ Lexicon_Table,
377
+ score_thresh=0.5,
378
+ point_gather_mode=None, ):
379
+ """
380
+ return center point and end point of TCL instance; filter with the char maps;
381
+ """
382
+ p_score = p_score[0]
383
+ f_direction = f_direction.transpose(1, 2, 0)
384
+ p_tcl_map = (p_score > score_thresh) * 1.0
385
+ skeleton_map = thin(p_tcl_map.astype(np.uint8))
386
+ instance_count, instance_label_map = cv2.connectedComponents(
387
+ skeleton_map.astype(np.uint8), connectivity=8)
388
+
389
+ # get TCL Instance
390
+ all_pos_yxs = []
391
+ if instance_count > 0:
392
+ for instance_id in range(1, instance_count):
393
+ pos_list = []
394
+ ys, xs = np.where(instance_label_map == instance_id)
395
+ pos_list = list(zip(ys, xs))
396
+
397
+ if len(pos_list) < 3:
398
+ continue
399
+
400
+ pos_list_sorted = sort_and_expand_with_direction_v2(
401
+ pos_list, f_direction, p_tcl_map)
402
+ all_pos_yxs.append(pos_list_sorted)
403
+
404
+ p_char_maps = p_char_maps.transpose([1, 2, 0])
405
+ decoded_str, keep_yxs_list = ctc_decoder_for_image(
406
+ all_pos_yxs,
407
+ logits_map=p_char_maps,
408
+ Lexicon_Table=Lexicon_Table,
409
+ point_gather_mode=point_gather_mode, )
410
+ return keep_yxs_list, decoded_str
411
+
412
+
413
+ def extract_main_direction(pos_list, f_direction):
414
+ """
415
+ f_direction: h x w x 2
416
+ pos_list: [[y, x], [y, x], [y, x] ...]
417
+ """
418
+ pos_list = np.array(pos_list)
419
+ point_direction = f_direction[pos_list[:, 0], pos_list[:, 1]]
420
+ point_direction = point_direction[:, ::-1] # x, y -> y, x
421
+ average_direction = np.mean(point_direction, axis=0, keepdims=True)
422
+ average_direction = average_direction / (
423
+ np.linalg.norm(average_direction) + 1e-6)
424
+ return average_direction
425
+
426
+
427
+ def sort_by_direction_with_image_id_deprecated(pos_list, f_direction):
428
+ """
429
+ f_direction: h x w x 2
430
+ pos_list: [[id, y, x], [id, y, x], [id, y, x] ...]
431
+ """
432
+ pos_list_full = np.array(pos_list).reshape(-1, 3)
433
+ pos_list = pos_list_full[:, 1:]
434
+ point_direction = f_direction[pos_list[:, 0], pos_list[:, 1]] # x, y
435
+ point_direction = point_direction[:, ::-1] # x, y -> y, x
436
+ average_direction = np.mean(point_direction, axis=0, keepdims=True)
437
+ pos_proj_leng = np.sum(pos_list * average_direction, axis=1)
438
+ sorted_list = pos_list_full[np.argsort(pos_proj_leng)].tolist()
439
+ return sorted_list
440
+
441
+
442
+ def sort_by_direction_with_image_id(pos_list, f_direction):
443
+ """
444
+ f_direction: h x w x 2
445
+ pos_list: [[y, x], [y, x], [y, x] ...]
446
+ """
447
+
448
+ def sort_part_with_direction(pos_list_full, point_direction):
449
+ pos_list_full = np.array(pos_list_full).reshape(-1, 3)
450
+ pos_list = pos_list_full[:, 1:]
451
+ point_direction = np.array(point_direction).reshape(-1, 2)
452
+ average_direction = np.mean(point_direction, axis=0, keepdims=True)
453
+ pos_proj_leng = np.sum(pos_list * average_direction, axis=1)
454
+ sorted_list = pos_list_full[np.argsort(pos_proj_leng)].tolist()
455
+ sorted_direction = point_direction[np.argsort(pos_proj_leng)].tolist()
456
+ return sorted_list, sorted_direction
457
+
458
+ pos_list = np.array(pos_list).reshape(-1, 3)
459
+ point_direction = f_direction[pos_list[:, 1], pos_list[:, 2]] # x, y
460
+ point_direction = point_direction[:, ::-1] # x, y -> y, x
461
+ sorted_point, sorted_direction = sort_part_with_direction(pos_list,
462
+ point_direction)
463
+
464
+ point_num = len(sorted_point)
465
+ if point_num >= 16:
466
+ middle_num = point_num // 2
467
+ first_part_point = sorted_point[:middle_num]
468
+ first_point_direction = sorted_direction[:middle_num]
469
+ sorted_fist_part_point, sorted_fist_part_direction = sort_part_with_direction(
470
+ first_part_point, first_point_direction)
471
+
472
+ last_part_point = sorted_point[middle_num:]
473
+ last_point_direction = sorted_direction[middle_num:]
474
+ sorted_last_part_point, sorted_last_part_direction = sort_part_with_direction(
475
+ last_part_point, last_point_direction)
476
+ sorted_point = sorted_fist_part_point + sorted_last_part_point
477
+ sorted_direction = sorted_fist_part_direction + sorted_last_part_direction
478
+
479
+ return sorted_point