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.
- openocr/__init__.py +11 -0
- openocr/configs/det/dbnet/repvit_db.yml +173 -0
- openocr/configs/rec/abinet/resnet45_trans_abinet_lang.yml +94 -0
- openocr/configs/rec/abinet/resnet45_trans_abinet_wo_lang.yml +93 -0
- openocr/configs/rec/abinet/svtrv2_abinet_lang.yml +130 -0
- openocr/configs/rec/abinet/svtrv2_abinet_wo_lang.yml +128 -0
- openocr/configs/rec/aster/resnet31_lstm_aster_tps_on.yml +93 -0
- openocr/configs/rec/aster/svtrv2_aster.yml +127 -0
- openocr/configs/rec/aster/svtrv2_aster_tps_on.yml +102 -0
- openocr/configs/rec/autostr/autostr_lstm_aster_tps_on.yml +95 -0
- openocr/configs/rec/busnet/svtrv2_busnet.yml +135 -0
- openocr/configs/rec/busnet/svtrv2_busnet_pretraining.yml +134 -0
- openocr/configs/rec/busnet/vit_busnet.yml +104 -0
- openocr/configs/rec/busnet/vit_busnet_pretraining.yml +104 -0
- openocr/configs/rec/cam/convnextv2_cam_tps_on.yml +118 -0
- openocr/configs/rec/cam/convnextv2_tiny_cam_tps_on.yml +118 -0
- openocr/configs/rec/cam/svtrv2_cam_tps_on.yml +123 -0
- openocr/configs/rec/cdistnet/resnet45_trans_cdistnet.yml +93 -0
- openocr/configs/rec/cdistnet/svtrv2_cdistnet.yml +139 -0
- openocr/configs/rec/cppd/svtr_base_cppd.yml +123 -0
- openocr/configs/rec/cppd/svtr_base_cppd_ch.yml +126 -0
- openocr/configs/rec/cppd/svtr_base_cppd_h8.yml +123 -0
- openocr/configs/rec/cppd/svtr_base_cppd_syn.yml +124 -0
- openocr/configs/rec/cppd/svtrv2_cppd.yml +150 -0
- openocr/configs/rec/dan/resnet45_fpn_dan.yml +98 -0
- openocr/configs/rec/dan/svtrv2_dan.yml +130 -0
- openocr/configs/rec/focalsvtr/focalsvtr_ctc.yml +137 -0
- openocr/configs/rec/gtc/svtrv2_lnconv_nrtr_gtc.yml +168 -0
- openocr/configs/rec/gtc/svtrv2_lnconv_smtr_gtc_long_infer.yml +151 -0
- openocr/configs/rec/gtc/svtrv2_lnconv_smtr_gtc_smtr_long.yml +150 -0
- openocr/configs/rec/gtc/svtrv2_lnconv_smtr_gtc_stream.yml +152 -0
- openocr/configs/rec/igtr/svtr_base_ds_igtr.yml +157 -0
- openocr/configs/rec/lister/focalsvtr_lister_wo_fem_maxratio12.yml +133 -0
- openocr/configs/rec/lister/svtrv2_lister_wo_fem_maxratio12.yml +138 -0
- openocr/configs/rec/lpv/svtr_base_lpv.yml +124 -0
- openocr/configs/rec/lpv/svtr_base_lpv_wo_glrm.yml +123 -0
- openocr/configs/rec/lpv/svtrv2_lpv.yml +147 -0
- openocr/configs/rec/lpv/svtrv2_lpv_wo_glrm.yml +146 -0
- openocr/configs/rec/maerec/vit_nrtr.yml +116 -0
- openocr/configs/rec/matrn/resnet45_trans_matrn.yml +95 -0
- openocr/configs/rec/matrn/svtrv2_matrn.yml +130 -0
- openocr/configs/rec/mgpstr/svtrv2_mgpstr_only_char.yml +140 -0
- openocr/configs/rec/mgpstr/vit_base_mgpstr_only_char.yml +111 -0
- openocr/configs/rec/mgpstr/vit_large_mgpstr_only_char.yml +110 -0
- openocr/configs/rec/mgpstr/vit_mgpstr.yml +110 -0
- openocr/configs/rec/mgpstr/vit_mgpstr_only_char.yml +110 -0
- openocr/configs/rec/moran/resnet31_lstm_moran.yml +92 -0
- openocr/configs/rec/nrtr/focalsvtr_nrtr_maxraio12.yml +145 -0
- openocr/configs/rec/nrtr/nrtr.yml +107 -0
- openocr/configs/rec/nrtr/svtr_base_nrtr.yml +118 -0
- openocr/configs/rec/nrtr/svtr_base_nrtr_syn.yml +119 -0
- openocr/configs/rec/nrtr/svtrv2_nrtr.yml +146 -0
- openocr/configs/rec/ote/svtr_base_h8_ote.yml +117 -0
- openocr/configs/rec/ote/svtr_base_ote.yml +116 -0
- openocr/configs/rec/parseq/focalsvtr_parseq_maxratio12.yml +140 -0
- openocr/configs/rec/parseq/svrtv2_parseq.yml +136 -0
- openocr/configs/rec/parseq/vit_parseq.yml +100 -0
- openocr/configs/rec/robustscanner/resnet31_robustscanner.yml +102 -0
- openocr/configs/rec/robustscanner/svtrv2_robustscanner.yml +134 -0
- openocr/configs/rec/sar/resnet31_lstm_sar.yml +94 -0
- openocr/configs/rec/sar/svtrv2_sar.yml +128 -0
- openocr/configs/rec/seed/resnet31_lstm_seed_tps_on.yml +96 -0
- openocr/configs/rec/smtr/focalsvtr_smtr.yml +150 -0
- openocr/configs/rec/smtr/focalsvtr_smtr_long.yml +133 -0
- openocr/configs/rec/smtr/svtrv2_smtr.yml +150 -0
- openocr/configs/rec/smtr/svtrv2_smtr_bi.yml +136 -0
- openocr/configs/rec/srn/resnet50_fpn_srn.yml +97 -0
- openocr/configs/rec/srn/svtrv2_srn.yml +131 -0
- openocr/configs/rec/svtrs/convnextv2_ctc.yml +105 -0
- openocr/configs/rec/svtrs/convnextv2_h8_ctc.yml +105 -0
- openocr/configs/rec/svtrs/convnextv2_h8_rctc.yml +106 -0
- openocr/configs/rec/svtrs/convnextv2_rctc.yml +106 -0
- openocr/configs/rec/svtrs/convnextv2_tiny_h8_ctc.yml +105 -0
- openocr/configs/rec/svtrs/convnextv2_tiny_h8_rctc.yml +106 -0
- openocr/configs/rec/svtrs/crnn_ctc.yml +99 -0
- openocr/configs/rec/svtrs/crnn_ctc_long.yml +116 -0
- openocr/configs/rec/svtrs/focalnet_base_ctc.yml +108 -0
- openocr/configs/rec/svtrs/focalnet_base_rctc.yml +109 -0
- openocr/configs/rec/svtrs/focalsvtr_ctc.yml +106 -0
- openocr/configs/rec/svtrs/focalsvtr_rctc.yml +107 -0
- openocr/configs/rec/svtrs/resnet45_trans_ctc.yml +103 -0
- openocr/configs/rec/svtrs/resnet45_trans_rctc.yml +104 -0
- openocr/configs/rec/svtrs/svtr_base_ctc.yml +110 -0
- openocr/configs/rec/svtrs/svtr_base_rctc.yml +111 -0
- openocr/configs/rec/svtrs/svtrnet_ctc_syn.yml +111 -0
- openocr/configs/rec/svtrs/vit_ctc.yml +103 -0
- openocr/configs/rec/svtrs/vit_rctc.yml +103 -0
- openocr/configs/rec/svtrv2/repsvtr_ch.yml +121 -0
- openocr/configs/rec/svtrv2/svtrv2_ch.yml +133 -0
- openocr/configs/rec/svtrv2/svtrv2_ctc.yml +136 -0
- openocr/configs/rec/svtrv2/svtrv2_rctc.yml +135 -0
- openocr/configs/rec/svtrv2/svtrv2_small_rctc.yml +135 -0
- openocr/configs/rec/svtrv2/svtrv2_smtr_gtc_rctc.yml +162 -0
- openocr/configs/rec/svtrv2/svtrv2_smtr_gtc_rctc_ch.yml +153 -0
- openocr/configs/rec/svtrv2/svtrv2_tiny_rctc.yml +135 -0
- openocr/configs/rec/visionlan/resnet45_trans_visionlan_LA.yml +103 -0
- openocr/configs/rec/visionlan/resnet45_trans_visionlan_LF_1.yml +102 -0
- openocr/configs/rec/visionlan/resnet45_trans_visionlan_LF_2.yml +103 -0
- openocr/configs/rec/visionlan/svtrv2_visionlan_LA.yml +112 -0
- openocr/configs/rec/visionlan/svtrv2_visionlan_LF_1.yml +111 -0
- openocr/configs/rec/visionlan/svtrv2_visionlan_LF_2.yml +112 -0
- openocr/demo_gradio.py +128 -0
- openocr/opendet/modeling/__init__.py +11 -0
- openocr/opendet/modeling/backbones/__init__.py +14 -0
- openocr/opendet/modeling/backbones/repvit.py +340 -0
- openocr/opendet/modeling/base_detector.py +69 -0
- openocr/opendet/modeling/heads/__init__.py +14 -0
- openocr/opendet/modeling/heads/db_head.py +73 -0
- openocr/opendet/modeling/necks/__init__.py +14 -0
- openocr/opendet/modeling/necks/db_fpn.py +609 -0
- openocr/opendet/postprocess/__init__.py +18 -0
- openocr/opendet/postprocess/db_postprocess.py +273 -0
- openocr/opendet/preprocess/__init__.py +154 -0
- openocr/opendet/preprocess/crop_resize.py +121 -0
- openocr/opendet/preprocess/db_resize_for_test.py +135 -0
- openocr/openrec/losses/__init__.py +62 -0
- openocr/openrec/losses/abinet_loss.py +42 -0
- openocr/openrec/losses/ar_loss.py +23 -0
- openocr/openrec/losses/cam_loss.py +48 -0
- openocr/openrec/losses/cdistnet_loss.py +34 -0
- openocr/openrec/losses/ce_loss.py +68 -0
- openocr/openrec/losses/cppd_loss.py +77 -0
- openocr/openrec/losses/ctc_loss.py +33 -0
- openocr/openrec/losses/igtr_loss.py +12 -0
- openocr/openrec/losses/lister_loss.py +14 -0
- openocr/openrec/losses/lpv_loss.py +30 -0
- openocr/openrec/losses/mgp_loss.py +34 -0
- openocr/openrec/losses/parseq_loss.py +12 -0
- openocr/openrec/losses/robustscanner_loss.py +20 -0
- openocr/openrec/losses/seed_loss.py +46 -0
- openocr/openrec/losses/smtr_loss.py +12 -0
- openocr/openrec/losses/srn_loss.py +40 -0
- openocr/openrec/losses/visionlan_loss.py +58 -0
- openocr/openrec/metrics/__init__.py +19 -0
- openocr/openrec/metrics/rec_metric.py +270 -0
- openocr/openrec/metrics/rec_metric_gtc.py +58 -0
- openocr/openrec/metrics/rec_metric_long.py +142 -0
- openocr/openrec/metrics/rec_metric_mgp.py +93 -0
- openocr/openrec/modeling/__init__.py +11 -0
- openocr/openrec/modeling/base_recognizer.py +69 -0
- openocr/openrec/modeling/common.py +238 -0
- openocr/openrec/modeling/decoders/__init__.py +109 -0
- openocr/openrec/modeling/decoders/abinet_decoder.py +283 -0
- openocr/openrec/modeling/decoders/aster_decoder.py +170 -0
- openocr/openrec/modeling/decoders/bus_decoder.py +133 -0
- openocr/openrec/modeling/decoders/cam_decoder.py +43 -0
- openocr/openrec/modeling/decoders/cdistnet_decoder.py +334 -0
- openocr/openrec/modeling/decoders/cppd_decoder.py +393 -0
- openocr/openrec/modeling/decoders/ctc_decoder.py +203 -0
- openocr/openrec/modeling/decoders/dan_decoder.py +203 -0
- openocr/openrec/modeling/decoders/igtr_decoder.py +815 -0
- openocr/openrec/modeling/decoders/lister_decoder.py +535 -0
- openocr/openrec/modeling/decoders/lpv_decoder.py +119 -0
- openocr/openrec/modeling/decoders/matrn_decoder.py +236 -0
- openocr/openrec/modeling/decoders/mgp_decoder.py +99 -0
- openocr/openrec/modeling/decoders/nrtr_decoder.py +439 -0
- openocr/openrec/modeling/decoders/ote_decoder.py +205 -0
- openocr/openrec/modeling/decoders/parseq_decoder.py +504 -0
- openocr/openrec/modeling/decoders/rctc_decoder.py +70 -0
- openocr/openrec/modeling/decoders/robustscanner_decoder.py +749 -0
- openocr/openrec/modeling/decoders/sar_decoder.py +236 -0
- openocr/openrec/modeling/decoders/smtr_decoder.py +621 -0
- openocr/openrec/modeling/decoders/smtr_decoder_nattn.py +521 -0
- openocr/openrec/modeling/decoders/srn_decoder.py +283 -0
- openocr/openrec/modeling/decoders/visionlan_decoder.py +321 -0
- openocr/openrec/modeling/encoders/__init__.py +39 -0
- openocr/openrec/modeling/encoders/autostr_encoder.py +327 -0
- openocr/openrec/modeling/encoders/cam_encoder.py +760 -0
- openocr/openrec/modeling/encoders/convnextv2.py +213 -0
- openocr/openrec/modeling/encoders/focalsvtr.py +631 -0
- openocr/openrec/modeling/encoders/nrtr_encoder.py +28 -0
- openocr/openrec/modeling/encoders/rec_hgnet.py +346 -0
- openocr/openrec/modeling/encoders/rec_lcnetv3.py +488 -0
- openocr/openrec/modeling/encoders/rec_mobilenet_v3.py +132 -0
- openocr/openrec/modeling/encoders/rec_mv1_enhance.py +254 -0
- openocr/openrec/modeling/encoders/rec_nrtr_mtb.py +37 -0
- openocr/openrec/modeling/encoders/rec_resnet_31.py +213 -0
- openocr/openrec/modeling/encoders/rec_resnet_45.py +183 -0
- openocr/openrec/modeling/encoders/rec_resnet_fpn.py +216 -0
- openocr/openrec/modeling/encoders/rec_resnet_vd.py +252 -0
- openocr/openrec/modeling/encoders/repvit.py +338 -0
- openocr/openrec/modeling/encoders/resnet31_rnn.py +123 -0
- openocr/openrec/modeling/encoders/svtrnet.py +574 -0
- openocr/openrec/modeling/encoders/svtrnet2dpos.py +616 -0
- openocr/openrec/modeling/encoders/svtrv2.py +470 -0
- openocr/openrec/modeling/encoders/svtrv2_lnconv.py +503 -0
- openocr/openrec/modeling/encoders/svtrv2_lnconv_two33.py +517 -0
- openocr/openrec/modeling/encoders/vit.py +120 -0
- openocr/openrec/modeling/transforms/__init__.py +15 -0
- openocr/openrec/modeling/transforms/aster_tps.py +262 -0
- openocr/openrec/modeling/transforms/moran.py +136 -0
- openocr/openrec/modeling/transforms/tps.py +246 -0
- openocr/openrec/optimizer/__init__.py +73 -0
- openocr/openrec/optimizer/lr.py +227 -0
- openocr/openrec/postprocess/__init__.py +72 -0
- openocr/openrec/postprocess/abinet_postprocess.py +37 -0
- openocr/openrec/postprocess/ar_postprocess.py +63 -0
- openocr/openrec/postprocess/ce_postprocess.py +43 -0
- openocr/openrec/postprocess/char_postprocess.py +108 -0
- openocr/openrec/postprocess/cppd_postprocess.py +42 -0
- openocr/openrec/postprocess/ctc_postprocess.py +119 -0
- openocr/openrec/postprocess/igtr_postprocess.py +100 -0
- openocr/openrec/postprocess/lister_postprocess.py +59 -0
- openocr/openrec/postprocess/mgp_postprocess.py +143 -0
- openocr/openrec/postprocess/nrtr_postprocess.py +75 -0
- openocr/openrec/postprocess/smtr_postprocess.py +73 -0
- openocr/openrec/postprocess/srn_postprocess.py +80 -0
- openocr/openrec/postprocess/visionlan_postprocess.py +81 -0
- openocr/openrec/preprocess/__init__.py +173 -0
- openocr/openrec/preprocess/abinet_aug.py +473 -0
- openocr/openrec/preprocess/abinet_label_encode.py +36 -0
- openocr/openrec/preprocess/ar_label_encode.py +36 -0
- openocr/openrec/preprocess/auto_augment.py +1012 -0
- openocr/openrec/preprocess/cam_label_encode.py +141 -0
- openocr/openrec/preprocess/ce_label_encode.py +116 -0
- openocr/openrec/preprocess/char_label_encode.py +36 -0
- openocr/openrec/preprocess/cppd_label_encode.py +173 -0
- openocr/openrec/preprocess/ctc_label_encode.py +124 -0
- openocr/openrec/preprocess/ep_label_encode.py +38 -0
- openocr/openrec/preprocess/igtr_label_encode.py +360 -0
- openocr/openrec/preprocess/mgp_label_encode.py +95 -0
- openocr/openrec/preprocess/parseq_aug.py +150 -0
- openocr/openrec/preprocess/rec_aug.py +211 -0
- openocr/openrec/preprocess/resize.py +534 -0
- openocr/openrec/preprocess/smtr_label_encode.py +125 -0
- openocr/openrec/preprocess/srn_label_encode.py +37 -0
- openocr/openrec/preprocess/visionlan_label_encode.py +67 -0
- openocr/tools/create_lmdb_dataset.py +118 -0
- openocr/tools/data/__init__.py +94 -0
- openocr/tools/data/collate_fn.py +100 -0
- openocr/tools/data/lmdb_dataset.py +142 -0
- openocr/tools/data/lmdb_dataset_test.py +166 -0
- openocr/tools/data/multi_scale_sampler.py +177 -0
- openocr/tools/data/ratio_dataset.py +217 -0
- openocr/tools/data/ratio_dataset_test.py +273 -0
- openocr/tools/data/ratio_dataset_tvresize.py +213 -0
- openocr/tools/data/ratio_dataset_tvresize_test.py +276 -0
- openocr/tools/data/ratio_sampler.py +190 -0
- openocr/tools/data/simple_dataset.py +263 -0
- openocr/tools/data/strlmdb_dataset.py +143 -0
- openocr/tools/engine/__init__.py +5 -0
- openocr/tools/engine/config.py +158 -0
- openocr/tools/engine/trainer.py +621 -0
- openocr/tools/eval_rec.py +41 -0
- openocr/tools/eval_rec_all_ch.py +184 -0
- openocr/tools/eval_rec_all_en.py +206 -0
- openocr/tools/eval_rec_all_long.py +119 -0
- openocr/tools/eval_rec_all_long_simple.py +122 -0
- openocr/tools/export_rec.py +118 -0
- openocr/tools/infer/onnx_engine.py +65 -0
- openocr/tools/infer/predict_rec.py +140 -0
- openocr/tools/infer/utility.py +234 -0
- openocr/tools/infer_det.py +449 -0
- openocr/tools/infer_e2e.py +462 -0
- openocr/tools/infer_e2e_parallel.py +184 -0
- openocr/tools/infer_rec.py +371 -0
- openocr/tools/train_rec.py +37 -0
- openocr/tools/utility.py +45 -0
- openocr/tools/utils/EN_symbol_dict.txt +94 -0
- openocr/tools/utils/__init__.py +0 -0
- openocr/tools/utils/ckpt.py +87 -0
- openocr/tools/utils/dict/ar_dict.txt +117 -0
- openocr/tools/utils/dict/arabic_dict.txt +161 -0
- openocr/tools/utils/dict/be_dict.txt +145 -0
- openocr/tools/utils/dict/bg_dict.txt +140 -0
- openocr/tools/utils/dict/chinese_cht_dict.txt +8421 -0
- openocr/tools/utils/dict/cyrillic_dict.txt +163 -0
- openocr/tools/utils/dict/devanagari_dict.txt +167 -0
- openocr/tools/utils/dict/en_dict.txt +63 -0
- openocr/tools/utils/dict/fa_dict.txt +136 -0
- openocr/tools/utils/dict/french_dict.txt +136 -0
- openocr/tools/utils/dict/german_dict.txt +143 -0
- openocr/tools/utils/dict/hi_dict.txt +162 -0
- openocr/tools/utils/dict/it_dict.txt +118 -0
- openocr/tools/utils/dict/japan_dict.txt +4399 -0
- openocr/tools/utils/dict/ka_dict.txt +153 -0
- openocr/tools/utils/dict/kie_dict/xfund_class_list.txt +4 -0
- openocr/tools/utils/dict/korean_dict.txt +3688 -0
- openocr/tools/utils/dict/latex_symbol_dict.txt +111 -0
- openocr/tools/utils/dict/latin_dict.txt +185 -0
- openocr/tools/utils/dict/layout_dict/layout_cdla_dict.txt +10 -0
- openocr/tools/utils/dict/layout_dict/layout_publaynet_dict.txt +5 -0
- openocr/tools/utils/dict/layout_dict/layout_table_dict.txt +1 -0
- openocr/tools/utils/dict/mr_dict.txt +153 -0
- openocr/tools/utils/dict/ne_dict.txt +153 -0
- openocr/tools/utils/dict/oc_dict.txt +96 -0
- openocr/tools/utils/dict/pu_dict.txt +130 -0
- openocr/tools/utils/dict/rs_dict.txt +91 -0
- openocr/tools/utils/dict/rsc_dict.txt +134 -0
- openocr/tools/utils/dict/ru_dict.txt +125 -0
- openocr/tools/utils/dict/spin_dict.txt +68 -0
- openocr/tools/utils/dict/ta_dict.txt +128 -0
- openocr/tools/utils/dict/table_dict.txt +277 -0
- openocr/tools/utils/dict/table_master_structure_dict.txt +39 -0
- openocr/tools/utils/dict/table_structure_dict.txt +28 -0
- openocr/tools/utils/dict/table_structure_dict_ch.txt +48 -0
- openocr/tools/utils/dict/te_dict.txt +151 -0
- openocr/tools/utils/dict/ug_dict.txt +114 -0
- openocr/tools/utils/dict/uk_dict.txt +142 -0
- openocr/tools/utils/dict/ur_dict.txt +137 -0
- openocr/tools/utils/dict/xi_dict.txt +110 -0
- openocr/tools/utils/dict90.txt +90 -0
- openocr/tools/utils/e2e_metric/Deteval.py +802 -0
- openocr/tools/utils/e2e_metric/polygon_fast.py +70 -0
- openocr/tools/utils/e2e_utils/extract_batchsize.py +86 -0
- openocr/tools/utils/e2e_utils/extract_textpoint_fast.py +479 -0
- openocr/tools/utils/e2e_utils/extract_textpoint_slow.py +582 -0
- openocr/tools/utils/e2e_utils/pgnet_pp_utils.py +159 -0
- openocr/tools/utils/e2e_utils/visual.py +152 -0
- openocr/tools/utils/en_dict.txt +95 -0
- openocr/tools/utils/gen_label.py +68 -0
- openocr/tools/utils/ic15_dict.txt +36 -0
- openocr/tools/utils/logging.py +56 -0
- openocr/tools/utils/poly_nms.py +132 -0
- openocr/tools/utils/ppocr_keys_v1.txt +6623 -0
- openocr/tools/utils/stats.py +58 -0
- openocr/tools/utils/utility.py +165 -0
- openocr/tools/utils/visual.py +117 -0
- openocr_python-0.0.2.dist-info/LICENCE +201 -0
- openocr_python-0.0.2.dist-info/METADATA +98 -0
- openocr_python-0.0.2.dist-info/RECORD +323 -0
- openocr_python-0.0.2.dist-info/WHEEL +5 -0
- openocr_python-0.0.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import cv2
|
|
3
|
+
from .ar_label_encode import ARLabelEncode
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def crop_safe(arr, rect, bbs=[], pad=0):
|
|
7
|
+
rect = np.array(rect)
|
|
8
|
+
rect[:2] -= pad
|
|
9
|
+
rect[2:] += 2 * pad
|
|
10
|
+
v0 = [max(0, rect[0]), max(0, rect[1])]
|
|
11
|
+
v1 = [
|
|
12
|
+
min(arr.shape[0], rect[0] + rect[2]),
|
|
13
|
+
min(arr.shape[1], rect[1] + rect[3])
|
|
14
|
+
]
|
|
15
|
+
arr = arr[v0[0]:v1[0], v0[1]:v1[1], ...]
|
|
16
|
+
if len(bbs) > 0:
|
|
17
|
+
for i in range(len(bbs)):
|
|
18
|
+
bbs[i, 0] -= v0[0]
|
|
19
|
+
bbs[i, 1] -= v0[1]
|
|
20
|
+
return arr, bbs
|
|
21
|
+
else:
|
|
22
|
+
return arr
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
try:
|
|
26
|
+
# pygame==2.5.2
|
|
27
|
+
import pygame
|
|
28
|
+
from pygame import freetype
|
|
29
|
+
except:
|
|
30
|
+
pass
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class CAMLabelEncode(ARLabelEncode):
|
|
34
|
+
"""Convert between text-label and text-index."""
|
|
35
|
+
|
|
36
|
+
def __init__(self,
|
|
37
|
+
max_text_length,
|
|
38
|
+
character_dict_path=None,
|
|
39
|
+
use_space_char=False,
|
|
40
|
+
font_path=None,
|
|
41
|
+
font_size=30,
|
|
42
|
+
font_strength=0.1,
|
|
43
|
+
image_shape=[32, 128],
|
|
44
|
+
**kwargs):
|
|
45
|
+
super(CAMLabelEncode,
|
|
46
|
+
self).__init__(max_text_length, character_dict_path,
|
|
47
|
+
use_space_char)
|
|
48
|
+
self.image_shape = image_shape
|
|
49
|
+
|
|
50
|
+
if font_path is not None:
|
|
51
|
+
freetype.init()
|
|
52
|
+
# init font
|
|
53
|
+
self.font = freetype.Font(font_path)
|
|
54
|
+
self.font.antialiased = True
|
|
55
|
+
self.font.origin = True
|
|
56
|
+
|
|
57
|
+
# choose font style
|
|
58
|
+
self.font.size = font_size
|
|
59
|
+
self.font.underline = False
|
|
60
|
+
|
|
61
|
+
self.font.strong = True
|
|
62
|
+
self.font.strength = font_strength
|
|
63
|
+
self.font.oblique = False
|
|
64
|
+
|
|
65
|
+
def render_normal(self, font, text):
|
|
66
|
+
# get the number of lines
|
|
67
|
+
lines = text.split('\n')
|
|
68
|
+
lengths = [len(l) for l in lines]
|
|
69
|
+
|
|
70
|
+
# font parameters:
|
|
71
|
+
line_spacing = font.get_sized_height() + 1
|
|
72
|
+
|
|
73
|
+
# initialize the surface to proper size:
|
|
74
|
+
line_bounds = font.get_rect(lines[np.argmax(lengths)])
|
|
75
|
+
fsize = (round(2.0 * line_bounds.width),
|
|
76
|
+
round(1.25 * line_spacing * len(lines)))
|
|
77
|
+
surf = pygame.Surface(fsize, pygame.locals.SRCALPHA, 32)
|
|
78
|
+
|
|
79
|
+
bbs = []
|
|
80
|
+
space = font.get_rect('O')
|
|
81
|
+
# space = font.get_rect(' ')
|
|
82
|
+
x, y = 0, 0
|
|
83
|
+
for l in lines:
|
|
84
|
+
x = 2 # carriage-return
|
|
85
|
+
y += line_spacing # line-feed
|
|
86
|
+
|
|
87
|
+
for ch in l: # render each character
|
|
88
|
+
if ch.isspace(): # just shift
|
|
89
|
+
x += space.width
|
|
90
|
+
else:
|
|
91
|
+
# render the character
|
|
92
|
+
ch_bounds = font.render_to(surf, (x, y), ch)
|
|
93
|
+
# ch_bounds.x = x + ch_bounds.x
|
|
94
|
+
# ch_bounds.y = y - ch_bounds.y
|
|
95
|
+
x += ch_bounds.width + 5
|
|
96
|
+
bbs.append(np.array(ch_bounds))
|
|
97
|
+
|
|
98
|
+
# get the union of characters for cropping:
|
|
99
|
+
r0 = pygame.Rect(bbs[0])
|
|
100
|
+
rect_union = r0.unionall(bbs)
|
|
101
|
+
|
|
102
|
+
# get the words:
|
|
103
|
+
# words = ' '.join(text.split())
|
|
104
|
+
|
|
105
|
+
# crop the surface to fit the text:
|
|
106
|
+
bbs = np.array(bbs)
|
|
107
|
+
surf_arr, bbs = crop_safe(pygame.surfarray.pixels_alpha(surf),
|
|
108
|
+
rect_union,
|
|
109
|
+
bbs,
|
|
110
|
+
pad=5)
|
|
111
|
+
surf_arr = surf_arr.swapaxes(0, 1)
|
|
112
|
+
|
|
113
|
+
# self.visualize_bb(surf_arr,bbs)
|
|
114
|
+
return surf_arr, bbs
|
|
115
|
+
|
|
116
|
+
def __call__(self, data):
|
|
117
|
+
data = super().__call__(data=data)
|
|
118
|
+
if data is None:
|
|
119
|
+
return None
|
|
120
|
+
word = []
|
|
121
|
+
for c in data['label'][1:data['length'] + 1]:
|
|
122
|
+
word.append(self.character[c])
|
|
123
|
+
word = ''.join(word)
|
|
124
|
+
# binary mask
|
|
125
|
+
binary_mask, bbs = self.render_normal(self.font, word)
|
|
126
|
+
cate_aware_surf = np.zeros((binary_mask.shape[0], binary_mask.shape[1],
|
|
127
|
+
len(self.character) - 3)).astype(np.uint8)
|
|
128
|
+
for id, bb in zip(data['label'][1:data['length'] + 1], bbs):
|
|
129
|
+
char_id = id - 1
|
|
130
|
+
cate_aware_surf[:, :,
|
|
131
|
+
char_id][bb[1]:bb[1] + bb[3], bb[0]:bb[0] +
|
|
132
|
+
bb[2]] = binary_mask[bb[1]:bb[1] + bb[3],
|
|
133
|
+
bb[0]:bb[0] + bb[2]]
|
|
134
|
+
binary_mask = cate_aware_surf
|
|
135
|
+
binary_mask = cv2.resize(
|
|
136
|
+
binary_mask, (self.image_shape[0] // 2, self.image_shape[1] // 2))
|
|
137
|
+
if np.max(binary_mask) > 0:
|
|
138
|
+
binary_mask = binary_mask / np.max(binary_mask) # [0 ~ 1]
|
|
139
|
+
binary_mask = binary_mask.astype(np.float32)
|
|
140
|
+
data['binary_mask'] = binary_mask
|
|
141
|
+
return data
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
from tools.utils.logging import get_logger
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class BaseRecLabelEncode(object):
|
|
9
|
+
"""Convert between text-label and text-index."""
|
|
10
|
+
|
|
11
|
+
def __init__(
|
|
12
|
+
self,
|
|
13
|
+
max_text_length,
|
|
14
|
+
character_dict_path=None,
|
|
15
|
+
use_space_char=False,
|
|
16
|
+
lower=False,
|
|
17
|
+
):
|
|
18
|
+
self.max_text_len = max_text_length
|
|
19
|
+
self.beg_str = 'sos'
|
|
20
|
+
self.end_str = 'eos'
|
|
21
|
+
self.lower = lower
|
|
22
|
+
self.reverse = False
|
|
23
|
+
if character_dict_path is None:
|
|
24
|
+
logger = get_logger()
|
|
25
|
+
logger.warning(
|
|
26
|
+
'The character_dict_path is None, model can only recognize number and lower letters'
|
|
27
|
+
)
|
|
28
|
+
self.character_str = '0123456789abcdefghijklmnopqrstuvwxyz'
|
|
29
|
+
dict_character = list(self.character_str)
|
|
30
|
+
self.lower = True
|
|
31
|
+
else:
|
|
32
|
+
self.character_str = []
|
|
33
|
+
with open(character_dict_path, 'rb') as fin:
|
|
34
|
+
lines = fin.readlines()
|
|
35
|
+
for line in lines:
|
|
36
|
+
line = line.decode('utf-8').strip('\n').strip('\r\n')
|
|
37
|
+
self.character_str.append(line)
|
|
38
|
+
if use_space_char:
|
|
39
|
+
self.character_str.append(' ')
|
|
40
|
+
dict_character = list(self.character_str)
|
|
41
|
+
if 'arabic' in character_dict_path:
|
|
42
|
+
self.reverse = True
|
|
43
|
+
dict_character = self.add_special_char(dict_character)
|
|
44
|
+
self.dict = {}
|
|
45
|
+
for i, char in enumerate(dict_character):
|
|
46
|
+
self.dict[char] = i
|
|
47
|
+
self.character = dict_character
|
|
48
|
+
|
|
49
|
+
def label_reverse(self, text):
|
|
50
|
+
text_re = []
|
|
51
|
+
c_current = ''
|
|
52
|
+
for c in text:
|
|
53
|
+
if not bool(re.search('[a-zA-Z0-9 :*./%+-١٢٣٤٥٦٧٨٩٠]', c)):
|
|
54
|
+
if c_current != '':
|
|
55
|
+
text_re.append(c_current)
|
|
56
|
+
text_re.append(c)
|
|
57
|
+
c_current = ''
|
|
58
|
+
else:
|
|
59
|
+
c_current += c
|
|
60
|
+
if c_current != '':
|
|
61
|
+
text_re.append(c_current)
|
|
62
|
+
|
|
63
|
+
return ''.join(text_re[::-1])
|
|
64
|
+
|
|
65
|
+
def add_special_char(self, dict_character):
|
|
66
|
+
return dict_character
|
|
67
|
+
|
|
68
|
+
def encode(self, text):
|
|
69
|
+
"""convert text-label into text-index.
|
|
70
|
+
input:
|
|
71
|
+
text: text labels of each image. [batch_size]
|
|
72
|
+
|
|
73
|
+
output:
|
|
74
|
+
text: concatenated text index for CTCLoss.
|
|
75
|
+
[sum(text_lengths)] = [text_index_0 + text_index_1 + ... + text_index_(n - 1)]
|
|
76
|
+
length: length of each text. [batch_size]
|
|
77
|
+
"""
|
|
78
|
+
if len(text) == 0 or len(text) > self.max_text_len:
|
|
79
|
+
return None
|
|
80
|
+
if self.lower:
|
|
81
|
+
text = text.lower()
|
|
82
|
+
text_list = []
|
|
83
|
+
for char in text:
|
|
84
|
+
if char not in self.dict:
|
|
85
|
+
# logger = get_logger()
|
|
86
|
+
# logger.warning('{} is not in dict'.format(char))
|
|
87
|
+
continue
|
|
88
|
+
text_list.append(self.dict[char])
|
|
89
|
+
if len(text_list) == 0:
|
|
90
|
+
return None
|
|
91
|
+
return text_list
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class CELabelEncode(BaseRecLabelEncode):
|
|
95
|
+
"""Convert between text-label and text-index."""
|
|
96
|
+
|
|
97
|
+
def __init__(self,
|
|
98
|
+
max_text_length,
|
|
99
|
+
character_dict_path=None,
|
|
100
|
+
use_space_char=False,
|
|
101
|
+
**kwargs):
|
|
102
|
+
super(CELabelEncode,
|
|
103
|
+
self).__init__(max_text_length, character_dict_path,
|
|
104
|
+
use_space_char)
|
|
105
|
+
|
|
106
|
+
def __call__(self, data):
|
|
107
|
+
text = data['label']
|
|
108
|
+
text = self.encode(text)
|
|
109
|
+
if text is None:
|
|
110
|
+
return None
|
|
111
|
+
data['length'] = np.array(len(text))
|
|
112
|
+
data['label'] = np.array(text)
|
|
113
|
+
return data
|
|
114
|
+
|
|
115
|
+
def add_special_char(self, dict_character):
|
|
116
|
+
return dict_character
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from .ctc_label_encode import BaseRecLabelEncode
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class CharLabelEncode(BaseRecLabelEncode):
|
|
7
|
+
"""Convert between text-label and text-index."""
|
|
8
|
+
|
|
9
|
+
def __init__(self,
|
|
10
|
+
max_text_length,
|
|
11
|
+
character_dict_path=None,
|
|
12
|
+
use_space_char=False,
|
|
13
|
+
**kwargs):
|
|
14
|
+
super(CharLabelEncode,
|
|
15
|
+
self).__init__(max_text_length, character_dict_path,
|
|
16
|
+
use_space_char)
|
|
17
|
+
|
|
18
|
+
def __call__(self, data):
|
|
19
|
+
text = data['label']
|
|
20
|
+
text = self.encode(text)
|
|
21
|
+
if text is None:
|
|
22
|
+
return None
|
|
23
|
+
if len(text) > self.max_text_len:
|
|
24
|
+
return None
|
|
25
|
+
data['length'] = np.array(len(text))
|
|
26
|
+
text_char = text + [104] * (self.max_text_len + 1 - len(text))
|
|
27
|
+
text.insert(0, 2)
|
|
28
|
+
text.append(3)
|
|
29
|
+
text = text + [0] * (self.max_text_len + 2 - len(text))
|
|
30
|
+
data['label'] = np.array(text)
|
|
31
|
+
data['label_char'] = np.array(text_char)
|
|
32
|
+
return data
|
|
33
|
+
|
|
34
|
+
def add_special_char(self, dict_character):
|
|
35
|
+
dict_character = ['blank', '<unk>', '<s>', '</s>'] + dict_character
|
|
36
|
+
return dict_character
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import random
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
from openrec.preprocess.ctc_label_encode import BaseRecLabelEncode
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class CPPDLabelEncode(BaseRecLabelEncode):
|
|
9
|
+
"""Convert between text-label and text-index."""
|
|
10
|
+
|
|
11
|
+
def __init__(
|
|
12
|
+
self,
|
|
13
|
+
max_text_length,
|
|
14
|
+
character_dict_path=None,
|
|
15
|
+
use_space_char=False,
|
|
16
|
+
ch=False,
|
|
17
|
+
# ch_7000=7000,
|
|
18
|
+
ignore_index=100,
|
|
19
|
+
use_sos=False,
|
|
20
|
+
pos_len=False,
|
|
21
|
+
**kwargs):
|
|
22
|
+
self.use_sos = use_sos
|
|
23
|
+
super(CPPDLabelEncode,
|
|
24
|
+
self).__init__(max_text_length, character_dict_path,
|
|
25
|
+
use_space_char)
|
|
26
|
+
self.ch = ch
|
|
27
|
+
self.ignore_index = ignore_index
|
|
28
|
+
self.pos_len = pos_len
|
|
29
|
+
|
|
30
|
+
def __call__(self, data):
|
|
31
|
+
text = data['label']
|
|
32
|
+
if self.ch:
|
|
33
|
+
text, text_node_index, text_node_num = self.encodech(text)
|
|
34
|
+
if text is None:
|
|
35
|
+
return None
|
|
36
|
+
if len(text) > self.max_text_len:
|
|
37
|
+
return None
|
|
38
|
+
data['length'] = np.array(len(text))
|
|
39
|
+
# text.insert(0, 0)
|
|
40
|
+
if self.pos_len:
|
|
41
|
+
text_pos_node = [i_ for i_ in range(len(text), -1, -1)
|
|
42
|
+
] + [100] * (self.max_text_len - len(text))
|
|
43
|
+
else:
|
|
44
|
+
text_pos_node = [1] * (len(text) + 1) + [0] * (
|
|
45
|
+
self.max_text_len - len(text))
|
|
46
|
+
|
|
47
|
+
text.append(0)
|
|
48
|
+
text + [0] * (self.max_text_len - len(text))
|
|
49
|
+
|
|
50
|
+
text = text + [self.ignore_index
|
|
51
|
+
] * (self.max_text_len + 1 - len(text))
|
|
52
|
+
|
|
53
|
+
data['label'] = np.array(text)
|
|
54
|
+
data['label_node'] = np.array(text_node_num + text_pos_node)
|
|
55
|
+
data['label_index'] = np.array(text_node_index)
|
|
56
|
+
# data['label_ctc'] = np.array(ctc_text)
|
|
57
|
+
return data
|
|
58
|
+
else:
|
|
59
|
+
text, text_char_node, ch_order = self.encode(text)
|
|
60
|
+
|
|
61
|
+
if text is None:
|
|
62
|
+
return None
|
|
63
|
+
if len(text) > self.max_text_len:
|
|
64
|
+
return None
|
|
65
|
+
data['length'] = np.array(len(text))
|
|
66
|
+
# text.insert(0, 0)
|
|
67
|
+
if self.pos_len:
|
|
68
|
+
text_pos_node = [i_ for i_ in range(len(text), -1, -1)
|
|
69
|
+
] + [100] * (self.max_text_len - len(text))
|
|
70
|
+
else:
|
|
71
|
+
text_pos_node = [1] * (len(text) + 1) + [0] * (
|
|
72
|
+
self.max_text_len - len(text))
|
|
73
|
+
|
|
74
|
+
text.append(0)
|
|
75
|
+
|
|
76
|
+
text = text + [self.ignore_index
|
|
77
|
+
] * (self.max_text_len + 1 - len(text))
|
|
78
|
+
data['label'] = np.array(text)
|
|
79
|
+
data['label_node'] = np.array(text_char_node + text_pos_node)
|
|
80
|
+
data['label_order'] = np.array(ch_order)
|
|
81
|
+
|
|
82
|
+
return data
|
|
83
|
+
|
|
84
|
+
def add_special_char(self, dict_character):
|
|
85
|
+
if self.use_sos:
|
|
86
|
+
dict_character = ['<s>', '</s>'] + dict_character
|
|
87
|
+
else:
|
|
88
|
+
dict_character = ['</s>'] + dict_character
|
|
89
|
+
self.num_character = len(dict_character)
|
|
90
|
+
|
|
91
|
+
return dict_character
|
|
92
|
+
|
|
93
|
+
def encode(self, text):
|
|
94
|
+
"""convert text-label into text-index.
|
|
95
|
+
input:
|
|
96
|
+
text: text labels of each image. [batch_size]
|
|
97
|
+
|
|
98
|
+
output:
|
|
99
|
+
text: concatenated text index for CTCLoss.
|
|
100
|
+
[sum(text_lengths)] = [text_index_0 + text_index_1 + ... + text_index_(n - 1)]
|
|
101
|
+
length: length of each text. [batch_size]
|
|
102
|
+
"""
|
|
103
|
+
if len(text) == 0:
|
|
104
|
+
return None, None, None
|
|
105
|
+
if self.lower:
|
|
106
|
+
text = text.lower()
|
|
107
|
+
text_node = [0 for _ in range(self.num_character)]
|
|
108
|
+
text_node[0] = 1
|
|
109
|
+
text_list = []
|
|
110
|
+
ch_order = []
|
|
111
|
+
order = 1
|
|
112
|
+
for char in text:
|
|
113
|
+
if char not in self.dict:
|
|
114
|
+
continue
|
|
115
|
+
text_list.append(self.dict[char])
|
|
116
|
+
text_node[self.dict[char]] += 1
|
|
117
|
+
ch_order.append(
|
|
118
|
+
[self.dict[char], text_node[self.dict[char]], order])
|
|
119
|
+
order += 1
|
|
120
|
+
|
|
121
|
+
no_ch_order = []
|
|
122
|
+
for char in self.character:
|
|
123
|
+
if char not in text:
|
|
124
|
+
no_ch_order.append([self.dict[char], 1, 0])
|
|
125
|
+
random.shuffle(no_ch_order)
|
|
126
|
+
ch_order = ch_order + no_ch_order
|
|
127
|
+
ch_order = ch_order[:self.max_text_len + 1]
|
|
128
|
+
|
|
129
|
+
if len(text_list) == 0 or len(text_list) > self.max_text_len:
|
|
130
|
+
return None, None, None
|
|
131
|
+
return text_list, text_node, ch_order.sort()
|
|
132
|
+
|
|
133
|
+
def encodech(self, text):
|
|
134
|
+
"""convert text-label into text-index.
|
|
135
|
+
input:
|
|
136
|
+
text: text labels of each image. [batch_size]
|
|
137
|
+
|
|
138
|
+
output:
|
|
139
|
+
text: concatenated text index for CTCLoss.
|
|
140
|
+
[sum(text_lengths)] = [text_index_0 + text_index_1 + ... + text_index_(n - 1)]
|
|
141
|
+
length: length of each text. [batch_size]
|
|
142
|
+
"""
|
|
143
|
+
if len(text) == 0:
|
|
144
|
+
return None, None, None
|
|
145
|
+
if self.lower:
|
|
146
|
+
text = text.lower()
|
|
147
|
+
text_node_dict = {}
|
|
148
|
+
text_node_dict.update({0: 1})
|
|
149
|
+
character_index = [_ for _ in range(self.num_character)]
|
|
150
|
+
text_list = []
|
|
151
|
+
for char in text:
|
|
152
|
+
if char not in self.dict:
|
|
153
|
+
continue
|
|
154
|
+
i_c = self.dict[char]
|
|
155
|
+
text_list.append(i_c)
|
|
156
|
+
|
|
157
|
+
if i_c in text_node_dict.keys():
|
|
158
|
+
text_node_dict[i_c] += 1
|
|
159
|
+
else:
|
|
160
|
+
text_node_dict.update({i_c: 1})
|
|
161
|
+
for ic in list(text_node_dict.keys()):
|
|
162
|
+
character_index.remove(ic)
|
|
163
|
+
none_char_index = random.sample(character_index,
|
|
164
|
+
37 - len(list(text_node_dict.keys())))
|
|
165
|
+
for ic in none_char_index:
|
|
166
|
+
text_node_dict[ic] = 0
|
|
167
|
+
|
|
168
|
+
text_node_index = sorted(text_node_dict)
|
|
169
|
+
|
|
170
|
+
text_node_num = [text_node_dict[k] for k in text_node_index]
|
|
171
|
+
if len(text_list) == 0 or len(text_list) > self.max_text_len:
|
|
172
|
+
return None, None, None
|
|
173
|
+
return text_list, text_node_index, text_node_num
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
from tools.utils.logging import get_logger
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class BaseRecLabelEncode(object):
|
|
9
|
+
"""Convert between text-label and text-index."""
|
|
10
|
+
|
|
11
|
+
def __init__(
|
|
12
|
+
self,
|
|
13
|
+
max_text_length,
|
|
14
|
+
character_dict_path=None,
|
|
15
|
+
use_space_char=False,
|
|
16
|
+
lower=False,
|
|
17
|
+
):
|
|
18
|
+
self.max_text_len = max_text_length
|
|
19
|
+
self.beg_str = 'sos'
|
|
20
|
+
self.end_str = 'eos'
|
|
21
|
+
self.lower = lower
|
|
22
|
+
self.reverse = False
|
|
23
|
+
if character_dict_path is None:
|
|
24
|
+
logger = get_logger()
|
|
25
|
+
logger.warning(
|
|
26
|
+
'The character_dict_path is None, model can only recognize number and lower letters'
|
|
27
|
+
)
|
|
28
|
+
self.character_str = '0123456789abcdefghijklmnopqrstuvwxyz'
|
|
29
|
+
dict_character = list(self.character_str)
|
|
30
|
+
self.lower = True
|
|
31
|
+
else:
|
|
32
|
+
self.character_str = []
|
|
33
|
+
with open(character_dict_path, 'rb') as fin:
|
|
34
|
+
lines = fin.readlines()
|
|
35
|
+
for line in lines:
|
|
36
|
+
line = line.decode('utf-8').strip('\n').strip('\r\n')
|
|
37
|
+
self.character_str.append(line)
|
|
38
|
+
if use_space_char:
|
|
39
|
+
self.character_str.append(' ')
|
|
40
|
+
dict_character = list(self.character_str)
|
|
41
|
+
if 'arabic' in character_dict_path:
|
|
42
|
+
self.reverse = True
|
|
43
|
+
dict_character = self.add_special_char(dict_character)
|
|
44
|
+
self.dict = {}
|
|
45
|
+
for i, char in enumerate(dict_character):
|
|
46
|
+
self.dict[char] = i
|
|
47
|
+
self.character = dict_character
|
|
48
|
+
|
|
49
|
+
def label_reverse(self, text):
|
|
50
|
+
text_re = []
|
|
51
|
+
c_current = ''
|
|
52
|
+
for c in text:
|
|
53
|
+
if not bool(re.search('[a-zA-Z0-9 :*./%+-١٢٣٤٥٦٧٨٩٠]', c)):
|
|
54
|
+
if c_current != '':
|
|
55
|
+
text_re.append(c_current)
|
|
56
|
+
text_re.append(c)
|
|
57
|
+
c_current = ''
|
|
58
|
+
else:
|
|
59
|
+
c_current += c
|
|
60
|
+
if c_current != '':
|
|
61
|
+
text_re.append(c_current)
|
|
62
|
+
|
|
63
|
+
return ''.join(text_re[::-1])
|
|
64
|
+
|
|
65
|
+
def add_special_char(self, dict_character):
|
|
66
|
+
return dict_character
|
|
67
|
+
|
|
68
|
+
def encode(self, text):
|
|
69
|
+
"""convert text-label into text-index.
|
|
70
|
+
input:
|
|
71
|
+
text: text labels of each image. [batch_size]
|
|
72
|
+
|
|
73
|
+
output:
|
|
74
|
+
text: concatenated text index for CTCLoss.
|
|
75
|
+
[sum(text_lengths)] = [text_index_0 + text_index_1 + ... + text_index_(n - 1)]
|
|
76
|
+
length: length of each text. [batch_size]
|
|
77
|
+
"""
|
|
78
|
+
if len(text) == 0:
|
|
79
|
+
return None
|
|
80
|
+
if self.lower:
|
|
81
|
+
text = text.lower()
|
|
82
|
+
text_list = []
|
|
83
|
+
for char in text:
|
|
84
|
+
if char not in self.dict:
|
|
85
|
+
continue
|
|
86
|
+
text_list.append(self.dict[char])
|
|
87
|
+
if len(text_list) == 0 or len(text_list) > self.max_text_len:
|
|
88
|
+
return None
|
|
89
|
+
return text_list
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class CTCLabelEncode(BaseRecLabelEncode):
|
|
93
|
+
"""Convert between text-label and text-index."""
|
|
94
|
+
|
|
95
|
+
def __init__(self,
|
|
96
|
+
max_text_length,
|
|
97
|
+
character_dict_path=None,
|
|
98
|
+
use_space_char=False,
|
|
99
|
+
**kwargs):
|
|
100
|
+
super(CTCLabelEncode,
|
|
101
|
+
self).__init__(max_text_length, character_dict_path,
|
|
102
|
+
use_space_char)
|
|
103
|
+
self.is_reverse = kwargs.get('is_reverse', False)
|
|
104
|
+
|
|
105
|
+
def __call__(self, data):
|
|
106
|
+
text = data['label']
|
|
107
|
+
if self.reverse and self.is_reverse: # for arabic rec
|
|
108
|
+
text = self.label_reverse(text)
|
|
109
|
+
text = self.encode(text)
|
|
110
|
+
if text is None:
|
|
111
|
+
return None
|
|
112
|
+
data['length'] = np.array(len(text))
|
|
113
|
+
text = text + [0] * (self.max_text_len - len(text))
|
|
114
|
+
data['label'] = np.array(text)
|
|
115
|
+
|
|
116
|
+
label = [0] * len(self.character)
|
|
117
|
+
for x in text:
|
|
118
|
+
label[x] += 1
|
|
119
|
+
data['label_ace'] = np.array(label)
|
|
120
|
+
return data
|
|
121
|
+
|
|
122
|
+
def add_special_char(self, dict_character):
|
|
123
|
+
dict_character = ['blank'] + dict_character
|
|
124
|
+
return dict_character
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from openrec.preprocess.ctc_label_encode import BaseRecLabelEncode
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class EPLabelEncode(BaseRecLabelEncode):
|
|
7
|
+
"""Convert between text-label and text-index."""
|
|
8
|
+
EOS = '</s>'
|
|
9
|
+
PAD = '<pad>'
|
|
10
|
+
|
|
11
|
+
def __init__(self,
|
|
12
|
+
max_text_length,
|
|
13
|
+
character_dict_path=None,
|
|
14
|
+
use_space_char=False,
|
|
15
|
+
**kwargs):
|
|
16
|
+
|
|
17
|
+
super(EPLabelEncode,
|
|
18
|
+
self).__init__(max_text_length, character_dict_path,
|
|
19
|
+
use_space_char)
|
|
20
|
+
|
|
21
|
+
def __call__(self, data):
|
|
22
|
+
text = data['label']
|
|
23
|
+
text = self.encode(text)
|
|
24
|
+
if text is None:
|
|
25
|
+
return None
|
|
26
|
+
if len(text) > self.max_text_len:
|
|
27
|
+
return None
|
|
28
|
+
|
|
29
|
+
data['length'] = np.array(len(text))
|
|
30
|
+
text = text + [self.dict[self.EOS]]
|
|
31
|
+
text = text + [self.dict[self.PAD]
|
|
32
|
+
] * (self.max_text_len + 1 - len(text))
|
|
33
|
+
data['label'] = np.array(text)
|
|
34
|
+
return data
|
|
35
|
+
|
|
36
|
+
def add_special_char(self, dict_character):
|
|
37
|
+
dict_character = [self.EOS] + dict_character + [self.PAD]
|
|
38
|
+
return dict_character
|