|
|
import os |
|
|
import io |
|
|
import json |
|
|
from typing import List, Tuple, Dict, Any |
|
|
|
|
|
import fitz |
|
|
from PIL import Image |
|
|
import gradio as gr |
|
|
|
|
|
|
|
|
|
|
|
_ocr_model = None |
|
|
|
|
|
|
|
|
def get_ocr_model(lang: str = "en"): |
|
|
global _ocr_model |
|
|
if _ocr_model is not None: |
|
|
return _ocr_model |
|
|
|
|
|
|
|
|
|
|
|
from paddleocr import PaddleOCR |
|
|
|
|
|
_ocr_model = PaddleOCR(use_angle_cls=True, lang=lang, show_log=False) |
|
|
return _ocr_model |
|
|
|
|
|
|
|
|
def pdf_page_to_image(pdf_doc: fitz.Document, page_index: int, dpi: int = 170) -> Image.Image: |
|
|
page = pdf_doc.load_page(page_index) |
|
|
zoom = dpi / 72.0 |
|
|
mat = fitz.Matrix(zoom, zoom) |
|
|
pix = page.get_pixmap(matrix=mat, alpha=False) |
|
|
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) |
|
|
return img |
|
|
|
|
|
|
|
|
def run_paddle_ocr_on_image(image: Image.Image, lang: str = "en") -> Tuple[str, List[Dict[str, Any]]]: |
|
|
ocr = get_ocr_model(lang=lang) |
|
|
|
|
|
import numpy as np |
|
|
|
|
|
img_np = np.array(image) |
|
|
result = ocr.ocr(img_np, cls=True) |
|
|
|
|
|
lines: List[str] = [] |
|
|
items: List[Dict[str, Any]] = [] |
|
|
|
|
|
|
|
|
for page_result in result: |
|
|
if page_result is None: |
|
|
continue |
|
|
for det in page_result: |
|
|
box = det[0] |
|
|
text = det[1][0] |
|
|
conf = float(det[1][1]) |
|
|
lines.append(text) |
|
|
items.append({"bbox": box, "text": text, "confidence": conf}) |
|
|
|
|
|
return "\n".join(lines), items |
|
|
|
|
|
|
|
|
def extract_text_from_pdf(file_obj, dpi: int = 170, max_pages: int | None = None, lang: str = "en") -> Tuple[str, str]: |
|
|
""" |
|
|
Returns combined text and a JSON string with per-page OCR results. |
|
|
""" |
|
|
if file_obj is None: |
|
|
return "", json.dumps({"pages": []}, ensure_ascii=False) |
|
|
|
|
|
|
|
|
pdf_path = file_obj if isinstance(file_obj, str) else getattr(file_obj, "name", None) |
|
|
if pdf_path is None or not os.path.exists(pdf_path): |
|
|
|
|
|
file_bytes = file_obj.read() if hasattr(file_obj, "read") else None |
|
|
if not file_bytes: |
|
|
return "", json.dumps({"pages": []}, ensure_ascii=False) |
|
|
pdf_doc = fitz.open(stream=file_bytes, filetype="pdf") |
|
|
else: |
|
|
pdf_doc = fitz.open(pdf_path) |
|
|
|
|
|
try: |
|
|
num_pages = pdf_doc.page_count |
|
|
if max_pages is not None: |
|
|
num_pages = min(num_pages, max_pages) |
|
|
|
|
|
all_text_lines: List[str] = [] |
|
|
pages_payload: List[Dict[str, Any]] = [] |
|
|
|
|
|
for page_index in range(num_pages): |
|
|
image = pdf_page_to_image(pdf_doc, page_index, dpi=dpi) |
|
|
page_text, page_items = run_paddle_ocr_on_image(image, lang=lang) |
|
|
|
|
|
all_text_lines.append(page_text) |
|
|
pages_payload.append({ |
|
|
"page": page_index + 1, |
|
|
"items": page_items, |
|
|
}) |
|
|
|
|
|
combined_text = "\n\n".join([t for t in all_text_lines if t]) |
|
|
json_payload = json.dumps({"pages": pages_payload}, ensure_ascii=False) |
|
|
|
|
|
return combined_text, json_payload |
|
|
finally: |
|
|
pdf_doc.close() |
|
|
|
|
|
|
|
|
def gradio_predict(pdf_file): |
|
|
|
|
|
text, _ = extract_text_from_pdf(pdf_file, dpi=300, max_pages=None, lang="en") |
|
|
return text |
|
|
|
|
|
|
|
|
with gr.Blocks(title="PDF OCR with PaddleOCR + PyMuPDF") as demo: |
|
|
gr.Markdown(""" |
|
|
# PDF OCR (PaddleOCR + PyMuPDF) |
|
|
Upload a PDF to extract text using OCR. The app renders pages with PyMuPDF at a high DPI and uses PaddleOCR for recognition. |
|
|
""") |
|
|
|
|
|
pdf_input = gr.File(label="PDF", file_types=[".pdf"], file_count="single") |
|
|
text_output = gr.Textbox(label="Extracted Text", lines=20) |
|
|
|
|
|
|
|
|
pdf_input.change(fn=gradio_predict, inputs=[pdf_input], outputs=[text_output], api_name="predict") |
|
|
|
|
|
|
|
|
gr.Markdown(""" |
|
|
## API usage |
|
|
- Use `gradio_client` to call this Space. Function signature: `gradio_predict(pdf_file)` → `text`. |
|
|
""") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
demo.launch() |
|
|
|