paper-qa-rag / script /ocr.py
whosouravsharma's picture
Unable to get page count. Is poppler installed and in PATH fixed
571e0cd
Raw History Blame Contribute Delete
876 Bytes
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