├── .gitignore ├── LICENSE ├── README.md ├── curve_text_rectification.py ├── data ├── test_images │ ├── arc_text.jpg │ ├── gt_12.jpg │ ├── gt_23.jpg │ ├── gt_3.jpg │ ├── gt_46.jpg │ ├── gt_49.jpg │ ├── gt_57.jpg │ ├── gt_61.jpg │ ├── gt_63.jpg │ ├── gt_74.jpg │ ├── gt_75.jpg │ ├── gt_81.jpg │ ├── gt_86.jpg │ ├── gt_88.jpg │ ├── gt_91.jpg │ ├── gt_93.jpg │ ├── gt_94.jpg │ ├── gt_95.jpg │ ├── gt_97.jpg │ ├── gt_99.jpg │ └── vincent.jpg ├── test_result │ ├── vis_arc_text.jpg │ ├── vis_gt_12.jpg │ ├── vis_gt_23.jpg │ ├── vis_gt_3.jpg │ ├── vis_gt_46.jpg │ ├── vis_gt_49.jpg │ ├── vis_gt_57.jpg │ ├── vis_gt_61.jpg │ ├── vis_gt_63.jpg │ ├── vis_gt_74.jpg │ ├── vis_gt_75.jpg │ ├── vis_gt_81.jpg │ ├── vis_gt_86.jpg │ ├── vis_gt_88.jpg │ ├── vis_gt_91.jpg │ ├── vis_gt_93.jpg │ ├── vis_gt_94.jpg │ ├── vis_gt_95.jpg │ ├── vis_gt_97.jpg │ ├── vis_gt_99.jpg │ └── vis_vincent.jpg └── test_txt │ ├── arc_text.txt │ ├── gt_12.txt │ ├── gt_23.txt │ ├── gt_3.txt │ ├── gt_46.txt │ ├── gt_49.txt │ ├── gt_57.txt │ ├── gt_61.txt │ ├── gt_63.txt │ ├── gt_74.txt │ ├── gt_75.txt │ ├── gt_81.txt │ ├── gt_86.txt │ ├── gt_88.txt │ ├── gt_91.txt │ ├── gt_93.txt │ ├── gt_94.txt │ ├── gt_95.txt │ ├── gt_97.txt │ ├── gt_99.txt │ └── vincent.txt ├── images ├── OneBoxImageResult.jpg ├── vis_arc_text.jpg ├── vis_gt_46.jpg ├── vis_gt_46_1.jpg ├── vis_gt_46_1_h.jpg ├── vis_gt_46_2.jpg ├── vis_gt_57.jpg ├── vis_gt_57_1.jpg ├── vis_gt_57_2.jpg ├── vis_gt_57_3.jpg ├── vis_gt_61.jpg ├── vis_gt_61_3.jpg ├── vis_gt_61_9.jpg ├── vis_gt_86.jpg ├── vis_gt_86_1.jpg ├── vis_gt_86_13.jpg ├── vis_gt_86_2.jpg ├── vis_gt_86_6.jpg ├── vis_gt_86_9.jpg ├── vis_gt_91.jpg ├── vis_gt_91_1.jpg ├── vis_gt_91_2.jpg ├── vis_gt_91_3.jpg └── vis_gt_91_4.jpg └── test.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | # pycharm 132 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Curve-Text-Rectification-Using-Pairs-Of-Points 2 | 3 | This repository is designed via traditional image processing, not deep learning. 4 | 5 | It's a way to rectify curve text images using spatial transformer by pairs of points. This could be rectify the curve text image after text detection, and helps text recognition easily in OCR tasks. 6 | 7 | This method is based on calibration technology of monocular vision. It depends on pairs of points by detector just like [CRAFT](https://arxiv.org/abs/1904.01941). 8 | 9 | ## Requirements 10 | 11 | - numpy 12 | - opencv-python 13 | 14 | ## Usage 15 | 16 | ``` 17 | python3 test.py --image image_path --txt points_txt_path_corresponding_to_image --mode calibration 18 | ``` 19 | 20 | ``` 21 | python3 test.py --image ./data/test_images/gt_86.jpg --txt ./data/test_txt/gt_86.txt --mode calibration 22 | ``` 23 | 24 | or 25 | 26 | ``` 27 | python3 curve_text_rectification.py --mode homography 28 | ``` 29 | 30 | Results will saved in `results` directory. 31 | 32 | | mode | description | 33 | | ------------- | ------------------------------------------------------------ | 34 | | `calibration` | Monocular Vision: 3-D model and fish-eye camera undistortion. | 35 | | `homography` | Split a curve text region up into several sub-4-points-bounding-box-images, homography transform and stitch. Higher FPS and lower precision | 36 | 37 | ## Interface 38 | 39 | ``` 40 | from curve_text_rectification import AutoRectifier 41 | autoRectifier = AutoRectifier() 42 | 43 | """ 44 | run for texts in an image 45 | :param image_data: numpy.ndarray. The shape is [h, w, 3] 46 | :param points_list: [[x1,y1,x2,y2,x3,y3,...], [x1,y1,x2,y2,x3,y3,...], ...], clockwise order, (x1,y1) must be the top-left of first char. 47 | :param interpolation: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_AREA, cv2.INTER_CUBIC, cv2.INTER_LANCZOS4 48 | :param ratio_width: roi_image width expansion. It should not be smaller than 1.0 49 | :param ratio_height: roi_image height expansion. It should not be smaller than 1.0 50 | :param loss_thresh: if loss greater than loss_thresh --> get_rotate_crop_image 51 | :param mode: 'calibration' or 'homography'. when homography, ratio_width and ratio_height must be 1.0 52 | :return: res: roi-image list, visualized_image: draw polys in original image 53 | """ 54 | res, visualized_image = autoRectifier.run(image_data, points_list, interpolation=cv2.INTER_LINEAR, ratio_width=1.0, ratio_height=1.0, loss_thresh=5.0, mode='calibration') 55 | ``` 56 | 57 | ## Demo 58 | 59 | ### 1 60 | 61 | ![](./images/vis_arc_text.jpg) 62 | 63 | ![](./images/OneBoxImageResult.jpg) 64 | 65 | ### 2 66 | 67 | ![](./images/vis_gt_91.jpg) 68 | 69 | ![](./images/vis_gt_91_1.jpg) 70 | 71 | ![](./images/vis_gt_91_2.jpg) 72 | 73 | ![](./images/vis_gt_91_3.jpg) 74 | 75 | ![](./images/vis_gt_91_4.jpg) 76 | 77 | ### 3 78 | 79 | ![](./images/vis_gt_61.jpg) 80 | 81 | ![](./images/vis_gt_61_3.jpg) 82 | 83 | ![](./images/vis_gt_61_9.jpg) 84 | 85 | ### 4 86 | 87 | ![](./images/vis_gt_86.jpg) 88 | 89 | ![](./images/vis_gt_86_1.jpg) 90 | 91 | ![](./images/vis_gt_86_2.jpg) 92 | 93 | ![](./images/vis_gt_86_6.jpg) 94 | 95 | ![](./images/vis_gt_86_9.jpg) 96 | 97 | ![](./images/vis_gt_86_13.jpg) 98 | 99 | ### 5 100 | 101 | ![](./images/vis_gt_57.jpg) 102 | 103 | ![](./images/vis_gt_57_1.jpg) 104 | 105 | ![](./images/vis_gt_57_2.jpg) 106 | 107 | ![](./images/vis_gt_57_3.jpg) 108 | 109 | ### calibration failure 110 | 111 | ![](./images/vis_gt_46.jpg) 112 | 113 | ![](./images/vis_gt_46_1.jpg) 114 | 115 | #### homography 116 | 117 | ![](./images/vis_gt_46_1_h.jpg) 118 | 119 | ## Reference 120 | 121 | - [Camera Calibration and 3D Reconstruction](https://docs.opencv.org/3.4.12/d9/d0c/group__calib3d.html) 122 | - [Character Region Awareness for Text Detection](https://arxiv.org/abs/1904.01941) 123 | - [CRAFT-pytorch](https://github.com/clovaai/CRAFT-pytorch) 124 | - [A Flexible New Technique for Camera Calibration](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/tr98-71.pdf) 125 | - [Monocular_vision](https://en.wikipedia.org/wiki/Monocular_vision) 126 | - [deep-text-recognition-benchmark](https://arxiv.org/abs/1904.01906) 127 | - [https://rrc.cvc.uab.es/](https://rrc.cvc.uab.es/) 128 | - [Geometric Image Transformations](https://docs.opencv.org/master/da/d54/group__imgproc__transform.html) 129 | -------------------------------------------------------------------------------- /curve_text_rectification.py: -------------------------------------------------------------------------------- 1 | import os, sys 2 | import numpy as np 3 | from numpy import cos, sin, arctan, sqrt 4 | import cv2 5 | import copy 6 | import time 7 | 8 | def Homography(image, img_points, world_width, world_height, 9 | interpolation=cv2.INTER_CUBIC, ratio_width=1.0, ratio_height=1.0): 10 | _points = np.array(img_points).reshape(-1, 2).astype(np.float32) 11 | 12 | expand_x = int(0.5 * world_width * (ratio_width - 1)) 13 | expand_y = int(0.5 * world_height * (ratio_height - 1)) 14 | 15 | pt_lefttop = [expand_x, expand_y] 16 | pt_righttop = [expand_x + world_width, expand_y] 17 | pt_leftbottom = [expand_x + world_width, expand_y + world_height] 18 | pt_rightbottom = [expand_x, expand_y + world_height] 19 | 20 | pts_std = np.float32([pt_lefttop, pt_righttop, 21 | pt_leftbottom, pt_rightbottom]) 22 | 23 | img_crop_width = int(world_width * ratio_width) 24 | img_crop_height = int(world_height * ratio_height) 25 | 26 | M = cv2.getPerspectiveTransform(_points, pts_std) 27 | 28 | dst_img = cv2.warpPerspective( 29 | image, 30 | M, (img_crop_width, img_crop_height), 31 | borderMode=cv2.BORDER_CONSTANT, # BORDER_CONSTANT BORDER_REPLICATE 32 | flags=interpolation) 33 | 34 | return dst_img 35 | 36 | class CurveTextRectifier: 37 | """ 38 | spatial transformer via monocular vision 39 | """ 40 | def __init__(self): 41 | self.get_virtual_camera_parameter() 42 | 43 | 44 | def get_virtual_camera_parameter(self): 45 | vcam_thz = 0 46 | vcam_thx1 = 180 47 | vcam_thy = 180 48 | vcam_thx2 = 0 49 | 50 | vcam_x = 0 51 | vcam_y = 0 52 | vcam_z = 100 53 | 54 | radian = np.pi / 180 55 | 56 | angle_z = radian * vcam_thz 57 | angle_x1 = radian * vcam_thx1 58 | angle_y = radian * vcam_thy 59 | angle_x2 = radian * vcam_thx2 60 | 61 | optic_x = vcam_x 62 | optic_y = vcam_y 63 | optic_z = vcam_z 64 | 65 | fu = 100 66 | fv = 100 67 | 68 | matT = np.zeros((4, 4)) 69 | matT[0, 0] = cos(angle_z) * cos(angle_y) - sin(angle_z) * sin(angle_x1) * sin(angle_y) 70 | matT[0, 1] = cos(angle_z) * sin(angle_y) * sin(angle_x2) - sin(angle_z) * ( 71 | cos(angle_x1) * cos(angle_x2) - sin(angle_x1) * cos(angle_y) * sin(angle_x2)) 72 | matT[0, 2] = cos(angle_z) * sin(angle_y) * cos(angle_x2) + sin(angle_z) * ( 73 | cos(angle_x1) * sin(angle_x2) + sin(angle_x1) * cos(angle_y) * cos(angle_x2)) 74 | matT[0, 3] = optic_x 75 | matT[1, 0] = sin(angle_z) * cos(angle_y) + cos(angle_z) * sin(angle_x1) * sin(angle_y) 76 | matT[1, 1] = sin(angle_z) * sin(angle_y) * sin(angle_x2) + cos(angle_z) * ( 77 | cos(angle_x1) * cos(angle_x2) - sin(angle_x1) * cos(angle_y) * sin(angle_x2)) 78 | matT[1, 2] = sin(angle_z) * sin(angle_y) * cos(angle_x2) - cos(angle_z) * ( 79 | cos(angle_x1) * sin(angle_x2) + sin(angle_x1) * cos(angle_y) * cos(angle_x2)) 80 | matT[1, 3] = optic_y 81 | matT[2, 0] = -cos(angle_x1) * sin(angle_y) 82 | matT[2, 1] = cos(angle_x1) * cos(angle_y) * sin(angle_x2) + sin(angle_x1) * cos(angle_x2) 83 | matT[2, 2] = cos(angle_x1) * cos(angle_y) * cos(angle_x2) - sin(angle_x1) * sin(angle_x2) 84 | matT[2, 3] = optic_z 85 | matT[3, 0] = 0 86 | matT[3, 1] = 0 87 | matT[3, 2] = 0 88 | matT[3, 3] = 1 89 | 90 | matS = np.zeros((4, 4)) 91 | matS[2, 3] = 0.5 92 | matS[3, 2] = 0.5 93 | 94 | self.ifu = 1 / fu 95 | self.ifv = 1 / fv 96 | 97 | self.matT = matT 98 | self.matS = matS 99 | self.K = np.dot(matT.T, matS) 100 | self.K = np.dot(self.K, matT) 101 | 102 | 103 | def vertical_text_process(self, points, org_size): 104 | """ 105 | change sequence amd process 106 | :param points: 107 | :param org_size: 108 | :return: 109 | """ 110 | org_w, org_h = org_size 111 | _points = np.array(points).reshape(-1).tolist() 112 | _points = np.array(_points[2:] + _points[:2]).reshape(-1, 2) 113 | 114 | # convert to horizontal points 115 | adjusted_points = np.zeros(_points.shape, dtype=np.float32) 116 | adjusted_points[:, 0] = _points[:, 1] 117 | adjusted_points[:, 1] = org_h - _points[:, 0] - 1 118 | 119 | _image_coord, _world_coord, _new_image_size = self.horizontal_text_process(adjusted_points) 120 | 121 | # # convert to vertical points back 122 | image_coord = _points.reshape(1, -1, 2) 123 | world_coord = np.zeros(_world_coord.shape, dtype=np.float32) 124 | world_coord[:, :, 0] = 0 - _world_coord[:, :, 1] 125 | world_coord[:, :, 1] = _world_coord[:, :, 0] 126 | world_coord[:, :, 2] = _world_coord[:, :, 2] 127 | new_image_size = (_new_image_size[1], _new_image_size[0]) 128 | 129 | return image_coord, world_coord, new_image_size 130 | 131 | 132 | def horizontal_text_process(self, points): 133 | """ 134 | get image coordinate and world coordinate 135 | :param points: 136 | :return: 137 | """ 138 | poly = np.array(points).reshape(-1) 139 | 140 | dx_list = [] 141 | dy_list = [] 142 | for i in range(1, len(poly) // 2): 143 | xdx = poly[i * 2] - poly[(i - 1) * 2] 144 | xdy = poly[i * 2 + 1] - poly[(i - 1) * 2 + 1] 145 | d = sqrt(xdx ** 2 + xdy ** 2) 146 | dx_list.append(d) 147 | 148 | for i in range(0, len(poly) // 4): 149 | ydx = poly[i * 2] - poly[len(poly) - 1 - (i * 2 + 1)] 150 | ydy = poly[i * 2 + 1] - poly[len(poly) - 1 - (i * 2)] 151 | d = sqrt(ydx ** 2 + ydy ** 2) 152 | dy_list.append(d) 153 | 154 | dx_list = [(dx_list[i] + dx_list[len(dx_list) - 1 - i]) / 2 for i in range(len(dx_list) // 2)] 155 | 156 | height = np.around(np.mean(dy_list)) 157 | 158 | rect_coord = [0, 0] 159 | for i in range(0, len(poly) // 4 - 1): 160 | x = rect_coord[-2] 161 | x += dx_list[i] 162 | y = 0 163 | rect_coord.append(x) 164 | rect_coord.append(y) 165 | 166 | rect_coord_half = copy.deepcopy(rect_coord) 167 | for i in range(0, len(poly) // 4): 168 | x = rect_coord_half[len(rect_coord_half) - 2 * i - 2] 169 | y = height 170 | rect_coord.append(x) 171 | rect_coord.append(y) 172 | 173 | np_rect_coord = np.array(rect_coord).reshape(-1, 2) 174 | x_min = np.min(np_rect_coord[:, 0]) 175 | y_min = np.min(np_rect_coord[:, 1]) 176 | x_max = np.max(np_rect_coord[:, 0]) 177 | y_max = np.max(np_rect_coord[:, 1]) 178 | new_image_size = (int(x_max - x_min + 0.5), int(y_max - y_min + 0.5)) 179 | x_mean = (x_max - x_min) / 2 180 | y_mean = (y_max - y_min) / 2 181 | np_rect_coord[:, 0] -= x_mean 182 | np_rect_coord[:, 1] -= y_mean 183 | rect_coord = np_rect_coord.reshape(-1).tolist() 184 | 185 | rect_coord = np.array(rect_coord).reshape(-1, 2) 186 | world_coord = np.ones((len(rect_coord), 3)) * 0 187 | 188 | world_coord[:, :2] = rect_coord 189 | 190 | image_coord = np.array(poly).reshape(1, -1, 2) 191 | world_coord = world_coord.reshape(1, -1, 3) 192 | 193 | return image_coord, world_coord, new_image_size 194 | 195 | 196 | def horizontal_text_estimate(self, points): 197 | """ 198 | horizontal or vertical text 199 | :param points: 200 | :return: 201 | """ 202 | pts = np.array(points).reshape(-1, 2) 203 | x_min = int(np.min(pts[:, 0])) 204 | y_min = int(np.min(pts[:, 1])) 205 | x_max = int(np.max(pts[:, 0])) 206 | y_max = int(np.max(pts[:, 1])) 207 | x = x_max - x_min 208 | y = y_max - y_min 209 | is_horizontal_text = True 210 | if y / x > 1.5: # vertical text condition 211 | is_horizontal_text = False 212 | return is_horizontal_text 213 | 214 | 215 | def virtual_camera_to_world(self, size): 216 | ifu, ifv = self.ifu, self.ifv 217 | K, matT = self.K, self.matT 218 | 219 | ppu = size[0] / 2 + 1e-6 220 | ppv = size[1] / 2 + 1e-6 221 | 222 | P = np.zeros((size[1], size[0], 3)) 223 | 224 | lu = np.array([i for i in range(size[0])]) 225 | lv = np.array([i for i in range(size[1])]) 226 | u, v = np.meshgrid(lu, lv) 227 | 228 | yp = (v - ppv) * ifv 229 | xp = (u - ppu) * ifu 230 | angle_a = arctan(sqrt(xp * xp + yp * yp)) 231 | angle_b = arctan(yp / xp) 232 | 233 | D0 = sin(angle_a) * cos(angle_b) 234 | D1 = sin(angle_a) * sin(angle_b) 235 | D2 = cos(angle_a) 236 | 237 | D0[xp <= 0] = -D0[xp <= 0] 238 | D1[xp <= 0] = -D1[xp <= 0] 239 | 240 | ratio_a = K[0, 0] * D0 * D0 + K[1, 1] * D1 * D1 + K[2, 2] * D2 * D2 + \ 241 | (K[0, 1] + K[1, 0]) * D0 * D1 + (K[0, 2] + K[2, 0]) * D0 * D2 + (K[1, 2] + K[2, 1]) * D1 * D2 242 | ratio_b = (K[0, 3] + K[3, 0]) * D0 + (K[1, 3] + K[3, 1]) * D1 + (K[2, 3] + K[3, 2]) * D2 243 | ratio_c = K[3, 3] * np.ones(ratio_b.shape) 244 | 245 | delta = ratio_b * ratio_b - 4 * ratio_a * ratio_c 246 | t = np.zeros(delta.shape) 247 | t[ratio_a == 0] = -ratio_c[ratio_a == 0] / ratio_b[ratio_a == 0] 248 | t[ratio_a != 0] = (-ratio_b[ratio_a != 0] + sqrt(delta[ratio_a != 0])) / (2 * ratio_a[ratio_a != 0]) 249 | t[delta < 0] = 0 250 | 251 | P[:, :, 0] = matT[0, 3] + t * (matT[0, 0] * D0 + matT[0, 1] * D1 + matT[0, 2] * D2) 252 | P[:, :, 1] = matT[1, 3] + t * (matT[1, 0] * D0 + matT[1, 1] * D1 + matT[1, 2] * D2) 253 | P[:, :, 2] = matT[2, 3] + t * (matT[2, 0] * D0 + matT[2, 1] * D1 + matT[2, 2] * D2) 254 | 255 | return P 256 | 257 | 258 | def world_to_image(self, image_size, world, intrinsic, distCoeffs, rotation, tvec): 259 | r11 = rotation[0, 0] 260 | r12 = rotation[0, 1] 261 | r13 = rotation[0, 2] 262 | r21 = rotation[1, 0] 263 | r22 = rotation[1, 1] 264 | r23 = rotation[1, 2] 265 | r31 = rotation[2, 0] 266 | r32 = rotation[2, 1] 267 | r33 = rotation[2, 2] 268 | 269 | t1 = tvec[0] 270 | t2 = tvec[1] 271 | t3 = tvec[2] 272 | 273 | k1 = distCoeffs[0] 274 | k2 = distCoeffs[1] 275 | p1 = distCoeffs[2] 276 | p2 = distCoeffs[3] 277 | k3 = distCoeffs[4] 278 | k4 = distCoeffs[5] 279 | k5 = distCoeffs[6] 280 | k6 = distCoeffs[7] 281 | 282 | if len(distCoeffs) > 8: 283 | s1 = distCoeffs[8] 284 | s2 = distCoeffs[9] 285 | s3 = distCoeffs[10] 286 | s4 = distCoeffs[11] 287 | else: 288 | s1 = s2 = s3 = s4 = 0 289 | 290 | if len(distCoeffs) > 12: 291 | tx = distCoeffs[12] 292 | ty = distCoeffs[13] 293 | else: 294 | tx = ty = 0 295 | 296 | fu = intrinsic[0, 0] 297 | fv = intrinsic[1, 1] 298 | ppu = intrinsic[0, 2] 299 | ppv = intrinsic[1, 2] 300 | 301 | cos_tx = cos(tx) 302 | cos_ty = cos(ty) 303 | sin_tx = sin(tx) 304 | sin_ty = sin(ty) 305 | 306 | tao11 = cos_ty * cos_tx * cos_ty + sin_ty * cos_tx * sin_ty 307 | tao12 = cos_ty * cos_tx * sin_ty * sin_tx - sin_ty * cos_tx * cos_ty * sin_tx 308 | tao13 = -cos_ty * cos_tx * sin_ty * cos_tx + sin_ty * cos_tx * cos_ty * cos_tx 309 | tao21 = -sin_tx * sin_ty 310 | tao22 = cos_ty * cos_tx * cos_tx + sin_tx * cos_ty * sin_tx 311 | tao23 = cos_ty * cos_tx * sin_tx - sin_tx * cos_ty * cos_tx 312 | 313 | P = np.zeros((image_size[1], image_size[0], 2)) 314 | 315 | c3 = r31 * world[:, :, 0] + r32 * world[:, :, 1] + r33 * world[:, :, 2] + t3 316 | c1 = r11 * world[:, :, 0] + r12 * world[:, :, 1] + r13 * world[:, :, 2] + t1 317 | c2 = r21 * world[:, :, 0] + r22 * world[:, :, 1] + r23 * world[:, :, 2] + t2 318 | 319 | x1 = c1 / c3 320 | y1 = c2 / c3 321 | x12 = x1 * x1 322 | y12 = y1 * y1 323 | x1y1 = 2 * x1 * y1 324 | r2 = x12 + y12 325 | r4 = r2 * r2 326 | r6 = r2 * r4 327 | 328 | radial_distortion = (1 + k1 * r2 + k2 * r4 + k3 * r6) / (1 + k4 * r2 + k5 * r4 + k6 * r6) 329 | x2 = x1 * radial_distortion + p1 * x1y1 + p2 * (r2 + 2 * x12) + s1 * r2 + s2 * r4 330 | y2 = y1 * radial_distortion + p2 * x1y1 + p1 * (r2 + 2 * y12) + s3 * r2 + s4 * r4 331 | 332 | x3 = tao11 * x2 + tao12 * y2 + tao13 333 | y3 = tao21 * x2 + tao22 * y2 + tao23 334 | 335 | P[:, :, 0] = fu * x3 + ppu 336 | P[:, :, 1] = fv * y3 + ppv 337 | P[c3 <= 0] = 0 338 | 339 | return P 340 | 341 | 342 | def spatial_transform(self, image_data, new_image_size, mtx, dist, rvecs, tvecs, interpolation): 343 | rotation, _ = cv2.Rodrigues(rvecs) 344 | world_map = self.virtual_camera_to_world(new_image_size) 345 | image_map = self.world_to_image(new_image_size, world_map, mtx, dist, rotation, tvecs) 346 | image_map = image_map.astype(np.float32) 347 | dst = cv2.remap(image_data, image_map[:, :, 0], image_map[:, :, 1], interpolation) 348 | return dst 349 | 350 | 351 | def calibrate(self, org_size, image_coord, world_coord): 352 | """ 353 | calibration 354 | :param org_size: 355 | :param image_coord: 356 | :param world_coord: 357 | :return: 358 | """ 359 | # flag = cv2.CALIB_RATIONAL_MODEL | cv2.CALIB_TILTED_MODEL | cv2.CALIB_THIN_PRISM_MODEL 360 | flag = cv2.CALIB_RATIONAL_MODEL 361 | flag2 = cv2.CALIB_RATIONAL_MODEL | cv2.CALIB_TILTED_MODEL 362 | flag3 = cv2.CALIB_RATIONAL_MODEL | cv2.CALIB_THIN_PRISM_MODEL 363 | flag4 = cv2.CALIB_RATIONAL_MODEL | cv2.CALIB_ZERO_TANGENT_DIST | cv2.CALIB_FIX_ASPECT_RATIO 364 | flag5 = cv2.CALIB_RATIONAL_MODEL | cv2.CALIB_TILTED_MODEL | cv2.CALIB_ZERO_TANGENT_DIST 365 | flag6 = cv2.CALIB_RATIONAL_MODEL | cv2.CALIB_FIX_ASPECT_RATIO 366 | flag_list = [flag2, flag3, flag4, flag5, flag6] 367 | 368 | ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(world_coord.astype(np.float32), 369 | image_coord.astype(np.float32), 370 | org_size, 371 | None, 372 | None, 373 | flags=flag) 374 | if ret > 2: 375 | # strategies 376 | min_ret = ret 377 | for i, flag in enumerate(flag_list): 378 | _ret, _mtx, _dist, _rvecs, _tvecs = cv2.calibrateCamera(world_coord.astype(np.float32), 379 | image_coord.astype(np.float32), 380 | org_size, 381 | None, 382 | None, 383 | flags=flag) 384 | if _ret < min_ret: 385 | min_ret = _ret 386 | ret, mtx, dist, rvecs, tvecs = _ret, _mtx, _dist, _rvecs, _tvecs 387 | 388 | return ret, mtx, dist, rvecs, tvecs 389 | 390 | 391 | def dc_homo(self, img, img_points, obj_points, is_horizontal_text, interpolation=cv2.INTER_LINEAR, 392 | ratio_width=1.0, ratio_height=1.0): 393 | """ 394 | divide and conquer: homography 395 | # ratio_width and ratio_height must be 1.0 here 396 | """ 397 | _img_points = img_points.reshape(-1, 2) 398 | _obj_points = obj_points.reshape(-1, 3) 399 | 400 | homo_img_list = [] 401 | width_list = [] 402 | height_list = [] 403 | # divide and conquer 404 | for i in range(len(_img_points) // 2 - 1): 405 | new_img_points = np.zeros((4, 2)).astype(np.float32) 406 | new_obj_points = np.zeros((4, 2)).astype(np.float32) 407 | 408 | new_img_points[0:2, :] = _img_points[i:(i + 2), :2] 409 | new_img_points[2:4, :] = _img_points[::-1, :][i:(i + 2), :2][::-1, :] 410 | 411 | new_obj_points[0:2, :] = _obj_points[i:(i + 2), :2] 412 | new_obj_points[2:4, :] = _obj_points[::-1, :][i:(i + 2), :2][::-1, :] 413 | 414 | if is_horizontal_text: 415 | world_width = np.abs(new_obj_points[1, 0] - new_obj_points[0, 0]) 416 | world_height = np.abs(new_obj_points[3, 1] - new_obj_points[0, 1]) 417 | else: 418 | world_width = np.abs(new_obj_points[1, 1] - new_obj_points[0, 1]) 419 | world_height = np.abs(new_obj_points[3, 0] - new_obj_points[0, 0]) 420 | 421 | homo_img = Homography(img, new_img_points, world_width, world_height, 422 | interpolation=interpolation, 423 | ratio_width=ratio_width, ratio_height=ratio_height) 424 | 425 | homo_img_list.append(homo_img) 426 | _h, _w = homo_img.shape[:2] 427 | width_list.append(_w) 428 | height_list.append(_h) 429 | 430 | # stitching 431 | rectified_image = np.zeros((np.max(height_list), sum(width_list), 3)).astype(np.uint8) 432 | 433 | st = 0 434 | for (homo_img, w, h) in zip(homo_img_list, width_list, height_list): 435 | rectified_image[:h, st:st + w, :] = homo_img 436 | st += w 437 | 438 | if not is_horizontal_text: 439 | # vertical rotation 440 | rectified_image = np.rot90(rectified_image, 3) 441 | 442 | return rectified_image 443 | 444 | 445 | def __call__(self, image_data, points, interpolation=cv2.INTER_LINEAR, ratio_width=1.0, ratio_height=1.0, mode='calibration'): 446 | """ 447 | spatial transform for a poly text 448 | :param image_data: 449 | :param points: [x1,y1,x2,y2,x3,y3,...], clockwise order, (x1,y1) must be the top-left of first char. 450 | :param interpolation: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_AREA, cv2.INTER_CUBIC, cv2.INTER_LANCZOS4 451 | :param ratio_width: roi_image width expansion. It should not be smaller than 1.0 452 | :param ratio_height: roi_image height expansion. It should not be smaller than 1.0 453 | :param mode: 'calibration' or 'homography'. when homography, ratio_width and ratio_height must be 1.0 454 | :return: 455 | """ 456 | org_h, org_w = image_data.shape[:2] 457 | org_size = (org_w, org_h) 458 | self.image = image_data 459 | 460 | is_horizontal_text = self.horizontal_text_estimate(points) 461 | if is_horizontal_text: 462 | image_coord, world_coord, new_image_size = self.horizontal_text_process(points) 463 | else: 464 | image_coord, world_coord, new_image_size = self.vertical_text_process(points, org_size) 465 | 466 | if mode.lower() == 'calibration': 467 | ret, mtx, dist, rvecs, tvecs = self.calibrate(org_size, image_coord, world_coord) 468 | 469 | st_size = (int(new_image_size[0]*ratio_width), int(new_image_size[1]*ratio_height)) 470 | dst = self.spatial_transform(image_data, st_size, mtx, dist[0], rvecs[0], tvecs[0], interpolation) 471 | elif mode.lower() == 'homography': 472 | # ratio_width and ratio_height must be 1.0 here and ret set to 0.01 without loss manually 473 | ret = 0.01 474 | dst = self.dc_homo(image_data, image_coord, world_coord, is_horizontal_text, 475 | interpolation=interpolation, ratio_width=1.0, ratio_height=1.0) 476 | else: 477 | raise ValueError('mode must be ["calibration", "homography"], but got {}'.format(mode)) 478 | 479 | return dst, ret 480 | 481 | 482 | class PlanB: 483 | def __call__(self, image, points, curveTextRectifier, interpolation=cv2.INTER_LINEAR, 484 | ratio_width=1.0, ratio_height=1.0, loss_thresh=5.0, square=False): 485 | """ 486 | Plan B using sub-image when it failed in original image 487 | :param image: 488 | :param points: 489 | :param curveTextRectifier: CurveTextRectifier 490 | :param interpolation: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_AREA, cv2.INTER_CUBIC, cv2.INTER_LANCZOS4 491 | :param ratio_width: roi_image width expansion. It should not be smaller than 1.0 492 | :param ratio_height: roi_image height expansion. It should not be smaller than 1.0 493 | :param loss_thresh: if loss greater than loss_thresh --> get_rotate_crop_image 494 | :param square: crop square image or not. True or False. The default is False 495 | :return: 496 | """ 497 | h, w = image.shape[:2] 498 | _points = np.array(points).reshape(-1, 2).astype(np.float32) 499 | x_min = int(np.min(_points[:, 0])) 500 | y_min = int(np.min(_points[:, 1])) 501 | x_max = int(np.max(_points[:, 0])) 502 | y_max = int(np.max(_points[:, 1])) 503 | dx = x_max - x_min 504 | dy = y_max - y_min 505 | max_d = max(dx, dy) 506 | mean_pt = np.mean(_points, 0) 507 | 508 | expand_x = (ratio_width - 1.0) * 0.5 * max_d 509 | expand_y = (ratio_height - 1.0) * 0.5 * max_d 510 | 511 | if square: 512 | x_min = np.clip(int(mean_pt[0] - max_d - expand_x), 0, w - 1) 513 | y_min = np.clip(int(mean_pt[1] - max_d - expand_y), 0, h - 1) 514 | x_max = np.clip(int(mean_pt[0] + max_d + expand_x), 0, w - 1) 515 | y_max = np.clip(int(mean_pt[1] + max_d + expand_y), 0, h - 1) 516 | else: 517 | x_min = np.clip(int(x_min - expand_x), 0, w - 1) 518 | y_min = np.clip(int(y_min - expand_y), 0, h - 1) 519 | x_max = np.clip(int(x_max + expand_x), 0, w - 1) 520 | y_max = np.clip(int(y_max + expand_y), 0, h - 1) 521 | 522 | new_image = image[y_min:y_max, x_min:x_max, :].copy() 523 | new_points = _points.copy() 524 | new_points[:, 0] -= x_min 525 | new_points[:, 1] -= y_min 526 | 527 | dst_img, loss = curveTextRectifier(new_image, new_points, interpolation, ratio_width, ratio_height, mode='calibration') 528 | 529 | return dst_img, loss 530 | 531 | 532 | class AutoRectifier: 533 | def __init__(self): 534 | self.npoints = 10 535 | self.curveTextRectifier = CurveTextRectifier() 536 | 537 | @staticmethod 538 | def get_rotate_crop_image(img, points, interpolation=cv2.INTER_CUBIC, ratio_width=1.0, ratio_height=1.0): 539 | """ 540 | crop or homography 541 | :param img: 542 | :param points: 543 | :param interpolation: 544 | :param ratio_width: 545 | :param ratio_height: 546 | :return: 547 | """ 548 | h, w = img.shape[:2] 549 | _points = np.array(points).reshape(-1, 2).astype(np.float32) 550 | 551 | if len(_points) != 4: 552 | x_min = int(np.min(_points[:, 0])) 553 | y_min = int(np.min(_points[:, 1])) 554 | x_max = int(np.max(_points[:, 0])) 555 | y_max = int(np.max(_points[:, 1])) 556 | dx = x_max - x_min 557 | dy = y_max - y_min 558 | expand_x = int(0.5 * dx * (ratio_width - 1)) 559 | expand_y = int(0.5 * dy * (ratio_height - 1)) 560 | x_min = np.clip(int(x_min - expand_x), 0, w - 1) 561 | y_min = np.clip(int(y_min - expand_y), 0, h - 1) 562 | x_max = np.clip(int(x_max + expand_x), 0, w - 1) 563 | y_max = np.clip(int(y_max + expand_y), 0, h - 1) 564 | 565 | dst_img = img[y_min:y_max, x_min:x_max, :].copy() 566 | else: 567 | img_crop_width = int( 568 | max( 569 | np.linalg.norm(_points[0] - _points[1]), 570 | np.linalg.norm(_points[2] - _points[3]))) 571 | img_crop_height = int( 572 | max( 573 | np.linalg.norm(_points[0] - _points[3]), 574 | np.linalg.norm(_points[1] - _points[2]))) 575 | 576 | dst_img = Homography(img, _points, img_crop_width, img_crop_height, interpolation, ratio_width, ratio_height) 577 | 578 | return dst_img 579 | 580 | 581 | def visualize(self, image_data, points_list): 582 | visualization = image_data.copy() 583 | 584 | for box in points_list: 585 | box = np.array(box).reshape(-1, 2).astype(np.int32) 586 | cv2.drawContours(visualization, [np.array(box).reshape((-1, 1, 2))], -1, (0, 0, 255), 2) 587 | for i, p in enumerate(box): 588 | if i != 0: 589 | cv2.circle(visualization, tuple(p), radius=1, color=(255, 0, 0), thickness=2) 590 | else: 591 | cv2.circle(visualization, tuple(p), radius=1, color=(255, 255, 0), thickness=2) 592 | return visualization 593 | 594 | 595 | def __call__(self, image_data, points, interpolation=cv2.INTER_LINEAR, 596 | ratio_width=1.0, ratio_height=1.0, loss_thresh=5.0, mode='calibration'): 597 | """ 598 | rectification in strategies for a poly text 599 | :param image_data: 600 | :param points: [x1,y1,x2,y2,x3,y3,...], clockwise order, (x1,y1) must be the top-left of first char. 601 | :param interpolation: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_AREA, cv2.INTER_CUBIC, cv2.INTER_LANCZOS4 602 | :param ratio_width: roi_image width expansion. It should not be smaller than 1.0 603 | :param ratio_height: roi_image height expansion. It should not be smaller than 1.0 604 | :param loss_thresh: if loss greater than loss_thresh --> get_rotate_crop_image 605 | :param mode: 'calibration' or 'homography'. when homography, ratio_width and ratio_height must be 1.0 606 | :return: 607 | """ 608 | _points = np.array(points).reshape(-1,2) 609 | if len(_points) >= self.npoints and len(_points) % 2 == 0: 610 | try: 611 | curveTextRectifier = CurveTextRectifier() 612 | 613 | dst_img, loss = curveTextRectifier(image_data, points, interpolation, ratio_width, ratio_height, mode) 614 | if loss >= 2: 615 | # for robust 616 | # large loss means it cannot be reconstruct correctly, we must find other way to reconstruct 617 | img_list, loss_list = [dst_img], [loss] 618 | _dst_img, _loss = PlanB()(image_data, points, curveTextRectifier, 619 | interpolation, ratio_width, ratio_height, 620 | loss_thresh=loss_thresh, 621 | square=True) 622 | img_list += [_dst_img] 623 | loss_list += [_loss] 624 | 625 | _dst_img, _loss = PlanB()(image_data, points, curveTextRectifier, 626 | interpolation, ratio_width, ratio_height, 627 | loss_thresh=loss_thresh, square=False) 628 | img_list += [_dst_img] 629 | loss_list += [_loss] 630 | 631 | min_loss = min(loss_list) 632 | dst_img = img_list[loss_list.index(min_loss)] 633 | 634 | if min_loss >= loss_thresh: 635 | print('calibration loss: {} is too large for spatial transformer. It is failed. Using get_rotate_crop_image'.format(loss)) 636 | dst_img = self.get_rotate_crop_image(image_data, points, interpolation, ratio_width, ratio_height) 637 | print('here') 638 | except Exception as e: 639 | print(e) 640 | dst_img = self.get_rotate_crop_image(image_data, points, interpolation, ratio_width, ratio_height) 641 | else: 642 | dst_img = self.get_rotate_crop_image(image_data, _points, interpolation, ratio_width, ratio_height) 643 | 644 | return dst_img 645 | 646 | 647 | def run(self, image_data, points_list, interpolation=cv2.INTER_LINEAR, 648 | ratio_width=1.0, ratio_height=1.0, loss_thresh=5.0, mode='calibration'): 649 | """ 650 | run for texts in an image 651 | :param image_data: numpy.ndarray. The shape is [h, w, 3] 652 | :param points_list: [[x1,y1,x2,y2,x3,y3,...], [x1,y1,x2,y2,x3,y3,...], ...], clockwise order, (x1,y1) must be the top-left of first char. 653 | :param interpolation: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_AREA, cv2.INTER_CUBIC, cv2.INTER_LANCZOS4 654 | :param ratio_width: roi_image width expansion. It should not be smaller than 1.0 655 | :param ratio_height: roi_image height expansion. It should not be smaller than 1.0 656 | :param loss_thresh: if loss greater than loss_thresh --> get_rotate_crop_image 657 | :param mode: 'calibration' or 'homography'. when homography, ratio_width and ratio_height must be 1.0 658 | :return: res: roi-image list, visualized_image: draw polys in original image 659 | """ 660 | if image_data is None: 661 | raise ValueError 662 | if not isinstance(points_list, list): 663 | raise ValueError 664 | for points in points_list: 665 | if not isinstance(points, list): 666 | raise ValueError 667 | 668 | if ratio_width < 1.0 or ratio_height < 1.0: 669 | raise ValueError('ratio_width and ratio_height cannot be smaller than 1, but got {}', (ratio_width, ratio_height)) 670 | 671 | if mode.lower() != 'calibration' and mode.lower() != 'homography': 672 | raise ValueError('mode must be ["calibration", "homography"], but got {}'.format(mode)) 673 | 674 | if mode.lower() == 'homography' and ratio_width != 1.0 and ratio_height != 1.0: 675 | raise ValueError('ratio_width and ratio_height must be 1.0 when mode is homography, but got mode:{}, ratio:({},{})'.format(mode, ratio_width, ratio_height)) 676 | 677 | res = [] 678 | for points in points_list: 679 | rectified_img = self(image_data, points, interpolation, ratio_width, ratio_height, 680 | loss_thresh=loss_thresh, mode=mode) 681 | res.append(rectified_img) 682 | 683 | # visualize 684 | visualized_image = self.visualize(image_data, points_list) 685 | 686 | return res, visualized_image 687 | 688 | 689 | 690 | if __name__ == '__main__': 691 | # test for a poly text 692 | print('begin') 693 | 694 | import argparse 695 | parser = argparse.ArgumentParser() 696 | parser.add_argument('--image', type=str, help='Assign the image path') 697 | parser.add_argument('--txt', type=str, help='Assign the path of .txt to get points', 698 | default=None) 699 | parser.add_argument('--mode', type=str, help='Assign the mode: calibration or homography', 700 | default='calibration') 701 | args = parser.parse_args() 702 | 703 | test_points = [106, 167, 147, 143, 264, 94, 319, 85, 443, 98, 492, 116, 557, 704 | 148, 539, 181, 474, 149, 435, 135, 319, 122, 274, 130, 166, 176, 705 | 125, 200] 706 | 707 | image_path = './data/test_images/arc_text.jpg' 708 | 709 | if not os.path.exists(image_path): 710 | raise FileNotFoundError 711 | 712 | with open(image_path, "rb") as f: 713 | image = np.array(bytearray(f.read()), dtype="uint8") 714 | image = cv2.imdecode(image, cv2.IMREAD_COLOR) 715 | 716 | if image is None: 717 | raise ValueError('the image of {} is None'.format(image_path)) 718 | 719 | autoRectifier = AutoRectifier() 720 | res = autoRectifier(image, test_points, interpolation=cv2.INTER_LINEAR, 721 | ratio_width=1.0, ratio_height=1.0, 722 | loss_thresh=5.0, mode=args.mode) 723 | 724 | save_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'results') 725 | if not os.path.exists(save_path): 726 | os.makedirs(save_path) 727 | cv2.imwrite(os.path.join(save_path, 'OneBoxImageResult.jpg'), res) 728 | print('{} is saved.'.format(os.path.join(save_path, 'OneBoxImageResult.jpg'))) 729 | print('done!') -------------------------------------------------------------------------------- /data/test_images/arc_text.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/arc_text.jpg -------------------------------------------------------------------------------- /data/test_images/gt_12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_12.jpg -------------------------------------------------------------------------------- /data/test_images/gt_23.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_23.jpg -------------------------------------------------------------------------------- /data/test_images/gt_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_3.jpg -------------------------------------------------------------------------------- /data/test_images/gt_46.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_46.jpg -------------------------------------------------------------------------------- /data/test_images/gt_49.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_49.jpg -------------------------------------------------------------------------------- /data/test_images/gt_57.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_57.jpg -------------------------------------------------------------------------------- /data/test_images/gt_61.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_61.jpg -------------------------------------------------------------------------------- /data/test_images/gt_63.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_63.jpg -------------------------------------------------------------------------------- /data/test_images/gt_74.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_74.jpg -------------------------------------------------------------------------------- /data/test_images/gt_75.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_75.jpg -------------------------------------------------------------------------------- /data/test_images/gt_81.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_81.jpg -------------------------------------------------------------------------------- /data/test_images/gt_86.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_86.jpg -------------------------------------------------------------------------------- /data/test_images/gt_88.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_88.jpg -------------------------------------------------------------------------------- /data/test_images/gt_91.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_91.jpg -------------------------------------------------------------------------------- /data/test_images/gt_93.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_93.jpg -------------------------------------------------------------------------------- /data/test_images/gt_94.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_94.jpg -------------------------------------------------------------------------------- /data/test_images/gt_95.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_95.jpg -------------------------------------------------------------------------------- /data/test_images/gt_97.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_97.jpg -------------------------------------------------------------------------------- /data/test_images/gt_99.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/gt_99.jpg -------------------------------------------------------------------------------- /data/test_images/vincent.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_images/vincent.jpg -------------------------------------------------------------------------------- /data/test_result/vis_arc_text.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_arc_text.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_12.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_23.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_23.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_3.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_46.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_46.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_49.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_49.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_57.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_57.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_61.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_61.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_63.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_63.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_74.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_74.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_75.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_75.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_81.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_81.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_86.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_86.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_88.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_88.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_91.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_91.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_93.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_93.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_94.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_94.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_95.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_95.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_97.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_97.jpg -------------------------------------------------------------------------------- /data/test_result/vis_gt_99.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_gt_99.jpg -------------------------------------------------------------------------------- /data/test_result/vis_vincent.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/data/test_result/vis_vincent.jpg -------------------------------------------------------------------------------- /data/test_txt/arc_text.txt: -------------------------------------------------------------------------------- 1 | 106,167,147,143,264,94,319,85,443,98,492,116,557,148,539,181,474,149,435,135,319,122,274,130,166,176,125,200 -------------------------------------------------------------------------------- /data/test_txt/gt_12.txt: -------------------------------------------------------------------------------- 1 | 193,149,223,115,298,49,346,37,422,45,502,108,535,138,510,162,477,132,410,77,347,71,313,81,249,138,219,172 2 | 1,80,144,76,146,120,3,124 3 | 77,124,85,123,99,121,112,121,130,122,142,120,150,119,152,137,143,137,131,139,112,138,100,139,87,140,78,141 4 | 0,127,73,123,74,141,1,146 5 | 260,152,463,146,464,173,261,179 6 | 297,181,310,180,335,179,362,179,387,177,415,178,434,177,433,204,414,205,389,204,363,206,335,206,312,207,299,208 7 | 213,324,246,293,299,253,353,241,429,250,490,293,516,315,493,344,467,322,417,285,354,278,313,287,269,321,237,352 8 | 235,377,495,373,496,402,236,406 9 | -------------------------------------------------------------------------------- /data/test_txt/gt_23.txt: -------------------------------------------------------------------------------- 1 | -26,282,8,260,110,195,167,153,230,116,310,62,377,26,438,80,372,117,275,185,210,223,156,263,55,328,20,350 2 | 316,205,385,205,385,234,316,234 3 | 368,249,375,249,381,249,387,248,394,247,402,243,407,241,410,249,406,251,397,255,388,257,382,257,375,258,368,258 4 | 405,490,418,490,418,517,405,517 5 | -------------------------------------------------------------------------------- /data/test_txt/gt_3.txt: -------------------------------------------------------------------------------- 1 | 294,94,307,95,324,97,344,104,360,112,371,122,381,131,369,153,359,143,347,133,335,127,318,121,301,119,288,118 2 | 226,133,235,124,240,119,251,111,267,103,283,123,295,118,288,94,277,99,278,126,263,133,255,138,244,147,235,157 3 | 260,142,269,141,285,140,301,138,322,138,337,139,347,140,346,158,337,157,323,157,302,157,286,158,270,159,261,160 4 | 251,165,268,164,280,164,300,163,325,163,339,162,356,162,355,181,338,181,325,181,300,181,281,183,269,183,252,184 5 | -------------------------------------------------------------------------------- /data/test_txt/gt_46.txt: -------------------------------------------------------------------------------- 1 | 156,231,187,202,267,137,323,134,382,153,445,219,473,257,446,276,418,238,367,183,319,166,275,168,211,224,180,252 2 | 173,498,198,472,275,399,312,391,383,398,465,449,495,473,471,503,441,479,372,435,313,430,290,435,228,495,204,522 3 | 337,494,374,494,374,514,337,514 4 | 462,493,493,493,493,521,462,521 5 | -------------------------------------------------------------------------------- /data/test_txt/gt_49.txt: -------------------------------------------------------------------------------- 1 | 354,319,383,280,455,205,543,178,637,194,719,266,756,307,740,319,702,278,630,214,543,198,464,223,401,291,371,330 2 | 375,331,416,284,491,219,554,207,634,227,706,304,748,356,717,372,676,320,617,257,552,242,502,251,444,305,403,351 3 | 478,265,640,265,640,406,478,406 4 | 355,405,381,392,434,496,408,509 5 | 432,420,672,437,671,455,431,439 6 | 434,442,669,458,668,477,432,461 7 | 712,444,729,444,729,462,712,462 8 | 699,464,720,464,720,483,699,483 9 | 442,465,473,467,515,471,547,472,586,475,629,478,661,479,660,497,628,496,585,493,545,491,513,489,472,485,441,483 10 | 684,483,707,483,707,502,684,502 11 | 425,497,444,497,444,515,425,515 12 | 497,496,600,502,599,520,496,514 13 | 668,499,689,499,689,518,668,518 14 | 447,510,479,524,522,539,547,542,590,541,631,524,662,511,670,527,639,541,594,559,547,560,518,556,470,540,439,526 15 | 8,740,56,740,56,763,8,763 16 | 56,740,81,740,81,763,56,763 17 | 280,743,294,743,321,743,352,743,373,742,397,744,421,745,421,761,396,760,373,758,353,760,320,759,296,759,281,759 18 | 87,746,95,746,114,746,133,746,154,745,171,745,185,744,185,759,171,760,153,760,133,761,114,761,96,761,88,761 19 | 249,745,273,745,273,761,249,761 20 | 186,748,193,748,203,747,207,747,216,747,221,748,227,749,227,761,221,760,216,759,207,759,201,759,193,760,186,761 21 | 875,750,1016,750,1016,761,875,761 22 | -------------------------------------------------------------------------------- /data/test_txt/gt_57.txt: -------------------------------------------------------------------------------- 1 | 181,244,198,239,225,234,247,231,276,234,300,238,322,244,317,268,295,262,273,258,247,256,231,257,203,263,186,268 2 | 218,260,281,260,281,277,218,277 3 | 214,289,285,289,285,304,214,304 4 | -------------------------------------------------------------------------------- /data/test_txt/gt_61.txt: -------------------------------------------------------------------------------- 1 | 200,9,453,-11,457,41,205,62 2 | 618,0,716,0,716,16,618,16 3 | 167,231,195,211,275,165,377,161,445,176,515,232,549,265,516,305,482,272,425,224,374,213,286,216,221,256,193,275 4 | 611,261,625,247,636,235,656,218,679,203,690,201,710,192,747,208,727,216,699,237,683,248,662,266,653,276,639,291 5 | 146,261,180,261,180,298,146,298 6 | 227,390,447,386,448,421,228,425 7 | 524,500,544,500,544,530,524,530 8 | 524,534,542,534,542,546,524,546 9 | 131,627,184,590,267,542,339,534,410,541,510,591,562,622,537,654,485,623,398,579,339,574,278,581,210,622,157,659 10 | 481,542,520,542,520,550,481,550 11 | 682,541,721,541,721,580,682,580 12 | 669,553,697,580,638,642,610,615 13 | 240,665,449,665,449,706,240,706 14 | -------------------------------------------------------------------------------- /data/test_txt/gt_63.txt: -------------------------------------------------------------------------------- 1 | 3,33,23,31,33,103,14,106 2 | 34,48,44,48,44,77,34,77 3 | 421,62,430,62,434,205,425,205 4 | 166,76,189,70,238,58,271,59,308,68,332,76,355,89,341,132,318,119,297,112,266,104,243,103,201,114,178,120 5 | 38,78,48,78,50,121,40,121 6 | 142,98,192,98,192,145,142,145 7 | 23,102,31,102,33,131,25,132 8 | 14,104,22,104,22,126,14,126 9 | 241,112,351,158,335,197,224,152 10 | 441,125,461,125,461,182,441,182 11 | 24,133,34,132,39,183,30,184 12 | 110,148,134,147,182,147,244,156,287,167,344,187,374,200,355,242,324,229,270,209,232,200,180,193,131,193,107,194 13 | 247,179,260,188,276,206,288,208,307,222,317,230,328,241,320,270,308,260,281,240,272,235,257,230,244,214,231,205 14 | 152,197,206,197,206,218,152,218 15 | 211,199,217,203,222,204,228,208,237,212,234,226,242,229,252,218,244,215,231,226,221,222,218,218,207,215,201,210 16 | 117,217,173,217,173,242,117,242 17 | 178,224,190,224,190,241,178,241 18 | 197,225,254,231,252,249,195,243 19 | 313,222,318,226,327,230,340,235,345,240,353,236,361,236,364,246,356,246,344,250,335,245,322,240,314,236,309,232 20 | 115,250,275,258,273,299,113,291 21 | 277,268,317,268,317,281,277,281 22 | 279,286,289,293,299,299,311,309,322,316,335,326,346,332,334,353,323,347,308,336,297,329,284,319,273,312,263,304 23 | 120,287,133,292,157,298,176,303,186,298,212,306,226,303,219,329,205,331,190,325,173,329,151,324,128,318,115,313 24 | 188,331,207,327,224,329,244,325,258,328,286,334,294,337,289,354,280,351,256,345,247,342,223,346,203,344,183,347 25 | 116,350,152,350,152,370,116,370 26 | 164,353,305,353,305,393,164,393 27 | 324,360,329,362,336,365,344,369,352,376,357,381,362,388,355,396,350,389,344,383,338,378,332,375,324,371,318,370 28 | 317,366,321,369,327,377,334,381,341,387,351,392,357,395,353,403,347,400,336,394,329,388,321,385,313,374,309,371 29 | 114,369,165,367,165,386,114,388 30 | 164,392,176,395,198,398,230,402,249,402,263,403,286,403,287,428,264,428,249,427,229,427,194,423,172,419,159,416 31 | 300,420,306,423,321,430,335,439,343,445,357,452,368,458,360,468,350,462,337,456,328,450,315,442,301,435,294,432 32 | 286,438,301,446,315,451,327,460,337,471,358,483,371,492,349,516,336,506,321,499,308,486,300,479,277,467,262,460 33 | 117,446,234,446,234,482,117,482 34 | 119,479,295,481,294,544,118,541 35 | 293,503,368,517,359,564,284,550 36 | 114,544,186,544,186,561,114,561 37 | 219,541,226,544,232,545,236,546,241,546,250,546,257,546,253,560,246,560,241,560,236,560,228,559,223,558,216,555 38 | 185,548,220,548,220,561,185,561 39 | 117,561,176,561,176,584,117,584 40 | 174,561,209,561,209,576,174,576 41 | 196,583,240,557,259,588,215,615 42 | 249,565,313,565,313,605,249,605 43 | 177,594,214,576,223,594,186,612 44 | -------------------------------------------------------------------------------- /data/test_txt/gt_74.txt: -------------------------------------------------------------------------------- 1 | 403,276,654,242,663,311,412,345 2 | 255,349,365,278,406,341,296,412 3 | 291,632,344,603,396,572,494,566,542,563,620,573,677,582,672,692,615,683,537,673,495,676,432,677,391,703,338,732 4 | -------------------------------------------------------------------------------- /data/test_txt/gt_75.txt: -------------------------------------------------------------------------------- 1 | 114,831,1000,920,978,1141,92,1053 2 | 3562,923,4019,960,4007,1114,3549,1077 3 | 3224,906,3292,944,3297,957,3369,966,3436,971,3519,976,3593,969,3524,1092,3450,1099,3417,1110,3341,1103,3300,1097,3237,1073,3169,1036 4 | 3293,1111,4307,1164,4293,1432,3280,1379 5 | 2332,1298,2362,1287,2408,1278,2458,1269,2513,1270,2557,1279,2588,1288,2581,1349,2550,1340,2507,1331,2463,1330,2414,1339,2375,1347,2345,1357 6 | 2327,1358,2613,1335,2624,1470,2338,1493 7 | 65,1370,826,1370,826,1616,65,1616 8 | 2337,1490,2383,1481,2437,1478,2491,1471,2536,1471,2580,1471,2626,1471,2615,1559,2569,1559,2542,1559,2502,1559,2444,1566,2385,1570,2339,1578 9 | 2755,1580,2929,1580,2929,1638,2755,1638 10 | 3808,1675,3865,1673,3947,1669,4047,1654,4102,1641,4202,1606,4256,1584,4300,1688,4246,1710,4140,1748,4070,1765,3957,1781,3866,1786,3808,1788 11 | 2421,1682,2660,1682,2660,1783,2421,1783 12 | 3504,1709,3802,1674,3818,1814,3520,1848 13 | 4114,1755,4307,1707,4333,1812,4141,1861 14 | 2813,1740,3030,1740,3030,1783,2813,1783 15 | 2372,1841,2417,1824,2489,1805,2562,1779,2623,1775,2688,1763,2736,1763,2743,1857,2695,1857,2632,1869,2576,1872,2528,1891,2461,1907,2417,1924 16 | 2871,1790,2972,1790,2972,1827,2871,1827 17 | 3819,1799,3862,1805,3941,1808,3972,1809,4025,1801,4102,1780,4143,1765,4149,1846,4108,1860,4044,1880,3982,1889,3941,1889,3860,1885,3817,1880 18 | 2833,1839,3008,1829,3010,1870,2835,1880 19 | 2530,1888,2715,1861,2722,1913,2538,1939 20 | 2711,1935,2791,1935,2791,2001,2711,2001 21 | 2403,1963,2714,1924,2730,2050,2418,2089 22 | 2416,2051,2830,2007,2845,2157,2432,2200 23 | 2760,2280,2810,2267,2846,2258,2896,2256,2931,2256,2937,2353,2988,2353,3016,2258,2966,2258,2931,2355,2896,2355,2875,2352,2827,2364,2776,2376 24 | 2473,2303,2519,2295,2575,2283,2639,2280,2706,2274,2760,2278,2808,2281,2784,2372,2737,2369,2703,2367,2656,2372,2590,2375,2533,2387,2487,2395 25 | 2571,2389,2618,2387,2690,2385,2747,2380,2794,2369,2838,2369,2884,2360,2896,2454,2849,2462,2792,2463,2754,2474,2711,2477,2624,2481,2577,2483 26 | 379,2469,1566,2363,1586,2586,399,2691 27 | 2560,2506,2781,2476,2789,2532,2567,2562 28 | -------------------------------------------------------------------------------- /data/test_txt/gt_81.txt: -------------------------------------------------------------------------------- 1 | 417,-64,611,37,574,109,380,7 2 | 0,164,29,164,29,201,0,201 3 | 354,226,450,240,447,262,351,248 4 | 8,238,382,269,374,365,0,334 5 | 0,376,266,387,264,445,-2,434 6 | -------------------------------------------------------------------------------- /data/test_txt/gt_86.txt: -------------------------------------------------------------------------------- 1 | 60,181,147,188,95,907,7,901 2 | 264,224,278,221,309,218,349,223,365,227,400,255,411,264,399,277,388,268,357,244,345,240,308,236,281,239,266,242 3 | 212,250,258,222,270,242,224,269 4 | 261,268,354,268,354,301,261,301 5 | 234,320,368,316,368,332,235,335 6 | 189,426,211,409,259,380,301,373,341,379,384,401,412,420,399,436,372,418,335,398,301,393,266,399,223,425,201,443 7 | 255,427,343,430,342,467,254,464 8 | 229,483,365,483,365,499,229,499 9 | 226,546,258,540,291,536,321,539,352,550,397,583,408,593,390,613,379,603,341,574,315,566,293,563,265,566,233,572 10 | 180,593,219,555,238,574,200,613 11 | 235,598,354,598,354,635,235,635 12 | 223,651,361,654,361,670,223,667 13 | 170,762,193,746,249,716,292,711,335,720,369,739,410,772,394,790,353,758,327,743,291,735,256,740,208,766,185,782 14 | 228,772,348,776,346,817,227,812 15 | 216,831,362,838,361,857,215,851 16 | -------------------------------------------------------------------------------- /data/test_txt/gt_88.txt: -------------------------------------------------------------------------------- 1 | 381,434,654,322,1486,69,2245,44,2608,135,3316,415,3770,683,3544,1157,3089,888,2452,636,2211,567,1579,585,802,825,529,937 2 | 3986,112,4164,102,4168,173,3990,184 3 | -------------------------------------------------------------------------------- /data/test_txt/gt_91.txt: -------------------------------------------------------------------------------- 1 | 614,252,673,263,815,306,890,359,975,469,1016,552,1049,646,957,680,923,586,888,517,820,428,771,394,637,355,578,344 2 | 358,299,446,380,229,618,141,538 3 | 963,666,1102,752,829,1199,690,1114 4 | 212,795,247,853,321,971,370,1006,419,1039,534,1077,599,1094,562,1212,497,1195,375,1155,299,1107,221,1044,150,929,115,872 5 | -------------------------------------------------------------------------------- /data/test_txt/gt_93.txt: -------------------------------------------------------------------------------- 1 | 218,0,338,0,338,44,218,44 2 | 170,48,394,48,394,124,170,124 3 | 407,49,557,58,552,141,402,132 4 | 174,194,477,186,479,287,176,295 5 | -1,318,697,308,698,409,0,418 6 | 172,436,504,436,504,528,172,528 7 | 379,571,593,586,589,641,375,627 8 | 116,582,358,568,361,615,119,629 9 | 344,625,614,654,607,711,338,682 10 | 111,645,326,629,330,675,114,691 11 | 44,781,128,788,125,827,41,820 12 | 134,785,298,788,297,827,133,823 13 | 306,787,476,791,475,830,305,825 14 | 534,792,551,792,580,791,618,798,653,806,676,804,700,805,694,838,670,837,649,839,610,830,577,824,555,825,537,825 15 | 486,794,520,794,520,828,486,828 16 | -------------------------------------------------------------------------------- /data/test_txt/gt_94.txt: -------------------------------------------------------------------------------- 1 | 575,2156,704,2067,995,1905,1279,1801,1725,1738,1938,1730,2154,1744,2166,2037,1951,2023,1750,2030,1345,2087,1112,2174,841,2326,712,2416 2 | 1819,2138,1928,2138,1928,2211,1819,2211 3 | 1131,2305,1242,2277,1328,2256,1474,2219,1556,2199,1682,2167,1793,2139,1819,2238,1708,2266,1583,2297,1499,2318,1351,2355,1270,2375,1159,2403 4 | 964,2356,1102,2356,1102,2428,964,2428 5 | 422,2879,2124,2346,2268,2806,566,3339 6 | 800,3451,882,3414,1098,3353,1193,3302,1404,3246,1547,3189,1633,3163,1687,3332,1601,3357,1476,3407,1240,3472,1156,3520,940,3581,859,3617 7 | 612,3768,732,3760,902,3754,1175,3667,1301,3589,1461,3533,1621,3426,1658,3585,1498,3692,1373,3737,1232,3821,955,3909,740,3924,620,3932 8 | 835,3998,976,3957,1389,3826,1677,3729,1887,3612,2109,3581,2372,3506,2419,3707,2157,3782,1967,3802,1741,3926,1467,4017,1039,4153,897,4195 9 | 874,4405,1973,3745,2127,4002,1028,4662 10 | 971,4168,1210,4168,1210,4364,971,4364 11 | -------------------------------------------------------------------------------- /data/test_txt/gt_95.txt: -------------------------------------------------------------------------------- 1 | 593,206,606,203,635,197,674,188,701,185,729,184,746,184,746,199,729,199,702,200,677,202,638,211,610,217,597,221 2 | 759,183,767,184,793,188,812,192,826,196,847,203,862,208,854,224,839,218,821,213,809,209,790,206,765,201,756,200 3 | 673,207,751,205,752,215,673,217 4 | 754,205,814,216,812,227,752,216 5 | 640,214,671,208,673,218,642,225 6 | 614,222,638,214,642,225,618,233 7 | 540,231,578,212,587,229,549,248 8 | 820,220,847,227,844,237,817,230 9 | 553,248,612,220,617,232,558,260 10 | 549,365,857,357,857,381,549,389 11 | 797,384,856,386,855,399,797,398 12 | 705,386,759,388,759,401,705,399 13 | 764,387,791,387,791,398,764,398 14 | 647,386,653,387,665,389,675,389,687,388,695,388,704,387,704,399,695,400,688,400,674,401,665,401,653,399,647,398 15 | 571,390,614,390,614,402,571,402 16 | 618,390,646,390,646,401,618,401 17 | 549,390,566,390,566,402,549,402 18 | -------------------------------------------------------------------------------- /data/test_txt/gt_97.txt: -------------------------------------------------------------------------------- 1 | 0,122,116,141,104,211,-10,192 2 | 220,179,237,182,249,185,276,189,299,193,313,197,330,202,322,235,305,230,294,227,267,222,245,219,230,216,213,213 3 | 379,201,562,231,551,295,368,264 4 | 0,222,24,222,24,248,0,248 5 | 28,229,41,231,68,235,90,239,106,241,129,245,146,247,143,266,126,263,103,260,87,258,65,253,38,249,25,247 6 | 337,238,373,238,373,261,337,261 7 | 170,249,362,278,359,300,167,271 8 | 403,286,412,287,433,290,453,293,465,293,488,297,497,298,493,316,484,315,462,311,450,311,431,309,409,306,400,305 9 | 497,303,504,304,518,305,528,306,538,308,550,310,561,312,559,324,547,322,535,320,527,319,516,317,501,316,495,315 10 | 118,329,132,329,132,350,118,350 11 | 153,327,236,331,234,361,152,357 12 | 241,333,319,343,316,371,237,361 13 | 321,348,352,348,352,368,321,368 14 | -------------------------------------------------------------------------------- /data/test_txt/gt_99.txt: -------------------------------------------------------------------------------- 1 | 3452,453,3811,453,3811,554,3452,554 2 | 778,522,2641,628,2618,1034,755,928 3 | 1442,1077,2173,1077,2173,1209,1442,1209 4 | 2126,1203,2399,1187,2403,1259,2130,1275 5 | 1946,1203,2104,1203,2104,1272,1946,1272 6 | 1707,1209,1927,1209,1927,1278,1707,1278 7 | 1449,1215,1682,1215,1682,1285,1449,1285 8 | 907,1439,1082,1478,1401,1522,1706,1535,2009,1528,2450,1404,2622,1360,2715,1694,2544,1737,2055,1871,1717,1881,1379,1868,1041,1822,866,1783 9 | 1100,2234,2767,2061,2798,2354,1130,2528 10 | 1460,2881,2644,2705,2677,2926,1493,3101 11 | -------------------------------------------------------------------------------- /data/test_txt/vincent.txt: -------------------------------------------------------------------------------- 1 | 141,106,179,83,187,105,194,133,197,167,187,191,183,210,170,232,127,222,140,201,146,177,156,152,154,151,150,128 -------------------------------------------------------------------------------- /images/OneBoxImageResult.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/OneBoxImageResult.jpg -------------------------------------------------------------------------------- /images/vis_arc_text.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_arc_text.jpg -------------------------------------------------------------------------------- /images/vis_gt_46.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_46.jpg -------------------------------------------------------------------------------- /images/vis_gt_46_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_46_1.jpg -------------------------------------------------------------------------------- /images/vis_gt_46_1_h.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_46_1_h.jpg -------------------------------------------------------------------------------- /images/vis_gt_46_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_46_2.jpg -------------------------------------------------------------------------------- /images/vis_gt_57.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_57.jpg -------------------------------------------------------------------------------- /images/vis_gt_57_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_57_1.jpg -------------------------------------------------------------------------------- /images/vis_gt_57_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_57_2.jpg -------------------------------------------------------------------------------- /images/vis_gt_57_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_57_3.jpg -------------------------------------------------------------------------------- /images/vis_gt_61.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_61.jpg -------------------------------------------------------------------------------- /images/vis_gt_61_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_61_3.jpg -------------------------------------------------------------------------------- /images/vis_gt_61_9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_61_9.jpg -------------------------------------------------------------------------------- /images/vis_gt_86.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_86.jpg -------------------------------------------------------------------------------- /images/vis_gt_86_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_86_1.jpg -------------------------------------------------------------------------------- /images/vis_gt_86_13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_86_13.jpg -------------------------------------------------------------------------------- /images/vis_gt_86_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_86_2.jpg -------------------------------------------------------------------------------- /images/vis_gt_86_6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_86_6.jpg -------------------------------------------------------------------------------- /images/vis_gt_86_9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_86_9.jpg -------------------------------------------------------------------------------- /images/vis_gt_91.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_91.jpg -------------------------------------------------------------------------------- /images/vis_gt_91_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_91_1.jpg -------------------------------------------------------------------------------- /images/vis_gt_91_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_91_2.jpg -------------------------------------------------------------------------------- /images/vis_gt_91_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_91_3.jpg -------------------------------------------------------------------------------- /images/vis_gt_91_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frotms/Curve-Text-Rectification-Using-Pairs-Of-Points/832730643e4ca0eaedfbcb03cbd06a1a4c017543/images/vis_gt_91_4.jpg -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import os, sys 2 | import numpy as np 3 | import cv2 4 | from curve_text_rectification import AutoRectifier 5 | 6 | def txt_reader(txt_path): 7 | if not os.path.exists(txt_path): 8 | raise FileNotFoundError 9 | 10 | points_list = [] 11 | with open(txt_path, 'r') as f: 12 | data = f.readlines() 13 | for item in data: 14 | item = item.strip('\r\n').strip('\r').strip('\n') 15 | item = item.replace(' ', '').replace('\t', '') 16 | info = item.split(',') 17 | box = [float(i) for i in info] 18 | points_list.append(box) 19 | return points_list 20 | 21 | if __name__ == '__main__': 22 | # test an image 23 | print('begin') 24 | 25 | import argparse, os 26 | parser = argparse.ArgumentParser() 27 | parser.add_argument('--image', type=str, help='Assign the image path') 28 | parser.add_argument('--txt', type=str, help='Assign the path of .txt to get points', 29 | default=None) 30 | parser.add_argument('--mode', type=str, help='Assign the mode: calibration or homography', 31 | default='calibration') 32 | args = parser.parse_args() 33 | 34 | image_path = os.path.abspath(args.image) 35 | txt_path = os.path.abspath(args.txt) 36 | mode = args.mode 37 | 38 | if not os.path.exists(image_path): 39 | raise FileNotFoundError 40 | 41 | with open(image_path, "rb") as f: 42 | image = np.array(bytearray(f.read()), dtype="uint8") 43 | image = cv2.imdecode(image, cv2.IMREAD_COLOR) 44 | 45 | if image is None: 46 | raise ValueError('the image of {} is None'.format(image_path)) 47 | 48 | points_list = txt_reader(txt_path) 49 | 50 | autoRectifier = AutoRectifier() 51 | res, visualized_image = autoRectifier.run(image, points_list, interpolation=cv2.INTER_LINEAR, 52 | ratio_width=1.0, ratio_height=1.0, 53 | loss_thresh=5.0, mode=mode) 54 | 55 | save_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'results') 56 | if not os.path.exists(save_path): 57 | os.makedirs(save_path) 58 | 59 | image_path = os.path.basename(image_path) 60 | profix = image_path.split('.')[-1] 61 | basename = image_path[:-(len(profix)+1)] 62 | cv2.imwrite(os.path.join(save_path, 'vis_{}.jpg'.format(basename)), visualized_image) 63 | for i, rectified_image in enumerate(res): 64 | cv2.imwrite(os.path.join(save_path, 'vis_{}_{}.jpg'.format(basename, i+1)), rectified_image) 65 | print('saved in {}'.format(save_path)) 66 | print('done!') --------------------------------------------------------------------------------