├── .github └── workflows │ └── python-package.yml ├── .idea ├── .gitignore ├── dbnavigator.xml ├── inspectionProfiles │ ├── Project_Default.xml │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── paddleOCRPOC.iml └── vcs.xml ├── LICENSE ├── README.md ├── detection ├── CalculateMetrics.py ├── GetImagePath.py ├── OcrToDf.py ├── ReadGroundTruthFile.py ├── __pycache__ │ ├── CalculateMetrics.cpython-310.pyc │ ├── GetImagePath.cpython-310.pyc │ ├── OcrToDf.cpython-310.pyc │ ├── ReadGroundTruthFile.cpython-310.pyc │ ├── check_update_ground_truth.cpython-310.pyc │ ├── create_sheet.cpython-310.pyc │ └── decmain.cpython-310.pyc ├── check_update_ground_truth.py ├── create_sheet.py └── decmain.py ├── main.py ├── recognition ├── __pycache__ │ ├── calculate_cer.cpython-310.pyc │ ├── convert_txt_to_dict.cpython-310.pyc │ ├── create_excel_sheet.cpython-310.pyc │ ├── evaluate_model.cpython-310.pyc │ ├── get_image_paths.cpython-310.pyc │ ├── load_model.cpython-310.pyc │ ├── process_images.cpython-310.pyc │ └── rec_main.cpython-310.pyc ├── calculate_cer.py ├── convert_txt_to_dict.py ├── create_excel_sheet.py ├── evaluate_model.py ├── get_image_paths.py ├── load_model.py ├── process_images.py └── rec_main.py └── requirements.txt /.github/workflows/python-package.yml: -------------------------------------------------------------------------------- 1 | # This workflow will install Python dependencies, run tests and lint with a variety of Python versions 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python 3 | 4 | name: Python package 5 | 6 | on: 7 | push: 8 | branches: [ "main" ] 9 | pull_request: 10 | branches: [ "main" ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | strategy: 17 | fail-fast: false 18 | matrix: 19 | python-version: ["3.9", "3.10", "3.11"] 20 | 21 | steps: 22 | - uses: actions/checkout@v3 23 | - name: Set up Python ${{ matrix.python-version }} 24 | uses: actions/setup-python@v3 25 | with: 26 | python-version: ${{ matrix.python-version }} 27 | - name: Install dependencies 28 | run: | 29 | python -m pip install --upgrade pip 30 | python -m pip install flake8 pytest 31 | if [ -f requirements.txt ]; then pip install -r requirements.txt; fi 32 | - name: Lint with flake8 33 | run: | 34 | # stop the build if there are Python syntax errors or undefined names 35 | flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics 36 | # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide 37 | flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics 38 | - name: Test with pytest 39 | run: | 40 | pytest 41 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/dbnavigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 77 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/paddleOCRPOC.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /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 | # paddleOCR for Det and Rec 2 | Optical Character Recognition (OCR) is a powerful technology that enables machines to recognize and extract text from images or scanned documents. OCR finds applications in various fields, including document digitization, text extraction from images, and text-based data analysis. In this article, we will explore how to use PaddleOCR, an advanced OCR toolkit based on deep learning, for text detection and recognition tasks. We will walk through a code snippet that demonstrates the process step-by-step. 3 | # Prerequisites 4 | Before we dive into the code, let's ensure we have everything set up to run the PaddleOCR library. Make sure you have the following prerequisites installed on your machine: 5 | Python (3.6 or higher) 6 | PaddleOCR library 7 | Other necessary dependencies (e.g., NumPy, pandas, etc) 8 | 9 | Before running the code snippet, make sure you have the necessary libraries installed. You can find all the required packages and their versions in the requirements.txt file 10 | ```python 11 | pip install -r requirements.txt 12 | ``` 13 | # Getting started 14 | In the provided ```main.py```, we have an example usage of the RecMain class for performing text recognition (OCR) on a folder of images and generating an output Excel file with evaluation metrics: 15 | ``` 16 | # Example usage: Replace with your image folder path, label file, and desired output file name 17 | image_folder = "path/to/image_folder" 18 | label_file = "path/to/txt_file" 19 | output_file = "raw_output.xlsx" 20 | 21 | if __name__ == "__main__": 22 | RecMain(image_folder=image_folder, rec_file=label_file, output_file=output_file).run_rec() 23 | ``` 24 | 25 | same can be initiated for detection 26 | ``` 27 | #Dec 28 | # Example usage: Replace with your image folder path, label file, and desired output file name 29 | image_folder = "path/to/image_folder" 30 | label_file = "path/to/txt_file" 31 | output_file = "raw_output.xlsx" 32 | 33 | if __name__ == "__main__": 34 | DecMain(image_folder_path=image_folder, label_file_path=label_file, output_file=output_file) \ 35 | .run_dec() 36 | ``` 37 | 38 | # 1. Text Detection 39 | The code provided is a part of a class named DecMain, which seems to be designed for Optical Character Recognition (OCR) evaluation using ground truth data. It appears to use PaddleOCR to extract text from images and then calculates metrics like precision, recall, and Character Error Rate (CER) to evaluate the performance of the OCR system. 40 | ```python 41 | class DecMain: 42 | def __init__(self, image_folder_path, label_file_path, output_file): 43 | self.image_folder_path = image_folder_path 44 | self.label_file_path = label_file_path 45 | self.output_file = output_file 46 | 47 | def run_dec(self): 48 | # Check and update the ground truth file 49 | CheckAndUpdateGroundTruth(self.label_file_path).check_and_update_ground_truth_file() 50 | 51 | df = OcrToDf(image_folder=self.image_folder_path, label_file=self.label_file_path, det=True, rec=True, cls=False).ocr_to_df() 52 | 53 | ground_truth_data = ReadGroundTruthFile(self.label_file_path).read_ground_truth_file() 54 | 55 | # Get the extracted text as a list of dictionaries (representing the OCR results) 56 | ocr_results = df.to_dict(orient="records") 57 | 58 | # Calculate precision, recall, and CER 59 | precision, recall, total_samples = CalculateMetrics(ground_truth_data, ocr_results).calculate_precision_recall() 60 | 61 | CreateSheet(dataframe=df, precision=precision, recall=recall, total_samples=total_samples, 62 | file_name=self.output_file).create_sheet() 63 | ``` 64 | # Note: Format of the DET Ground Truth Label File 65 | ``` 66 | To perform OCR evaluation using the DecMain class and the provided code, it's crucial to format the ground truth label file correctly. 67 | The label file should be in JSON format and follow the structure as shown below: 68 | 69 | image_name.jpg [{"transcription": "215mm 18", "points": [[199, 6], [357, 6], [357, 33], [199, 33]], "difficult": False, "key_cls": "digits"}, {"transcription": "XZE SA", "points": [[15, 6], [140, 6], [140, 36], [15, 36]], "difficult": False, "key_cls": "text"}] 70 | 71 | The label file should be in JSON format. 72 | Each line of the file represents an image's OCR ground truth. 73 | Each line contains the filename of the image, followed by the OCR results for that image in the form of a JSON object. 74 | The JSON object should have the following keys: 75 | "transcription": The ground truth text transcription of the image. 76 | "points": A list of four points representing the bounding box coordinates of the text region in the image. 77 | "difficult": A boolean value indicating whether the text region is difficult to recognize. 78 | "key_cls": The class label of the OCR result, e.g., "digits" or "text". 79 | Make sure to follow this format while creating the ground truth label file for accurate OCR evaluation. 80 | ``` 81 | # 2. Text Recognition 82 | The code provided defines a class named RecMain, which is designed to run text recognition (OCR) using a pre-trained OCR model on a folder of images and generate an evaluation Excel sheet. 83 | ```python 84 | class RecMain: 85 | def __init__(self, image_folder, rec_file, output_file): 86 | self.image_folder = image_folder 87 | self.rec_file = rec_file 88 | self.output_file = output_file 89 | 90 | def run_rec(self): 91 | image_paths = GetImagePathsFromFolder(self.image_folder, self.rec_file). \ 92 | get_image_paths_from_folder() 93 | 94 | ocr_model = LoadRecModel().load_model() 95 | 96 | results = ProcessImages(ocr=ocr_model, image_paths=image_paths).process_images() 97 | 98 | ground_truth_data = ConvertTextToDict(self.rec_file).convert_txt_to_dict() 99 | 100 | model_predictions, ground_truth_texts, image_names, precision, recall, \ 101 | overall_model_precision, overall_model_recall, cer_data_list = EvaluateRecModel(results, 102 | ground_truth_data).evaluate_model() 103 | 104 | # Create Excel sheet 105 | CreateMetricExcel(image_names, model_predictions, ground_truth_texts, 106 | precision, recall, cer_data_list, overall_model_precision, overall_model_recall, 107 | self.output_file).create_excel_sheet() 108 | ``` 109 | # Note: Format of the Ground Truth Text File 110 | ``` 111 | To perform OCR evaluation using the RecMain class and the provided code, it's essential to format the ground truth (GT) text file correctly. 112 | The GT text file should be in the following format: 113 | 114 | image_name.jpg text 115 | 116 | Each line of the file represents an image's GT text. 117 | Each line contains the filename of the image, followed by a tab character (\t), and then the GT text for that image. 118 | Ensure that the GT text file contains GT text entries for all the images present in the image folder specified in the RecMain class. The GT text should match the actual text content present in the images. This format is necessary for accurate evaluation of the OCR model's performance. 119 | ``` 120 | 121 | **If you find this library useful, please consider starring this repository from the top of this page.** 122 | [![](https://i.imgur.com/oSLuE0e.png)](#) 123 | 124 | # Support my work 125 | Buy Me A Coffee 126 | 127 | # License 128 | ``` 129 | Copyright [2023] [Vinod Baste] 130 | 131 | Licensed under the Apache License, Version 2.0 (the "License"); 132 | you may not use this file except in compliance with the License. 133 | You may obtain a copy of the License at 134 | 135 | http://www.apache.org/licenses/LICENSE-2.0 136 | 137 | Unless required by applicable law or agreed to in writing, software 138 | distributed under the License is distributed on an "AS IS" BASIS, 139 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 140 | See the License for the specific language governing permissions and 141 | limitations under the License. 142 | ``` 143 | -------------------------------------------------------------------------------- /detection/CalculateMetrics.py: -------------------------------------------------------------------------------- 1 | class CalculateMetrics: 2 | def __init__(self, ground_truth_data_value, ocr_results_value): 3 | self.ground_truth_data_value = ground_truth_data_value 4 | self.ocr_results_value = ocr_results_value 5 | 6 | def calculate_precision_recall(self): 7 | total_image_samples = len(self.ground_truth_data_value) 8 | true_positives = 0 9 | false_positives = 0 10 | false_negatives = 0 11 | 12 | for gt_entry, ocr_entry in zip(self.ground_truth_data_value, self.ocr_results_value): 13 | gt_regions = gt_entry["Regions"] 14 | ocr_text = ocr_entry.get("Extracted_Text", "") # Replace "Extracted_Text" with the correct key name 15 | 16 | if not ocr_text: # Handle empty or None "Extracted_Text" as a special case 17 | ocr_text = "" 18 | 19 | # Combine all ground truth transcriptions into one string for comparison 20 | gt_text = " ".join(region["transcription"] for region in gt_regions) 21 | 22 | # Print the ground truth and detected text for each image 23 | print(f"Image: {gt_entry['Image_Path']}") 24 | print(f"Ground Truth: {gt_text}") 25 | print(f"Detected Text: {ocr_text}\n") 26 | 27 | # Calculate precision and recall 28 | true_positives_img = sum(1 for ocr_char, gt_char in zip(ocr_text, gt_text) if ocr_char == gt_char) 29 | false_positives_img = len(ocr_text) - true_positives_img 30 | false_negatives_img = len(gt_text) - true_positives_img 31 | 32 | # Update overall metrics 33 | true_positives += true_positives_img 34 | false_positives += false_positives_img 35 | false_negatives += false_negatives_img 36 | 37 | # Check for zero true positives to avoid division by zero 38 | if true_positives == 0: 39 | precision_value = 0.0 40 | recall_value = 0.0 41 | else: 42 | precision_value = true_positives / (true_positives + false_positives) 43 | recall_value = true_positives / (true_positives + false_negatives) 44 | 45 | return precision_value, recall_value, total_image_samples 46 | -------------------------------------------------------------------------------- /detection/GetImagePath.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | class GetImagePath: 5 | 6 | def __init__(self, image_name, image_folder_path): 7 | self.image_name = image_name 8 | self.image_folder_path = image_folder_path 9 | 10 | def get_image_path_by_name(self): 11 | image_path = os.path.join(self.image_folder_path, self.image_name) 12 | if os.path.exists(image_path) and os.path.isfile(image_path): 13 | return image_path 14 | else: 15 | print(f"Image with name '{self.image_name}' not found in the folder '{self.image_folder_path}'.") 16 | return None 17 | -------------------------------------------------------------------------------- /detection/OcrToDf.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import pandas as pd 4 | from paddleocr import PaddleOCR 5 | 6 | from detection.GetImagePath import GetImagePath 7 | 8 | 9 | class OcrToDf: 10 | def __init__(self, 11 | image_folder, 12 | label_file, 13 | lang='en', 14 | use_gpu=False, 15 | rec_path=None, 16 | det_db_thresh=0.5, 17 | rec_thresh=0.5, 18 | image_shape=(640, 640), 19 | cls=False, 20 | det=True, 21 | rec=True 22 | ): 23 | self.image_folder_path = image_folder 24 | self.label_file_path = label_file 25 | self.lang = lang 26 | self.use_gpu = use_gpu 27 | self.rec_path = rec_path 28 | self.det_db_thresh = det_db_thresh 29 | self.rec_thresh = rec_thresh 30 | self.image_shape = image_shape 31 | self.cls = cls 32 | self.det = det, 33 | self.rec = rec 34 | 35 | def ocr_to_df(self): 36 | # Initialize PaddleOCR with the text detection_poc model 37 | ocr = PaddleOCR( 38 | lang=self.lang, 39 | use_gpu=self.use_gpu, 40 | rec_path=self.rec_path, 41 | det_db_thresh=self.det_db_thresh, 42 | rec_thresh=self.rec_thresh, 43 | image_shape=self.image_shape, 44 | ) 45 | 46 | results = [] 47 | 48 | with open(self.label_file_path, 'r') as f: 49 | ground_truth_labels = f.readlines() 50 | 51 | for ground_truth in ground_truth_labels: 52 | 53 | image_path = GetImagePath(image_name=ground_truth.strip().split('\t')[0], 54 | image_folder_path=self.image_folder_path).get_image_path_by_name() 55 | 56 | if not image_path or image_path is None: 57 | continue 58 | 59 | # Perform text detection_poc and recognition (OCR) on the image 60 | result = ocr.ocr(image_path, cls=self.cls, det=self.det, rec=self.rec) 61 | if result[0]: 62 | print(result[0]) 63 | extracted_text = [] 64 | 65 | # Extract the text from OCR results 66 | for line in result[0]: 67 | for word_info in line[1]: 68 | extracted_text.append(str(word_info)) 69 | break 70 | 71 | regions = eval(ground_truth.strip().split('\t')[1]) 72 | 73 | # Combine all ground truth transcriptions into one string for comparison 74 | gt_text = " ".join(region["transcription"] for region in regions) 75 | 76 | # Calculate precision and recall 77 | true_positives = sum(1 for ocr_char, gt_char in zip(extracted_text, gt_text) if ocr_char == gt_char) 78 | false_positives = len(extracted_text) - true_positives 79 | false_negatives = len(gt_text) - true_positives 80 | 81 | # Check for division by zero 82 | if (true_positives + false_positives) == 0: 83 | precision_value = 0.0 84 | else: 85 | precision_value = true_positives / (true_positives + false_positives) 86 | 87 | if (true_positives + false_negatives) == 0: 88 | recall_value = 0.0 89 | else: 90 | recall_value = true_positives / (true_positives + false_negatives) 91 | 92 | # Append the extracted text and ground truth label to the results list 93 | results.append({"Image_Path": str(os.path.basename(image_path)), 94 | "Extracted_Text": " ".join(extracted_text), 95 | "Ground_Truth": gt_text, 96 | "Precision": precision_value, 97 | "Recall": recall_value 98 | }) 99 | 100 | # Create a DataFrame from the results list 101 | dataframe = pd.DataFrame(results) 102 | 103 | return dataframe 104 | -------------------------------------------------------------------------------- /detection/ReadGroundTruthFile.py: -------------------------------------------------------------------------------- 1 | class ReadGroundTruthFile: 2 | def __init__(self, ground_truth_file): 3 | self.ground_truth_file = ground_truth_file 4 | 5 | def read_ground_truth_file(self): 6 | with open(self.ground_truth_file, 'r') as f: 7 | lines = f.readlines() 8 | 9 | ground_truth_data_value = [] 10 | for line in lines: 11 | image_path, regions = line.strip().split('\t') 12 | regions = eval(regions) # Convert the string representation of the list to a Python list 13 | ground_truth_data_value.append({"Image_Path": image_path, "Regions": regions}) 14 | 15 | return ground_truth_data_value 16 | -------------------------------------------------------------------------------- /detection/__pycache__/CalculateMetrics.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodbaste/paddleOCR_rec_dec/bff1af448e4d9629d5eb723f13dc802c56fef394/detection/__pycache__/CalculateMetrics.cpython-310.pyc -------------------------------------------------------------------------------- /detection/__pycache__/GetImagePath.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodbaste/paddleOCR_rec_dec/bff1af448e4d9629d5eb723f13dc802c56fef394/detection/__pycache__/GetImagePath.cpython-310.pyc -------------------------------------------------------------------------------- /detection/__pycache__/OcrToDf.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodbaste/paddleOCR_rec_dec/bff1af448e4d9629d5eb723f13dc802c56fef394/detection/__pycache__/OcrToDf.cpython-310.pyc -------------------------------------------------------------------------------- /detection/__pycache__/ReadGroundTruthFile.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodbaste/paddleOCR_rec_dec/bff1af448e4d9629d5eb723f13dc802c56fef394/detection/__pycache__/ReadGroundTruthFile.cpython-310.pyc -------------------------------------------------------------------------------- /detection/__pycache__/check_update_ground_truth.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodbaste/paddleOCR_rec_dec/bff1af448e4d9629d5eb723f13dc802c56fef394/detection/__pycache__/check_update_ground_truth.cpython-310.pyc -------------------------------------------------------------------------------- /detection/__pycache__/create_sheet.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodbaste/paddleOCR_rec_dec/bff1af448e4d9629d5eb723f13dc802c56fef394/detection/__pycache__/create_sheet.cpython-310.pyc -------------------------------------------------------------------------------- /detection/__pycache__/decmain.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodbaste/paddleOCR_rec_dec/bff1af448e4d9629d5eb723f13dc802c56fef394/detection/__pycache__/decmain.cpython-310.pyc -------------------------------------------------------------------------------- /detection/check_update_ground_truth.py: -------------------------------------------------------------------------------- 1 | class CheckAndUpdateGroundTruth: 2 | def __init__(self, ground_truth_file): 3 | self.ground_truth_file = ground_truth_file 4 | 5 | def check_and_update_ground_truth_file(self): 6 | # Read the contents of the ground truth file 7 | with open(self.ground_truth_file, 'r') as f: 8 | content = f.read() 9 | 10 | # Replace all occurrences of 'false' with 'False' 11 | content = content.replace('false', 'False') 12 | 13 | # Write the modified content back to the ground truth file 14 | with open(self.ground_truth_file, 'w') as f: 15 | f.write(content) 16 | -------------------------------------------------------------------------------- /detection/create_sheet.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | class CreateSheet: 5 | def __init__(self, dataframe, precision, recall, total_samples, file_name="output_file.xlsx", 6 | extracted_text_sheet="Extracted_Texts_Metrics", model_metrics_sheet="Model_Metrics"): 7 | self.precision = precision 8 | self.recall = recall 9 | self.total_samples = total_samples 10 | self.df = dataframe 11 | self.file_name = file_name 12 | self.extracted_text_sheet = extracted_text_sheet 13 | self.model_metrics_sheet = model_metrics_sheet 14 | 15 | def create_sheet(self): 16 | # Create a DataFrame for the entire model's metrics 17 | df_model_metrics = pd.DataFrame( 18 | {"Model": "en_PP-OCRv3_det", "Precision": [self.precision], "Recall": [self.recall], 19 | "Total Samples": [self.total_samples]}) 20 | 21 | # Create a Pandas Excel writer object 22 | with pd.ExcelWriter(self.file_name, engine="xlsxwriter") as writer: 23 | # Save the DataFrame with extracted texts to the first sheet 24 | self.df.to_excel(writer, sheet_name=self.extracted_text_sheet, index=False) 25 | worksheet = writer.sheets[self.extracted_text_sheet] 26 | # Set the column width 27 | worksheet.set_column("A:C", 50) # Adjust the width as needed 28 | # Freeze the first row 29 | worksheet.freeze_panes(1, 0) 30 | 31 | # Save the DataFrame with model metrics to the second sheet 32 | df_model_metrics.to_excel(writer, sheet_name=self.model_metrics_sheet, index=False) 33 | worksheet = writer.sheets[self.model_metrics_sheet] 34 | # Set the column width 35 | worksheet.set_column("A:D", 20) # Adjust the width as needed 36 | # Freeze the first row 37 | worksheet.freeze_panes(1, 0) 38 | -------------------------------------------------------------------------------- /detection/decmain.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from detection.CalculateMetrics import CalculateMetrics 4 | from detection.OcrToDf import OcrToDf 5 | from detection.ReadGroundTruthFile import ReadGroundTruthFile 6 | from detection.check_update_ground_truth import CheckAndUpdateGroundTruth 7 | from detection.create_sheet import CreateSheet 8 | 9 | os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" 10 | 11 | 12 | class DecMain: 13 | def __init__(self, image_folder_path, label_file_path, output_file): 14 | self.image_folder_path = image_folder_path 15 | self.label_file_path = label_file_path 16 | self.output_file = output_file 17 | 18 | def run_dec(self): 19 | # Check and update the ground truth file 20 | CheckAndUpdateGroundTruth(self.label_file_path).check_and_update_ground_truth_file() 21 | 22 | df = OcrToDf(image_folder=self.image_folder_path, label_file=self.label_file_path, det=True, rec=True, cls=False).ocr_to_df() 23 | 24 | ground_truth_data = ReadGroundTruthFile(self.label_file_path).read_ground_truth_file() 25 | 26 | # Get the extracted text as a list of dictionaries (representing the OCR results) 27 | ocr_results = df.to_dict(orient="records") 28 | 29 | # Calculate precision, recall, and CER 30 | precision, recall, total_samples = CalculateMetrics(ground_truth_data, ocr_results).calculate_precision_recall() 31 | 32 | CreateSheet(dataframe=df, precision=precision, recall=recall, total_samples=total_samples, 33 | file_name=self.output_file).create_sheet() 34 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from detection.decmain import DecMain 2 | from recognition.rec_main import RecMain 3 | 4 | """ 5 | Note: Format of the REC Ground Truth Text File 6 | 7 | To perform OCR evaluation using the RecMain class and the provided code, it's essential to format the ground truth (GT) text file correctly. The GT text file should be in the following format: 8 | T25166601GTD_202301181427_1 - Copy (2)_crop_0.jpg MICHELIN 9 | Each line of the file represents an image's GT text. 10 | Each line contains the filename of the image, followed by a tab character (\t), and then the GT text for that image. 11 | Ensure that the GT text file contains GT text entries for all the images present in the image folder specified in the RecMain class. The GT text should match the actual text content present in the images. This format is necessary for accurate evaluation of the OCR model's performance. 12 | """ 13 | 14 | """ 15 | Note: Format of the DET Ground Truth Label File 16 | 17 | To perform OCR evaluation using the DecMain class and the provided code, it's crucial to format the ground truth label file correctly. The label file should be in JSON format and follow the structure as shown below: 18 | 19 | identity_T40829322TB_202212131924_1 - Copy (2).PNG [{"transcription": "215mm 18", "points": [[199, 6], [357, 6], [357, 33], [199, 33]], "difficult": False, "key_cls": "digits"}, {"transcription": "XZE SA", "points": [[15, 6], [140, 6], [140, 36], [15, 36]], "difficult": False, "key_cls": "text"}] 20 | The label file should be in JSON format. 21 | Each line of the file represents an image's OCR ground truth. 22 | Each line contains the filename of the image, followed by the OCR results for that image in the form of a JSON object. 23 | The JSON object should have the following keys: 24 | 25 | "transcription": The ground truth text transcription of the image. 26 | "points": A list of four points representing the bounding box coordinates of the text region in the image. 27 | "difficult": A boolean value indicating whether the text region is difficult to recognize. 28 | "key_cls": The class label of the OCR result, e.g., "digits" or "text". 29 | Make sure to follow this format while creating the ground truth label file for accurate OCR evaluation.""" 30 | 31 | # Example usage: Replace with your image folder path, label file, and desired output file name 32 | image_folder = "path/to/image_folder" 33 | label_file = "path/to/txt_file" 34 | output_file = "raw_output.xlsx" 35 | 36 | # if __name__ == "__main__": 37 | # DecMain(image_folder_path=image_folder, label_file_path=label_file, output_file=output_file) \ 38 | # .run_dec() 39 | 40 | if __name__ == "__main__": 41 | RecMain(image_folder=image_folder, rec_file=label_file, output_file=output_file).run_rec() 42 | -------------------------------------------------------------------------------- /recognition/__pycache__/calculate_cer.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodbaste/paddleOCR_rec_dec/bff1af448e4d9629d5eb723f13dc802c56fef394/recognition/__pycache__/calculate_cer.cpython-310.pyc -------------------------------------------------------------------------------- /recognition/__pycache__/convert_txt_to_dict.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodbaste/paddleOCR_rec_dec/bff1af448e4d9629d5eb723f13dc802c56fef394/recognition/__pycache__/convert_txt_to_dict.cpython-310.pyc -------------------------------------------------------------------------------- /recognition/__pycache__/create_excel_sheet.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodbaste/paddleOCR_rec_dec/bff1af448e4d9629d5eb723f13dc802c56fef394/recognition/__pycache__/create_excel_sheet.cpython-310.pyc -------------------------------------------------------------------------------- /recognition/__pycache__/evaluate_model.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodbaste/paddleOCR_rec_dec/bff1af448e4d9629d5eb723f13dc802c56fef394/recognition/__pycache__/evaluate_model.cpython-310.pyc -------------------------------------------------------------------------------- /recognition/__pycache__/get_image_paths.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodbaste/paddleOCR_rec_dec/bff1af448e4d9629d5eb723f13dc802c56fef394/recognition/__pycache__/get_image_paths.cpython-310.pyc -------------------------------------------------------------------------------- /recognition/__pycache__/load_model.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodbaste/paddleOCR_rec_dec/bff1af448e4d9629d5eb723f13dc802c56fef394/recognition/__pycache__/load_model.cpython-310.pyc -------------------------------------------------------------------------------- /recognition/__pycache__/process_images.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodbaste/paddleOCR_rec_dec/bff1af448e4d9629d5eb723f13dc802c56fef394/recognition/__pycache__/process_images.cpython-310.pyc -------------------------------------------------------------------------------- /recognition/__pycache__/rec_main.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodbaste/paddleOCR_rec_dec/bff1af448e4d9629d5eb723f13dc802c56fef394/recognition/__pycache__/rec_main.cpython-310.pyc -------------------------------------------------------------------------------- /recognition/calculate_cer.py: -------------------------------------------------------------------------------- 1 | 2 | class CalculateCER: 3 | def __init__(self, model_texts, ground_truth_texts_data): 4 | self.model_texts = model_texts 5 | self.ground_truth_texts_data = ground_truth_texts_data 6 | 7 | def calculate_cer(self): 8 | cer_scores = [] 9 | 10 | for model_text, ground_truth_text in zip(self.model_texts, self.ground_truth_texts_data): 11 | # Obtain Sentence-Level Character Error Rate (CER) 12 | cer_score = fastwer.score_sent(model_text, ground_truth_text, char_level=True) 13 | cer_scores.append(cer_score) 14 | 15 | return cer_scores 16 | -------------------------------------------------------------------------------- /recognition/convert_txt_to_dict.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | class ConvertTextToDict: 5 | def __init__(self, txt_file): 6 | self.txt_file = txt_file 7 | 8 | def convert_txt_to_dict(self): 9 | if not os.path.exists(self.txt_file): 10 | raise FileNotFoundError(f"The TXT file '{self.txt_file}' does not exist.") 11 | 12 | data_list = [] 13 | with open(self.txt_file, 'r') as file: 14 | for line_num, line in enumerate(file, 1): 15 | line = line.strip() 16 | if not line: 17 | continue # Skip empty lines 18 | parts = line.split('\t') 19 | if len(parts) != 2: 20 | raise ValueError(f"Invalid format at line {line_num} in '{self.txt_file}'. " 21 | f"Expected 'image_name\\tground_truth_text' format.") 22 | file_name, ground_truth_text = parts 23 | data_list.append({ 24 | 'file_name': file_name, 25 | 'ground_truth_text': ground_truth_text 26 | }) 27 | 28 | print(data_list) 29 | return data_list 30 | -------------------------------------------------------------------------------- /recognition/create_excel_sheet.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | class CreateMetricExcel: 5 | def __init__(self, image_names, model_predictions, ground_truth_texts, precision_list, 6 | recall_list, cer_list, overall_model_precision, overall_model_recall, 7 | file_name="output_file.xlsx", 8 | extracted_text_sheet="Extracted_Texts_Metrics", model_metrics_sheet="Model_Metrics"): 9 | self.image_names = image_names 10 | self.model_predictions = model_predictions 11 | self.ground_truth_texts = ground_truth_texts 12 | self.precision_list = precision_list 13 | self.recall_list = recall_list 14 | self.cer_list = cer_list 15 | self.overall_model_precision = overall_model_precision 16 | self.overall_model_recall = overall_model_recall 17 | self.file_name = file_name 18 | self.extracted_text_sheet = extracted_text_sheet 19 | self.model_metrics_sheet = model_metrics_sheet 20 | 21 | def create_excel_sheet(self): 22 | df_metrics = pd.DataFrame({"Image": str(self.image_names), 23 | "Extracted_Text": self.model_predictions, 24 | "Ground_Truth": self.ground_truth_texts, 25 | "Precision": self.precision_list, 26 | "Recall": self.recall_list, 27 | "CER": self.cer_list 28 | }) 29 | 30 | df_model_metrics = pd.DataFrame( 31 | {"Model": "en_PP-OCRv3_rec", "Precision": [self.overall_model_precision], 32 | "Recall": [self.overall_model_recall], "Total Samples": [len(self.image_names)]}) 33 | 34 | # Create a Pandas Excel writer object 35 | with pd.ExcelWriter(self.file_name, engine="xlsxwriter") as writer: 36 | # Save the DataFrame with extracted texts to the first sheet 37 | df_metrics.to_excel(writer, sheet_name=self.extracted_text_sheet, index=False) 38 | worksheet = writer.sheets[self.extracted_text_sheet] 39 | # Set the column width 40 | worksheet.set_column("A:F", 50) # Adjust the width as needed 41 | # Freeze the first row 42 | worksheet.freeze_panes(1, 0) 43 | 44 | # Save the DataFrame with model metrics to the second sheet 45 | df_model_metrics.to_excel(writer, sheet_name=self.model_metrics_sheet, index=False) 46 | worksheet = writer.sheets[self.model_metrics_sheet] 47 | # Set the column width 48 | worksheet.set_column("A:D", 20) # Adjust the width as needed 49 | # Freeze the first row 50 | worksheet.freeze_panes(1, 0) 51 | -------------------------------------------------------------------------------- /recognition/evaluate_model.py: -------------------------------------------------------------------------------- 1 | from sklearn.metrics import precision_recall_fscore_support, precision_score, recall_score, accuracy_score 2 | 3 | from recognition.calculate_cer import CalculateCER 4 | 5 | 6 | class EvaluateRecModel: 7 | def __init__(self, ocr_results, rec_ground_truth_data): 8 | self.ocr_results = ocr_results 9 | self.rec_ground_truth_data = rec_ground_truth_data 10 | 11 | def evaluate_model(self): 12 | model_predictions_data = [] 13 | ground_truth_texts_data = [] 14 | image_names_data = [] 15 | precision_list = [] 16 | recall_list = [] 17 | 18 | for result, annotation in zip(self.ocr_results, self.rec_ground_truth_data): 19 | model_text = [word_info[0] for line in result for word_info in line] 20 | model_text = ' '.join(model_text).strip() 21 | model_predictions_data.append(model_text) 22 | # Handle missing ground truth text 23 | ground_truth_text = annotation.get('ground_truth_text', 'NILL').replace(' ', '') 24 | ground_truth_texts_data.append(ground_truth_text) 25 | image_names_data.append(annotation.get('file_name', 'NILL')) # Handle missing image names 26 | 27 | # Calculate precision and recall for the current image 28 | precision_value, recall_value, f_score, true_sum = \ 29 | precision_recall_fscore_support([ground_truth_text], [model_text], average='weighted') 30 | precision_list.append(precision_value) 31 | recall_list.append(recall_value) 32 | 33 | overall_model_precision = precision_score(ground_truth_texts_data, model_predictions_data, average='weighted') 34 | overall_model_recall = recall_score(ground_truth_texts_data, model_predictions_data, average='weighted') 35 | 36 | cer_data_list = CalculateCER(model_predictions_data, ground_truth_texts_data).calculate_cer() 37 | 38 | return model_predictions_data, ground_truth_texts_data, \ 39 | image_names_data, precision_list, recall_list, overall_model_precision, overall_model_recall, cer_data_list 40 | -------------------------------------------------------------------------------- /recognition/get_image_paths.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from detection.GetImagePath import GetImagePath 4 | 5 | 6 | class GetImagePathsFromFolder: 7 | def __init__(self, image_folder, txt_file): 8 | self.image_folder = image_folder 9 | self.txt_file = txt_file 10 | 11 | def get_image_paths_from_folder(self): 12 | if not os.path.exists(self.image_folder): 13 | raise FileNotFoundError(f"The image folder '{self.image_folder}' does not exist.") 14 | 15 | image_paths = [] 16 | 17 | with open(self.txt_file, 'r') as file: 18 | for line_num, line in enumerate(file, 1): 19 | line = line.strip() 20 | if not line: 21 | continue # Skip empty lines 22 | 23 | image_path = GetImagePath(image_name=line.split('\t')[0], 24 | image_folder_path=self.image_folder).get_image_path_by_name() 25 | image_paths.append(os.path.join(self.image_folder, image_path)) 26 | 27 | return image_paths 28 | -------------------------------------------------------------------------------- /recognition/load_model.py: -------------------------------------------------------------------------------- 1 | from paddleocr import PaddleOCR 2 | 3 | 4 | class LoadRecModel: 5 | def __init__(self, use_gpu=False, rec_path=None, lang='en', rec_thresh=0.5, image_shape=(640, 640)): 6 | self.rec_thresh = rec_thresh 7 | self.image_shape = image_shape 8 | self.lang = lang 9 | self.use_gpu = use_gpu 10 | self.rec_path = rec_path 11 | 12 | def load_model(self): 13 | ocr = PaddleOCR(lang=self.lang, 14 | use_gpu=self.use_gpu, 15 | rec_path=self.rec_path, 16 | image_shape=self.image_shape, 17 | rec_thresh=self.rec_thresh) 18 | 19 | return ocr 20 | -------------------------------------------------------------------------------- /recognition/process_images.py: -------------------------------------------------------------------------------- 1 | from paddleocr import PaddleOCR 2 | 3 | 4 | class ProcessImages: 5 | 6 | def __init__(self, ocr, image_paths, cls=False, det=False, rec=True): 7 | self.ocr = ocr 8 | self.image_paths = image_paths 9 | self.cls = cls 10 | self.det = det 11 | self.rec = rec 12 | 13 | def process_images(self): 14 | results = [] 15 | 16 | for image_path in self.image_paths: 17 | result = self.ocr.ocr(image_path, cls=self.cls, det=True, rec=self.rec) # No need for the detection model 18 | results.append(result) 19 | 20 | return results 21 | -------------------------------------------------------------------------------- /recognition/rec_main.py: -------------------------------------------------------------------------------- 1 | from recognition.convert_txt_to_dict import ConvertTextToDict 2 | from recognition.create_excel_sheet import CreateMetricExcel 3 | from recognition.evaluate_model import EvaluateRecModel 4 | from recognition.get_image_paths import GetImagePathsFromFolder 5 | from recognition.load_model import LoadRecModel 6 | from recognition.process_images import ProcessImages 7 | 8 | 9 | class RecMain: 10 | def __init__(self, image_folder, rec_file, output_file): 11 | self.image_folder = image_folder 12 | self.rec_file = rec_file 13 | self.output_file = output_file 14 | 15 | def run_rec(self): 16 | image_paths = GetImagePathsFromFolder(self.image_folder, self.rec_file). \ 17 | get_image_paths_from_folder() 18 | 19 | ocr_model = LoadRecModel().load_model() 20 | 21 | results = ProcessImages(ocr=ocr_model, image_paths=image_paths).process_images() 22 | 23 | ground_truth_data = ConvertTextToDict(self.rec_file).convert_txt_to_dict() 24 | 25 | model_predictions, ground_truth_texts, image_names, precision, recall, \ 26 | overall_model_precision, overall_model_recall, cer_data_list = EvaluateRecModel(results, 27 | ground_truth_data).evaluate_model() 28 | 29 | # Create Excel sheet 30 | CreateMetricExcel(image_names, model_predictions, ground_truth_texts, 31 | precision, recall, cer_data_list, overall_model_precision, overall_model_recall, 32 | self.output_file).create_excel_sheet() 33 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | paddleocr==2.6.1.3 2 | paddlepaddle-gpu==2.5.0 3 | paddlepaddle==2.5.0 4 | pandas==2.0.3 5 | xlsxwriter==3.1.1 6 | scikit-learn==1.2.2 7 | fastwer 8 | --------------------------------------------------------------------------------