aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authoradamjanovsky2023-10-12 09:53:26 +0200
committerGitHub2023-10-12 09:53:26 +0200
commit1185fbf4c87c3446fddf01e28971eb85b6b03c6b (patch)
treeace02d5a0ecc25f1a09e65dd06fa5032c7050e8b
parent272d290c0014f9b925dbb01a4f46a3a1a767b67e (diff)
parent779c19ecca9612861b8d5694c6d8a0c3e27036cb (diff)
downloadsec-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
-rw-r--r--pyproject.toml1
-rw-r--r--requirements/dev_requirements.txt13
-rw-r--r--requirements/requirements.txt5
-rw-r--r--requirements/test_requirements.txt7
-rw-r--r--src/sec_certs/utils/pdf.py15
5 files changed, 35 insertions, 6 deletions
diff --git a/pyproject.toml b/pyproject.toml
index e26334d6..877802eb 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -60,6 +60,7 @@
"networkx",
"pydantic",
"psutil",
+ "pytesseract",
]
[project.optional-dependencies]
diff --git a/requirements/dev_requirements.txt b/requirements/dev_requirements.txt
index 4e0dad8e..1c75eb49 100644
--- a/requirements/dev_requirements.txt
+++ b/requirements/dev_requirements.txt
@@ -73,6 +73,8 @@ docutils==0.17.1
# sphinx
entrypoints==0.4
# via jupyter-client
+exceptiongroup==1.1.3
+ # via pytest
executing==1.2.0
# via stack-data
fastjsonschema==2.16.2
@@ -97,6 +99,7 @@ importlib-metadata==5.1.0
# via
# jupyter-cache
# myst-nb
+ # sphinx
iniconfig==1.1.1
# via pytest
ipykernel==6.19.1
@@ -222,6 +225,7 @@ packaging==22.0
# matplotlib
# pikepdf
# pydata-sphinx-theme
+ # pytesseract
# pytest
# setuptools-scm
# spacy
@@ -252,6 +256,7 @@ pillow==10.0.1
# via
# matplotlib
# pikepdf
+ # pytesseract
# sec-certs (./../pyproject.toml)
pip-tools==6.11.0
# via sec-certs (./../pyproject.toml)
@@ -304,6 +309,8 @@ pyrsistent==0.19.2
# via jsonschema
pysankeybeta==1.4.0
# via sec-certs (./../pyproject.toml)
+pytesseract==0.3.10
+ # via sec-certs (./../pyproject.toml)
pytest==7.2.0
# via
# pytest-cov
@@ -434,7 +441,9 @@ tomli==2.0.1
# pytest
# setuptools-scm
tornado==6.3.3
- # via setuptools-scm
+ # via
+ # ipykernel
+ # jupyter-client
tqdm==4.64.1
# via
# sec-certs (./../pyproject.toml)
@@ -464,10 +473,12 @@ types-urllib3==1.26.25.4
# via types-requests
typing-extensions==4.4.0
# via
+ # black
# mypy
# myst-nb
# myst-parser
# pydantic
+ # pypdf
# setuptools-scm
urllib3==1.26.17
# via requests
diff --git a/requirements/requirements.txt b/requirements/requirements.txt
index 8c0e1299..470dab9d 100644
--- a/requirements/requirements.txt
+++ b/requirements/requirements.txt
@@ -128,6 +128,7 @@ packaging==22.0
# ipykernel
# matplotlib
# pikepdf
+ # pytesseract
# setuptools-scm
# spacy
pandas==1.5.2
@@ -152,6 +153,7 @@ pillow==10.0.1
# via
# matplotlib
# pikepdf
+ # pytesseract
# sec-certs (./../pyproject.toml)
pkgconfig==1.5.5
# via sec-certs (./../pyproject.toml)
@@ -189,6 +191,8 @@ pyrsistent==0.19.2
# via jsonschema
pysankeybeta==1.4.0
# via sec-certs (./../pyproject.toml)
+pytesseract==0.3.10
+ # via sec-certs (./../pyproject.toml)
python-dateutil==2.8.2
# via
# jupyter-client
@@ -275,6 +279,7 @@ typer==0.7.0
typing-extensions==4.4.0
# via
# pydantic
+ # pypdf
# setuptools-scm
urllib3==1.26.17
# via requests
diff --git a/requirements/test_requirements.txt b/requirements/test_requirements.txt
index 89670807..6f0e6710 100644
--- a/requirements/test_requirements.txt
+++ b/requirements/test_requirements.txt
@@ -52,6 +52,8 @@ distro==1.8.0
# via tabula-py
entrypoints==0.4
# via jupyter-client
+exceptiongroup==1.1.3
+ # via pytest
executing==1.2.0
# via stack-data
fonttools==4.38.0
@@ -136,6 +138,7 @@ packaging==22.0
# ipykernel
# matplotlib
# pikepdf
+ # pytesseract
# pytest
# setuptools-scm
# spacy
@@ -161,6 +164,7 @@ pillow==10.0.1
# via
# matplotlib
# pikepdf
+ # pytesseract
# sec-certs (./../pyproject.toml)
pkgconfig==1.5.5
# via sec-certs (./../pyproject.toml)
@@ -200,6 +204,8 @@ pyrsistent==0.19.2
# via jsonschema
pysankeybeta==1.4.0
# via sec-certs (./../pyproject.toml)
+pytesseract==0.3.10
+ # via sec-certs (./../pyproject.toml)
pytest==7.2.0
# via
# pytest-cov
@@ -295,6 +301,7 @@ typer==0.7.0
typing-extensions==4.4.0
# via
# pydantic
+ # pypdf
# setuptools-scm
urllib3==1.26.17
# via requests
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 = ""