|
| 1 | +import os |
| 2 | +import cv2 |
| 3 | +import pytesseract |
| 4 | +import numpy as np |
| 5 | +from tkinter import Tk, filedialog |
| 6 | +from PIL import Image, ImageEnhance, ImageFilter |
| 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(image_path, extracted_text): |
| 62 | + base_name = os.path.splitext(image_path)[0] |
| 63 | + text_file_path = base_name + "_extracted_text.txt" |
| 64 | + |
| 65 | + with open(text_file_path, 'w', encoding='utf-8') as file: |
| 66 | + file.write(extracted_text) |
| 67 | + |
| 68 | + print(f"Extracted text saved to {text_file_path}") |
| 69 | + |
| 70 | +def main(): |
| 71 | + # Hide the root window |
| 72 | + Tk().withdraw() |
| 73 | + |
| 74 | + # Get the directory of the current script |
| 75 | + script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 76 | + screenshots_smart_dir = os.path.join(script_dir, 'screenshots_smart') |
| 77 | + |
| 78 | + if os.path.exists(screenshots_smart_dir): |
| 79 | + # If screenshots_smart folder exists, ask the user to select an image from there |
| 80 | + image_path = filedialog.askopenfilename(initialdir=screenshots_smart_dir, title="Select a Screenshot", filetypes=[("Image Files", ".png;.jpg;.jpeg;.bmp;*.tiff")]) |
| 81 | + else: |
| 82 | + print(f"'screenshots_smart' folder not found in {script_dir}. Please select an image file.") |
| 83 | + # Ask user to add a photo |
| 84 | + image_path = filedialog.askopenfilename(title="Select a Screenshot", filetypes=[("Image Files", ".png;.jpg;.jpeg;.bmp;*.tiff")]) |
| 85 | + |
| 86 | + # If no file is selected, search for images on the Desktop |
| 87 | + if not image_path: |
| 88 | + desktop_dir = os.path.join(os.path.expanduser("~"), "Desktop") |
| 89 | + image_files = [f for f in os.listdir(desktop_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.tiff'))] |
| 90 | + |
| 91 | + if image_files: |
| 92 | + image_path = os.path.join(desktop_dir, image_files[0]) |
| 93 | + print(f"Using image found on Desktop: {image_path}") |
| 94 | + else: |
| 95 | + print("No images found on Desktop.") |
| 96 | + return |
| 97 | + |
| 98 | + # Check if the user selected a file |
| 99 | + if not image_path: |
| 100 | + print("No file selected.") |
| 101 | + return |
| 102 | + |
| 103 | + # Preprocess the image |
| 104 | + image = preprocess_image(image_path) |
| 105 | + |
| 106 | + # Segment the image into sections |
| 107 | + sections = segment_image(image) |
| 108 | + |
| 109 | + # Extract text from each section and aggregate it |
| 110 | + extracted_text = "" |
| 111 | + for section in sections: |
| 112 | + text = extract_text_from_section(image, section) |
| 113 | + if text: |
| 114 | + extracted_text += text + "\n" + "-" * 40 + "\n" |
| 115 | + |
| 116 | + # Print and save the extracted text |
| 117 | + if extracted_text: |
| 118 | + print("Extracted Text:") |
| 119 | + print(extracted_text) |
| 120 | + save_extracted_text(image_path, extracted_text) |
| 121 | + else: |
| 122 | + print("No text extracted.") |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + main() |
0 commit comments