Commit ·
826e4da
1
Parent(s): 3726d9c
Upload 13 files
Browse files- Multi Engine OCR/.idea/.gitignore +3 -0
- Multi Engine OCR/.idea/Multi Engine OCR.iml +12 -0
- Multi Engine OCR/.idea/inspectionProfiles/profiles_settings.xml +6 -0
- Multi Engine OCR/.idea/misc.xml +4 -0
- Multi Engine OCR/.idea/modules.xml +8 -0
- Multi Engine OCR/.idea/vcs.xml +6 -0
- Multi Engine OCR/.idea/workspace.xml +99 -0
- Multi Engine OCR/docs/readme.md +52 -0
- Multi Engine OCR/docs/requirement.md +44 -0
- Multi Engine OCR/main.py +49 -0
- Multi Engine OCR/requirment.txt +7 -0
- Multi Engine OCR/src/__pycache__/ocr_utils.cpython-310.pyc +0 -0
- Multi Engine OCR/src/ocr_utils.py +91 -0
Multi Engine OCR/.idea/.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default ignored files
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
Multi Engine OCR/.idea/Multi Engine OCR.iml
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<module type="PYTHON_MODULE" version="4">
|
| 3 |
+
<component name="NewModuleRootManager">
|
| 4 |
+
<content url="file://$MODULE_DIR$" />
|
| 5 |
+
<orderEntry type="jdk" jdkName="Python 3.10" jdkType="Python SDK" />
|
| 6 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
| 7 |
+
</component>
|
| 8 |
+
<component name="PyDocumentationSettings">
|
| 9 |
+
<option name="format" value="PLAIN" />
|
| 10 |
+
<option name="myDocStringFormat" value="Plain" />
|
| 11 |
+
</component>
|
| 12 |
+
</module>
|
Multi Engine OCR/.idea/inspectionProfiles/profiles_settings.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<settings>
|
| 3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
| 4 |
+
<version value="1.0" />
|
| 5 |
+
</settings>
|
| 6 |
+
</component>
|
Multi Engine OCR/.idea/misc.xml
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10" project-jdk-type="Python SDK" />
|
| 4 |
+
</project>
|
Multi Engine OCR/.idea/modules.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectModuleManager">
|
| 4 |
+
<modules>
|
| 5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/Multi Engine OCR.iml" filepath="$PROJECT_DIR$/.idea/Multi Engine OCR.iml" />
|
| 6 |
+
</modules>
|
| 7 |
+
</component>
|
| 8 |
+
</project>
|
Multi Engine OCR/.idea/vcs.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="VcsDirectoryMappings">
|
| 4 |
+
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
| 5 |
+
</component>
|
| 6 |
+
</project>
|
Multi Engine OCR/.idea/workspace.xml
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="AutoImportSettings">
|
| 4 |
+
<option name="autoReloadType" value="SELECTIVE" />
|
| 5 |
+
</component>
|
| 6 |
+
<component name="ChangeListManager">
|
| 7 |
+
<list default="true" id="1a49e8ed-9f8f-408e-ba38-b2104c9b2e66" name="Changes" comment="Updated docx ocr now supports reading tables in docx ,also added new library">
|
| 8 |
+
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
|
| 9 |
+
<change beforePath="$PROJECT_DIR$/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/main.py" afterDir="false" />
|
| 10 |
+
<change beforePath="$PROJECT_DIR$/src/ocr_utils.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/ocr_utils.py" afterDir="false" />
|
| 11 |
+
</list>
|
| 12 |
+
<option name="SHOW_DIALOG" value="false" />
|
| 13 |
+
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
| 14 |
+
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
| 15 |
+
<option name="LAST_RESOLUTION" value="IGNORE" />
|
| 16 |
+
</component>
|
| 17 |
+
<component name="Git.Pull.Settings">
|
| 18 |
+
<option name="OPTIONS">
|
| 19 |
+
<set>
|
| 20 |
+
<option value="REBASE" />
|
| 21 |
+
</set>
|
| 22 |
+
</option>
|
| 23 |
+
</component>
|
| 24 |
+
<component name="Git.Settings">
|
| 25 |
+
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
| 26 |
+
</component>
|
| 27 |
+
<component name="MarkdownSettingsMigration">
|
| 28 |
+
<option name="stateVersion" value="1" />
|
| 29 |
+
</component>
|
| 30 |
+
<component name="ProjectColorInfo">{
|
| 31 |
+
"associatedIndex": 3
|
| 32 |
+
}</component>
|
| 33 |
+
<component name="ProjectId" id="2UIePEyXYZkWWMjFkRFaRmF8EFZ" />
|
| 34 |
+
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
|
| 35 |
+
<component name="ProjectViewState">
|
| 36 |
+
<option name="hideEmptyMiddlePackages" value="true" />
|
| 37 |
+
<option name="showLibraryContents" value="true" />
|
| 38 |
+
</component>
|
| 39 |
+
<component name="PropertiesComponent"><![CDATA[{
|
| 40 |
+
"keyToString": {
|
| 41 |
+
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
| 42 |
+
"RunOnceActivity.ShowReadmeOnStart": "true",
|
| 43 |
+
"git-widget-placeholder": "master",
|
| 44 |
+
"ignore.virus.scanning.warn.message": "true",
|
| 45 |
+
"last_opened_file_path": "C:/Users/shubh/Desktop/Major Project/Multi Engine OCR"
|
| 46 |
+
},
|
| 47 |
+
"keyToStringList": {
|
| 48 |
+
"com.intellij.ide.scratch.ScratchImplUtil$2/New Scratch File": [
|
| 49 |
+
"Python"
|
| 50 |
+
]
|
| 51 |
+
}
|
| 52 |
+
}]]></component>
|
| 53 |
+
<component name="RecentsManager">
|
| 54 |
+
<key name="MoveFile.RECENT_KEYS">
|
| 55 |
+
<recent name="C:\Users\shubh\Desktop\Major Project\Multi Engine OCR" />
|
| 56 |
+
<recent name="C:\Users\shubh\Desktop\Major Project\Multi Engine OCR\docs" />
|
| 57 |
+
</key>
|
| 58 |
+
</component>
|
| 59 |
+
<component name="RunAnythingCache">
|
| 60 |
+
<option name="myCommands">
|
| 61 |
+
<command value="pip install -r requirements.txt" />
|
| 62 |
+
</option>
|
| 63 |
+
</component>
|
| 64 |
+
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
| 65 |
+
<component name="TaskManager">
|
| 66 |
+
<task active="true" id="Default" summary="Default task">
|
| 67 |
+
<changelist id="1a49e8ed-9f8f-408e-ba38-b2104c9b2e66" name="Changes" comment="" />
|
| 68 |
+
<created>1692631496020</created>
|
| 69 |
+
<option name="number" value="Default" />
|
| 70 |
+
<option name="presentableId" value="Default" />
|
| 71 |
+
<updated>1692631496020</updated>
|
| 72 |
+
</task>
|
| 73 |
+
<task id="LOCAL-00001" summary="Updated docx ocr now supports reading tables in docx">
|
| 74 |
+
<option name="closed" value="true" />
|
| 75 |
+
<created>1692788848522</created>
|
| 76 |
+
<option name="number" value="00001" />
|
| 77 |
+
<option name="presentableId" value="LOCAL-00001" />
|
| 78 |
+
<option name="project" value="LOCAL" />
|
| 79 |
+
<updated>1692788848522</updated>
|
| 80 |
+
</task>
|
| 81 |
+
<option name="localTasksCounter" value="2" />
|
| 82 |
+
<servers />
|
| 83 |
+
</component>
|
| 84 |
+
<component name="Vcs.Log.Tabs.Properties">
|
| 85 |
+
<option name="TAB_STATES">
|
| 86 |
+
<map>
|
| 87 |
+
<entry key="MAIN">
|
| 88 |
+
<value>
|
| 89 |
+
<State />
|
| 90 |
+
</value>
|
| 91 |
+
</entry>
|
| 92 |
+
</map>
|
| 93 |
+
</option>
|
| 94 |
+
</component>
|
| 95 |
+
<component name="VcsManagerConfiguration">
|
| 96 |
+
<MESSAGE value="Updated docx ocr now supports reading tables in docx" />
|
| 97 |
+
<option name="LAST_COMMIT_MESSAGE" value="Updated docx ocr now supports reading tables in docx" />
|
| 98 |
+
</component>
|
| 99 |
+
</project>
|
Multi Engine OCR/docs/readme.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# OCR Utility
|
| 2 |
+
|
| 3 |
+
Welcome to the OCR Utility, where text extraction from images and documents becomes a breeze! This tool utilizes powerful libraries to help you unveil hidden words and secrets.
|
| 4 |
+
|
| 5 |
+
## Overview
|
| 6 |
+
|
| 7 |
+
The OCR Utility allows you to extract text from various file formats, including images (PNG, JPEG, TIFF) and documents (DOCX, PDF). It employs both Tesseract OCR and EasyOCR for accurate and multilingual text recognition.
|
| 8 |
+
|
| 9 |
+
## Features
|
| 10 |
+
|
| 11 |
+
- Extract text from images (PNG, JPEG, TIFF).
|
| 12 |
+
- Extract text from DOCX files.
|
| 13 |
+
- Extract text from PDF files with the help of Poppler.
|
| 14 |
+
- Utilize Tesseract OCR and EasyOCR for precise text recognition.
|
| 15 |
+
- Enhance images before processing for better results.
|
| 16 |
+
|
| 17 |
+
## Requirements
|
| 18 |
+
|
| 19 |
+
Before you start your magical journey, make sure you have the required ingredients:
|
| 20 |
+
|
| 21 |
+
- **Python 3.6+**: The enchanting language that powers the utility.
|
| 22 |
+
- **numpy**: The essence of number-crunching magic.
|
| 23 |
+
- **Pillow (PIL)**: Your trusty wand for image manipulation.
|
| 24 |
+
- **pytesseract**: The all-seeing eye for text extraction.
|
| 25 |
+
- **easyocr**: The ancient tome of multilingual text deciphering.
|
| 26 |
+
- **python-docx**: The scroll for unveiling secrets from DOCX files.
|
| 27 |
+
- **pdf2image**: The spellbook for converting PDFs into images.
|
| 28 |
+
- **Tesseract OCR**: Download and set the path to the executable.
|
| 29 |
+
- **Poppler for Windows**: Download and remember the path for PDF handling.
|
| 30 |
+
|
| 31 |
+
For detailed instructions on obtaining these ingredients, check out the [Requirements Guide](requirement.md).
|
| 32 |
+
|
| 33 |
+
## Usage
|
| 34 |
+
|
| 35 |
+
1. Clone the repository to your local machine.
|
| 36 |
+
2. Install the required Python packages using `pip install -r requirements.txt`.
|
| 37 |
+
3. Follow the steps in the Requirements Guide to set up Tesseract OCR and Poppler for Windows.
|
| 38 |
+
4. Run the `main.py` script and follow the prompts to extract text from your desired file.
|
| 39 |
+
|
| 40 |
+
## Contribution
|
| 41 |
+
|
| 42 |
+
Contributions are welcome! If you find issues or have ideas for improvements, feel free to open an issue or submit a pull request.
|
| 43 |
+
|
| 44 |
+
## Credits
|
| 45 |
+
|
| 46 |
+
This utility was created by [Your Name] as a project inspired by the magic of text extraction.
|
| 47 |
+
|
| 48 |
+
## License
|
| 49 |
+
|
| 50 |
+
This project is licensed under the [MIT License](LICENSE).
|
| 51 |
+
|
| 52 |
+
---
|
Multi Engine OCR/docs/requirement.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# OCR Utility Requirements
|
| 2 |
+
|
| 3 |
+
Welcome to the OCR Utility! Before you embark on your journey of text extraction wizardry, let's make sure you have everything you need. This list is like assembling your spellbook for enchanting text from images and documents.
|
| 4 |
+
|
| 5 |
+
## Required Libraries
|
| 6 |
+
|
| 7 |
+
You'll need some magical libraries to cast your OCR spells. These libraries help with image manipulation, text recognition, and more. Don't worry about the version numbers - we've got the latest spells prepared for you!
|
| 8 |
+
|
| 9 |
+
- **numpy**: The essence of number-crunching magic.
|
| 10 |
+
- **PIL (Pillow)**: Your trusty wand for image manipulation.
|
| 11 |
+
- **pytesseract**: The all-seeing eye for text extraction.
|
| 12 |
+
- **easyocr**: The ancient tome of multilingual text deciphering.
|
| 13 |
+
- **python-docx**: The scroll for unveiling secrets from DOCX files.
|
| 14 |
+
- **pdf2image**: The spellbook for converting PDFs into images.
|
| 15 |
+
|
| 16 |
+
## Obtaining the Magic Ingredients
|
| 17 |
+
|
| 18 |
+
### Tesseract OCR
|
| 19 |
+
|
| 20 |
+
Before you dive into the mystical world of OCR, you need to conjure Tesseract OCR into your realm. It's like getting your very own Oracle.
|
| 21 |
+
|
| 22 |
+
1. **Download**: Visit the [Tesseract OCR Download Page](https://github.com/tesseract-ocr/tesseract) and grab the executable that matches your operating system.
|
| 23 |
+
|
| 24 |
+
2. **Set the Path**: Add the path to Tesseract executable (the folder containing `tesseract.exe`) to your system's environment variables. This lets your spells reach Tesseract from anywhere.
|
| 25 |
+
|
| 26 |
+
### Poppler for Windows
|
| 27 |
+
|
| 28 |
+
To handle enchanted PDFs, you need Poppler for Windows. It's like getting a magical key to unlock the secrets of PDF documents.
|
| 29 |
+
|
| 30 |
+
1. **Download**: Go to the [Poppler for Windows Website](http://blog.alivate.com.au/poppler-windows/) and download the version that suits your system.
|
| 31 |
+
|
| 32 |
+
2. **Install**: Extract the downloaded file and place it in a safe and magical location on your system.
|
| 33 |
+
|
| 34 |
+
3. **Poppler Path**: Remember the location where you placed Poppler? You'll need to use that path later when you're casting your PDF spells.
|
| 35 |
+
|
| 36 |
+
## You're Ready to Cast Spells!
|
| 37 |
+
|
| 38 |
+
With the required libraries and mystical tools in your grasp, you're ready to weave your incantations and unveil the hidden words within images and documents.
|
| 39 |
+
|
| 40 |
+
Remember, the magic lies in your code, but these requirements and tools are your trusty allies on your OCR quest. May your text be extracted flawlessly, and your documents reveal their secrets!
|
| 41 |
+
|
| 42 |
+
> Note: If you're a Mac or Linux sorcerer, you'll have a slightly different set of spells, but the journey remains just as magical!
|
| 43 |
+
|
| 44 |
+
Now, go forth and conquer the realm of text extraction!
|
Multi Engine OCR/main.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datetime
|
| 2 |
+
import os
|
| 3 |
+
import tkinter as tk
|
| 4 |
+
from tkinter import filedialog
|
| 5 |
+
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
from src.ocr_utils import process_image_tesseract, process_image_easyocr, \
|
| 9 |
+
extract_text_from_docx, process_pdf
|
| 10 |
+
|
| 11 |
+
if __name__ == "__main__":
|
| 12 |
+
root = tk.Tk()
|
| 13 |
+
root.withdraw()
|
| 14 |
+
|
| 15 |
+
file_path = filedialog.askopenfilename(title="Select Input File")
|
| 16 |
+
|
| 17 |
+
if not file_path:
|
| 18 |
+
print("No file selected. Exiting...")
|
| 19 |
+
else:
|
| 20 |
+
output_folder = "output_files"
|
| 21 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 22 |
+
|
| 23 |
+
extracted_text = ""
|
| 24 |
+
|
| 25 |
+
if file_path.lower().endswith('.docx'):
|
| 26 |
+
extracted_text = extract_text_from_docx(file_path)
|
| 27 |
+
elif file_path.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff')):
|
| 28 |
+
image = Image.open(file_path)
|
| 29 |
+
extracted_text = process_image_tesseract(image) + process_image_easyocr(image)
|
| 30 |
+
if extracted_text:
|
| 31 |
+
print("Extracted Text:\n", extracted_text)
|
| 32 |
+
elif file_path.lower().endswith('.pdf'):
|
| 33 |
+
try:
|
| 34 |
+
poppler_path = r"C:\Program Files (x86)\poppler-23.08.0\Library\bin"
|
| 35 |
+
extracted_text = process_pdf(file_path, poppler_path, output_folder)
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"An error occurred during PDF processing: {e}")
|
| 38 |
+
else:
|
| 39 |
+
print(f"Unsupported file format: {file_path}")
|
| 40 |
+
|
| 41 |
+
if extracted_text:
|
| 42 |
+
try:
|
| 43 |
+
current_datetime = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 44 |
+
txt_filename = f"extracted_text_{current_datetime}.txt"
|
| 45 |
+
with open(os.path.join(output_folder, txt_filename), "w", encoding="utf-8") as txt_file:
|
| 46 |
+
txt_file.write(extracted_text)
|
| 47 |
+
print("Extraction completed. Text saved as:", txt_filename)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print("Error while saving extracted content:", str(e))
|
Multi Engine OCR/requirment.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pip install numpy
|
| 2 |
+
pip install pillow
|
| 3 |
+
pip install pytesseract
|
| 4 |
+
pip install easyocr
|
| 5 |
+
pip install python-docx
|
| 6 |
+
pip install pdf2image
|
| 7 |
+
pip install docx2txt
|
Multi Engine OCR/src/__pycache__/ocr_utils.cpython-310.pyc
ADDED
|
Binary file (3.07 kB). View file
|
|
|
Multi Engine OCR/src/ocr_utils.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import docx2txt
|
| 3 |
+
import easyocr
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pytesseract
|
| 6 |
+
from PIL import ImageEnhance, ImageFilter
|
| 7 |
+
from docx import Document # Import the Document class from docx module
|
| 8 |
+
from pdf2image import convert_from_path
|
| 9 |
+
|
| 10 |
+
# Path to Tesseract executable
|
| 11 |
+
pytesseract.pytesseract.tesseract_cmd = r"D:\Tesseract-OCR\tesseract.exe"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def preprocess_image(image):
|
| 15 |
+
"""Preprocess an image (enhance contrast and sharpness, denoise)"""
|
| 16 |
+
image = image.convert("RGB")
|
| 17 |
+
enhancer = ImageEnhance.Contrast(image)
|
| 18 |
+
image = enhancer.enhance(2.0) # Increase contrast
|
| 19 |
+
enhancer = ImageEnhance.Sharpness(image)
|
| 20 |
+
image = enhancer.enhance(2.0) # Increase sharpness
|
| 21 |
+
image = image.filter(ImageFilter.GaussianBlur(radius=2)) # Apply Gaussian blur for denoising
|
| 22 |
+
return image
|
| 23 |
+
def process_image_tesseract(image):
|
| 24 |
+
"""Process an image using Tesseract OCR"""
|
| 25 |
+
try:
|
| 26 |
+
preprocessed_image = preprocess_image(image)
|
| 27 |
+
text = pytesseract.image_to_string(preprocessed_image)
|
| 28 |
+
return text
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"An error occurred during Tesseract OCR processing: {e}")
|
| 31 |
+
return ""
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def process_image_easyocr(image):
|
| 35 |
+
"""Process an image using EasyOCR"""
|
| 36 |
+
try:
|
| 37 |
+
preprocessed_image = preprocess_image(image)
|
| 38 |
+
image_np = np.array(preprocessed_image)
|
| 39 |
+
reader = easyocr.Reader(lang_list=['en', 'hi', 'mr'])
|
| 40 |
+
results = reader.readtext(image_np)
|
| 41 |
+
text = " ".join([result[1] for result in results]) # Concatenate recognized text
|
| 42 |
+
return text
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"An error occurred during EasyOCR processing: {e}")
|
| 45 |
+
return ""
|
| 46 |
+
|
| 47 |
+
def extract_text_from_docx(docx_path):
|
| 48 |
+
"""Extract text from DOCX file"""
|
| 49 |
+
try:
|
| 50 |
+
extracted_text = ""
|
| 51 |
+
|
| 52 |
+
# Extract text from paragraphs
|
| 53 |
+
text = docx2txt.process(docx_path)
|
| 54 |
+
extracted_text += text
|
| 55 |
+
|
| 56 |
+
# Extract text from tables
|
| 57 |
+
doc = Document(docx_path)
|
| 58 |
+
for table in doc.tables:
|
| 59 |
+
table_text = extract_table_text(table)
|
| 60 |
+
extracted_text += table_text + "\n"
|
| 61 |
+
|
| 62 |
+
return extracted_text
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print(f"An error occurred during DOCX processing: {e}")
|
| 65 |
+
return ""
|
| 66 |
+
|
| 67 |
+
def extract_table_text(table):
|
| 68 |
+
"""Extract text from a table in DOCX"""
|
| 69 |
+
table_text = ""
|
| 70 |
+
for row in table.rows:
|
| 71 |
+
for cell in row.cells:
|
| 72 |
+
cell_text = ""
|
| 73 |
+
for paragraph in cell.paragraphs:
|
| 74 |
+
cell_text += paragraph.text + " "
|
| 75 |
+
table_text += cell_text.strip() + "\t"
|
| 76 |
+
table_text += "\n"
|
| 77 |
+
return table_text
|
| 78 |
+
|
| 79 |
+
def process_pdf(pdf_path, poppler_path, output_folder):
|
| 80 |
+
"""Process a PDF file"""
|
| 81 |
+
try:
|
| 82 |
+
pdf_filename = os.path.basename(pdf_path)
|
| 83 |
+
images = convert_from_path(pdf_path, poppler_path=poppler_path)
|
| 84 |
+
extracted_text = ""
|
| 85 |
+
for i, image in enumerate(images):
|
| 86 |
+
text = process_image_tesseract(image) + process_image_easyocr(image)
|
| 87 |
+
extracted_text += text + "\n"
|
| 88 |
+
return extracted_text
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"An error occurred during PDF processing: {e}")
|
| 91 |
+
return ""
|