File size: 876 Bytes
d5c018a
 
 
 
 
 
 
 
 
 
 
 
571e0cd
 
d5c018a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
import os
from pdf2image import convert_from_path 
import pytesseract
from langchain_core.documents import Document
from script.errors import IngestionError


def extract_text_via_ocr(pdf_path: str) -> list[Document]:
  POPPLER_PATH = os.getenv("POPPLER_PATH", "/usr/bin")

  images = convert_from_path(
     pdf_path,
     poppler_path = POPPLER_PATH

  )
    
  documents = []

  for i, img in enumerate(images):
      text = pytesseract.image_to_string(img)
      if text.strip():
          documents.append(
              Document(
                  page_content=text,
                  metadata={
                      "page": i,
                      "source": "ocr"
                  }
              )
          )

  if not documents:
        raise IngestionError(
            code="OCR_FAILED",
            message="OCR produced no text"
        )

  return documents