File size: 786 Bytes
03fb27e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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