Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

58 lines
1.8KB

  1. """OCR PDF pages that have no native extractable text.
  2. Usage: python tools/ocr_image_pages.py "<Book Title>"
  3. Writes library/<Book Title>/ocr-image-pages.json. The split_book.py exporter
  4. automatically inserts this OCR beneath the matching PDF page markers.
  5. """
  6. import json
  7. import sys
  8. from pathlib import Path
  9. import numpy as np
  10. import pymupdf
  11. from rapidocr_onnxruntime import RapidOCR
  12. ROOT = Path(__file__).resolve().parent.parent / "library"
  13. def main(book):
  14. pdf_path = ROOT / book / "source-file.pdf"
  15. output_path = ROOT / book / "ocr-image-pages.json"
  16. document = pymupdf.open(pdf_path)
  17. pages = [number + 1 for number, page in enumerate(document) if not page.get_text().strip()]
  18. engine = RapidOCR()
  19. recovered = {}
  20. print(f"OCR pages for {book}: {pages}", flush=True)
  21. for index, page_number in enumerate(pages, start=1):
  22. page = document[page_number - 1]
  23. pixmap = page.get_pixmap(
  24. matrix=pymupdf.Matrix(2, 2),
  25. colorspace=pymupdf.csRGB,
  26. alpha=False,
  27. )
  28. image = np.frombuffer(pixmap.samples, dtype=np.uint8).reshape(
  29. pixmap.height, pixmap.width, pixmap.n
  30. )
  31. result, _ = engine(image)
  32. lines = [item[1].strip() for item in (result or []) if item[1].strip()]
  33. recovered[str(page_number)] = (
  34. "\n".join(lines)
  35. if lines
  36. else "[Image-only page; OCR found no readable text. Consult the PDF for visual content.]"
  37. )
  38. print(f"{index}/{len(pages)} PDF page {page_number}: {len(lines)} OCR lines", flush=True)
  39. output_path.write_text(
  40. json.dumps(recovered, ensure_ascii=False, indent=2) + "\n",
  41. encoding="utf-8",
  42. newline="\n",
  43. )
  44. print(f"Wrote {output_path}", flush=True)
  45. if __name__ == "__main__":
  46. main(sys.argv[1])

Powered by TurnKey Linux.