yamanavijayavardhan's picture
fix circular call 3
237de6f
raw
history blame
4.7 kB
import cv2
import os
import tempfile
import logging
import numpy as np
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from utils import notification_queue, log_print
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Import HTR modules
from HTR.word import convert_image
from HTR.strike import struck_images
from HTR.hcr import text
from HTR.spell_and_gramer_check import spell_grammer
def preprocess_image(img):
"""Preprocess image to improve text detection"""
try:
# Convert to grayscale
notification_queue.put({
"type": "info",
"message": "Converting image to grayscale..."
})
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply adaptive thresholding
notification_queue.put({
"type": "info",
"message": "Applying adaptive thresholding..."
})
binary = cv2.adaptiveThreshold(
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, 11, 2
)
# Denoise
notification_queue.put({
"type": "info",
"message": "Denoising image..."
})
denoised = cv2.fastNlMeansDenoising(binary)
# Convert back to BGR
return cv2.cvtColor(denoised, cv2.COLOR_GRAY2BGR)
except Exception as e:
error_msg = str(e)
notification_queue.put({
"type": "error",
"message": f"Error in image preprocessing: {error_msg}"
})
return img
def extract_text_from_image(img_path):
try:
# Ensure the image exists
if not os.path.exists(img_path):
notification_queue.put({
"type": "error",
"message": f"Image file not found: {img_path}"
})
return ""
# Read the image
notification_queue.put({
"type": "info",
"message": f"Reading image: {os.path.basename(img_path)}"
})
img = cv2.imread(img_path)
if img is None:
notification_queue.put({
"type": "error",
"message": f"Failed to read image: {img_path}"
})
return ""
# Log image properties
notification_queue.put({
"type": "info",
"message": f"Processing image: {os.path.basename(img_path)}\nImage shape: {img.shape}\nImage type: {img.dtype}"
})
# Process the image
notification_queue.put({
"type": "info",
"message": "Converting image to text regions..."
})
imgs = convert_image(img)
if not imgs:
notification_queue.put({
"type": "warning",
"message": "No text regions detected in image. Processing whole image..."
})
# Try processing the whole image as one region
temp_path = os.path.join(tempfile.gettempdir(), 'whole_image.png')
cv2.imwrite(temp_path, img)
imgs = [temp_path]
notification_queue.put({
"type": "info",
"message": f"Found {len(imgs)} text regions"
})
notification_queue.put({
"type": "info",
"message": "Processing text regions..."
})
images_path = struck_images(imgs)
if not images_path:
notification_queue.put({
"type": "error",
"message": "No valid text regions after processing"
})
return ""
notification_queue.put({
"type": "info",
"message": "Extracting text from regions..."
})
t = text(images_path)
if not t:
notification_queue.put({
"type": "error",
"message": "No text could be extracted from image"
})
return ""
notification_queue.put({
"type": "info",
"message": "Performing spell checking..."
})
t = spell_grammer(t)
notification_queue.put({
"type": "success",
"message": "Text extraction complete",
"data": {
"extracted_text": t
}
})
return t
except Exception as e:
error_msg = str(e)
notification_queue.put({
"type": "error",
"message": f"Error in text extraction: {error_msg}"
})
return ""
# extract_text_from_image("ans_image/1.jpg")