diff options
| author | adamjanovsky | 2023-10-12 09:53:26 +0200 |
|---|---|---|
| committer | GitHub | 2023-10-12 09:53:26 +0200 |
| commit | 1185fbf4c87c3446fddf01e28971eb85b6b03c6b (patch) | |
| tree | ace02d5a0ecc25f1a09e65dd06fa5032c7050e8b /src | |
| parent | 272d290c0014f9b925dbb01a4f46a3a1a767b67e (diff) | |
| parent | 779c19ecca9612861b8d5694c6d8a0c3e27036cb (diff) | |
| download | sec-certs-1185fbf4c87c3446fddf01e28971eb85b6b03c6b.tar.gz sec-certs-1185fbf4c87c3446fddf01e28971eb85b6b03c6b.tar.zst sec-certs-1185fbf4c87c3446fddf01e28971eb85b6b03c6b.zip | |
Merge pull request #357 from crocs-muni/feat-pytesseract-wrapper
feat: Added pytesseract wrapper
Diffstat (limited to 'src')
| -rw-r--r-- | src/sec_certs/utils/pdf.py | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/src/sec_certs/utils/pdf.py b/src/sec_certs/utils/pdf.py index 749a8a5a..7736b5d0 100644 --- a/src/sec_certs/utils/pdf.py +++ b/src/sec_certs/utils/pdf.py @@ -11,6 +11,8 @@ from typing import Any import pdftotext import pikepdf +import pytesseract +from PIL import Image from sec_certs import constants from sec_certs.constants import ( @@ -51,13 +53,16 @@ def ocr_pdf_file(pdf_path: Path) -> str: ) if ppm.returncode != 0: raise ValueError(f"pdftoppm failed: {ppm.returncode}") + for ppm_path in map(Path, glob.glob(str(tmppath / "image*.ppm"))): base = ppm_path.with_suffix("") - tes = subprocess.run( - ["tesseract", "-l", "eng+deu+fra", ppm_path, base], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL - ) - if tes.returncode != 0: - raise ValueError(f"tesseract failed: {tes.returncode}") + content = pytesseract.image_to_string(Image.open(ppm_path), lang="eng+deu+fra") + + if content: + with Path(base.with_suffix(".txt")).open("w") as file: + file.write(content) + else: + raise ValueError(f"OCR failed for document {ppm_path}. Check document manually") contents = "" |
