tarujain8's picture
feat: implement OCR service using Tesseract for image text extraction
03fb27e
Raw
History Blame Contribute Delete
786 Bytes
import pytesseract
from PIL import Image
from backend.core.logger import get_logger
logger = get_logger(__name__)
def extract_text_from_images(image_paths):
texts = []
logger.info(f"OCR STARTED FOR {len(image_paths)} IMAGES")
for path in image_paths:
try:
logger.info(f"OPENING IMAGE: {path}")
img = Image.open(path)
logger.info(f"RUNNING OCR ON: {path}")
text = pytesseract.image_to_string(img)
logger.info(f"OCR RESULT:\n{text[:1000]}")
texts.append(text)
except Exception as e:
logger.exception(f"OCR FAILED: {e}")
texts.append("")
final_text = "\n".join(texts)
logger.info(f"FINAL OCR TEXT:\n{final_text[:3000]}")
return final_text