Download Multi Engine OCR/main.py from SAM69/Multi_Engine_OCR: direct link, hf CLI and curl.
- Browser
- Download file 1.92 kB
-
https://huggingface.co/SAM69/Multi_Engine_OCR/resolve/main/Multi%20Engine%20OCR/main.py
- Command line
-
hf download 'hf://SAM69/Multi_Engine_OCR/Multi Engine OCR/main.py'
-
curl -L -o main.py https://huggingface.co/SAM69/Multi_Engine_OCR/resolve/main/Multi%20Engine%20OCR/main.py
1.92 kB
| import datetime | |
| import os | |
| import tkinter as tk | |
| from tkinter import filedialog | |
| from PIL import Image | |
| from src.ocr_utils import process_image_tesseract, process_image_easyocr, \ | |
| extract_text_from_docx, process_pdf | |
| if __name__ == "__main__": | |
| root = tk.Tk() | |
| root.withdraw() | |
| file_path = filedialog.askopenfilename(title="Select Input File") | |
| if not file_path: | |
| print("No file selected. Exiting...") | |
| else: | |
| output_folder = "output_files" | |
| os.makedirs(output_folder, exist_ok=True) | |
| extracted_text = "" | |
| if file_path.lower().endswith('.docx'): | |
| extracted_text = extract_text_from_docx(file_path) | |
| elif file_path.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff')): | |
| image = Image.open(file_path) | |
| extracted_text = process_image_tesseract(image) + process_image_easyocr(image) | |
| if extracted_text: | |
| print("Extracted Text:\n", extracted_text) | |
| elif file_path.lower().endswith('.pdf'): | |
| try: | |
| poppler_path = r"C:\Program Files (x86)\poppler-23.08.0\Library\bin" | |
| extracted_text = process_pdf(file_path, poppler_path, output_folder) | |
| except Exception as e: | |
| print(f"An error occurred during PDF processing: {e}") | |
| else: | |
| print(f"Unsupported file format: {file_path}") | |
| if extracted_text: | |
| try: | |
| current_datetime = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") | |
| txt_filename = f"extracted_text_{current_datetime}.txt" | |
| with open(os.path.join(output_folder, txt_filename), "w", encoding="utf-8") as txt_file: | |
| txt_file.write(extracted_text) | |
| print("Extraction completed. Text saved as:", txt_filename) | |
| except Exception as e: | |
| print("Error while saving extracted content:", str(e)) |