Reputation: 1
I'm trying to write code that receives an image, processes it, and tries to locate a specific word in the code (Wave) and also prints all the text it contains, but for some reason my code fails to process the image properly and returns incorrect text. Attaching the code and the image I'm trying to process the text from:
The image I am tring to read:
import cv2
import pytesseract
import re
from PIL import Image
import numpy as np
# Set Tesseract path (Windows users)
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
def cut_photo(image_path):
"""Crop the image to focus on a specific region."""
im = Image.open(image_path)
# Get image size
width, height = im.size
# Define crop area (adjust based on image structure)
left = width * 0.2
top = 50
right = width * 0.8
bottom = height * 0.1
# Crop the image
im_cropped = im.crop((left, top, right, bottom))
# Convert PIL image to OpenCV format (NumPy array)
return cv2.cvtColor(np.array(im_cropped), cv2.COLOR_RGB2BGR)
def find_word_in_image(image_path, target_word):
"""Find a specific word in an image using OCR."""
# Load and crop the image
image = cut_photo(image_path)
# Convert the image to HLS color space
hls = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)
# Define lower and upper limits for the white color in HLS
white_lo = np.array([0, 200, 0]) # Lower limit for white (high lightness, low saturation)
white_hi = np.array([179, 255, 255]) # Upper limit for white (high lightness, high saturation)
# Mask image to only select "white"
mask = cv2.inRange(hls, white_lo, white_hi)
# Apply the mask to the original image
white_masked_image = cv2.bitwise_and(image, image, mask=mask)
# Perform OCR
recognized_text = pytesseract.image_to_string(white_masked_image, lang='eng', config='--psm 6')
# Print recognized text (for debugging)
print(f"OCR Output:\n{recognized_text}")
# Search for the target word (case insensitive)
if re.search(rf"\b{re.escape(target_word)}\b", recognized_text, re.IGNORECASE):
print(f"✅ Found '{target_word}' in the image!")
else:
print(f"❌ '{target_word}' NOT found in the image.")
# Show processed image
cv2.imshow("Processed Image", white_masked_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Example Usage
image_path = r"C:\python_projects\macro\photos\map\above.png"
target_word = "Wave" # Change this to the word you're searching for
find_word_in_image(image_path, target_word)
I tried removing objects that are not white (the color of the words) from the image to remove objects that would interfere with the code processing the image but it still doesn't work. I would appreciate help on how to improve my code
Upvotes: 0
Views: 40