|
| 1 | +import os |
| 2 | +import cv2 |
| 3 | +import pytesseract |
| 4 | +import numpy as np |
| 5 | +from tkinter import Tk, filedialog, Text, Button, Label, Scrollbar, RIGHT, Y, BOTH, END, Frame, Entry, messagebox |
| 6 | +from PIL import Image, ImageEnhance, ImageFilter, ImageTk |
| 7 | + |
| 8 | +# Manually specify the path to the Tesseract executable |
| 9 | +pytesseract.pytesseract.tesseract_cmd = r'C:\Users\kulitesh\Tesseract-OCR\Tesseract-OCR\tesseract.exe' |
| 10 | + |
| 11 | +def preprocess_image(image_path): |
| 12 | + # Open the image using PIL |
| 13 | + image = Image.open(image_path) |
| 14 | + |
| 15 | + # Convert to grayscale |
| 16 | + image = image.convert('L') |
| 17 | + |
| 18 | + # Increase contrast |
| 19 | + enhancer = ImageEnhance.Contrast(image) |
| 20 | + image = enhancer.enhance(2) |
| 21 | + |
| 22 | + # Apply a median filter to remove noise |
| 23 | + image = image.filter(ImageFilter.MedianFilter(size=3)) |
| 24 | + |
| 25 | + # Convert the image to a NumPy array for OpenCV processing |
| 26 | + image = cv2.cvtColor(np.array(image), cv2.COLOR_GRAY2BGR) |
| 27 | + |
| 28 | + return image |
| 29 | + |
| 30 | +def segment_image(image): |
| 31 | + # Convert to grayscale |
| 32 | + gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
| 33 | + |
| 34 | + # Apply adaptive thresholding |
| 35 | + binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) |
| 36 | + |
| 37 | + # Find contours |
| 38 | + contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
| 39 | + |
| 40 | + # Sort contours by area (largest first) |
| 41 | + contours = sorted(contours, key=cv2.contourArea, reverse=True) |
| 42 | + |
| 43 | + sections = [] |
| 44 | + for contour in contours: |
| 45 | + x, y, w, h = cv2.boundingRect(contour) |
| 46 | + if w > 50 and h > 50: # Filter out small contours |
| 47 | + sections.append((x, y, w, h)) |
| 48 | + |
| 49 | + return sections |
| 50 | + |
| 51 | +def extract_text_from_section(image, section): |
| 52 | + x, y, w, h = section |
| 53 | + roi = image[y:y+h, x:x+w] |
| 54 | + |
| 55 | + # Use Tesseract to extract text with custom configuration |
| 56 | + custom_config = r'--oem 3 --psm 6 -l eng' |
| 57 | + text = pytesseract.image_to_string(roi, config=custom_config) |
| 58 | + |
| 59 | + return text |
| 60 | + |
| 61 | +def save_extracted_text(file_path, extracted_text): |
| 62 | + with open(file_path, 'w', encoding='utf-8') as file: |
| 63 | + file.write(extracted_text) |
| 64 | + |
| 65 | + messagebox.showinfo("Saved", f"Extracted text saved to {file_path}") |
| 66 | + |
| 67 | +def gui_main(): |
| 68 | + def upload_image(): |
| 69 | + file_path = filedialog.askopenfilename( |
| 70 | + initialdir=os.path.join(os.path.expanduser("~"), "Desktop"), |
| 71 | + title="Select an Image from Desktop", |
| 72 | + filetypes=[("Image files", "*.jpg *.jpeg *.png *.bmp *.tiff")] |
| 73 | + ) |
| 74 | + |
| 75 | + if file_path: |
| 76 | + try: |
| 77 | + image = preprocess_image(file_path) |
| 78 | + sections = segment_image(image) |
| 79 | + extracted_text = "" |
| 80 | + for section in sections: |
| 81 | + text = extract_text_from_section(image, section) |
| 82 | + if text: |
| 83 | + extracted_text += text + "\n" + "-" * 40 + "\n" |
| 84 | + |
| 85 | + if extracted_text: |
| 86 | + text_area.delete('1.0', END) |
| 87 | + text_area.insert(END, extracted_text) |
| 88 | + display_image(file_path) |
| 89 | + else: |
| 90 | + text_area.delete('1.0', END) |
| 91 | + text_area.insert(END, "No text extracted.") |
| 92 | + except Exception as e: |
| 93 | + text_area.delete('1.0', END) |
| 94 | + text_area.insert(END, f"Error: {str(e)}") |
| 95 | + |
| 96 | + def display_image(image_path): |
| 97 | + img = Image.open(image_path) |
| 98 | + img.thumbnail((250, 250)) |
| 99 | + img = ImageTk.PhotoImage(img) |
| 100 | + panel.config(image=img) |
| 101 | + panel.image = img |
| 102 | + |
| 103 | + def save_text(): |
| 104 | + file_path = filedialog.asksaveasfilename( |
| 105 | + defaultextension=".txt", |
| 106 | + filetypes=[("Text files", ".txt"), ("All files", ".*")] |
| 107 | + ) |
| 108 | + if file_path: |
| 109 | + extracted_text = text_area.get("1.0", END) |
| 110 | + save_extracted_text(file_path, extracted_text) |
| 111 | + |
| 112 | + root = Tk() |
| 113 | + root.title("OCR Application") |
| 114 | + root.configure(bg='black') |
| 115 | + |
| 116 | + frame = Frame(root, bg='black') |
| 117 | + frame.pack(pady=10) |
| 118 | + |
| 119 | + label = Label(frame, text="Upload an Image for OCR", bg='black', fg='white') |
| 120 | + label.pack(pady=10) |
| 121 | + |
| 122 | + upload_button = Button(frame, text="Upload Image", command=upload_image, bg='white', fg='black') |
| 123 | + upload_button.pack(pady=5) |
| 124 | + |
| 125 | + save_button = Button(frame, text="Save Extracted Text", command=save_text, bg='white', fg='black') |
| 126 | + save_button.pack(pady=5) |
| 127 | + |
| 128 | + text_frame = Frame(root) |
| 129 | + text_frame.pack(fill=BOTH, expand=True, padx=10, pady=10) |
| 130 | + |
| 131 | + scrollbar = Scrollbar(text_frame) |
| 132 | + scrollbar.pack(side=RIGHT, fill=Y) |
| 133 | + |
| 134 | + text_area = Text(text_frame, wrap='word', yscrollcommand=scrollbar.set, height=20, bg='black', fg='white', insertbackground='white') |
| 135 | + text_area.pack(fill=BOTH, expand=True) |
| 136 | + scrollbar.config(command=text_area.yview) |
| 137 | + |
| 138 | + panel = Label(frame, bg='black') |
| 139 | + panel.pack(pady=10) |
| 140 | + |
| 141 | + root.mainloop() |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + gui_main() |
0 commit comments