Reputation: 1
I'm want to extract text from blur image [blur text image] 1 using below code. I'm new to this and I have to complete this task. Can anyone suggest better code for me?
import cv2 import pytesseract import numpy as np from PIL import Image, ImageEnhance, ImageFilter # If using Windows, set the Tesseract executable path: # pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' def deskew(image): # Identify coordinates of non-zero pixels (text) coords = np.column_stack(np.where(image > 0)) # Compute angle of rotation based on a minimum-area rectangle angle = cv2.minAreaRect(coords)[-1] if angle < -45: angle = -(90 + angle) else: angle = -angle (h, w) = image.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, angle, 1.0) rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) return rotated def sharpen_image(image_path): img = cv2.imread(image_path) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Convert to grayscale (often helps) # # Define the sharpening kernel (adjust values as needed) kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]]) sharpened = cv2.filter2D(gray, -1, kernel) # Apply the kernel# Convert back to PIL Image for pytesseract (if needed) sharpened_pil = Image.fromarray(sharpened) return sharpened_pil # Load the image img = cv2.imread('image.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.GaussianBlur # Apply noise reduction denoised = cv2.fastNlMeansDenoising(gray, h=30, templateWindowSize=7, searchWindowSize=21) # Apply thresholding _, thresh = cv2.threshold(img, 20, 255, cv2.THRESH_BINARY) # Apply dilation and erosion to remove noise kernel = np.ones((1, 1), np.uint8) processed = cv2.dilate(thresh, kernel, iterations=1) # processed = cv2.erode(processed, kernel, iterations=1) # # Apply image scaling # processed = cv2.resize(processed, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC) # Optionally, display the processed image for debugging cv2.imshow("Processed", processed) cv2.waitKey(0) cv2.destroyAllWindows() # Use Tesseract with custom configuration: # --oem 3: Use the default neural network LSTM engine. # --psm 6: Assume a single uniform block of text. # -l eng: Set language to English. pytesseract.pytesseract.tesseract_cmd = 'C:/Users/dsammengi.ctr/AppData/Local/Programs/Tesseract-OCR/tesseract.exe' custom_config = r'--oem 3 --psm 6 -l eng' processed = sharpen_image('image.png') extracted_text = pytesseract.image_to_string(processed, config=custom_config) print("Extracted Text:") print(extracted_text)
Upvotes: -5
Views: 48