From 0eaf610cfe6a0a36cfad2fa71b42d1f921b6e49b Mon Sep 17 00:00:00 2001 From: GeorgeFI Date: Wed, 20 Sep 2023 20:53:19 +0200 Subject: feat: Added pytesseract wrapper --- src/sec_certs/utils/pdf.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/sec_certs/utils/pdf.py b/src/sec_certs/utils/pdf.py index 749a8a5a..b4ec1e94 100644 --- a/src/sec_certs/utils/pdf.py +++ b/src/sec_certs/utils/pdf.py @@ -11,6 +11,7 @@ from typing import Any import pdftotext import pikepdf +import pytesseract from sec_certs import constants from sec_certs.constants import ( @@ -51,13 +52,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(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 = "" -- cgit v1.3.1 From 498308a737943b87ecbf38a05404d4caa22fa726 Mon Sep 17 00:00:00 2001 From: GeorgeFI Date: Mon, 25 Sep 2023 21:12:26 +0200 Subject: fix: OCR with Pytesseract fixed now --- src/sec_certs/utils/pdf.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/sec_certs/utils/pdf.py b/src/sec_certs/utils/pdf.py index b4ec1e94..7736b5d0 100644 --- a/src/sec_certs/utils/pdf.py +++ b/src/sec_certs/utils/pdf.py @@ -12,6 +12,7 @@ from typing import Any import pdftotext import pikepdf import pytesseract +from PIL import Image from sec_certs import constants from sec_certs.constants import ( @@ -55,7 +56,7 @@ def ocr_pdf_file(pdf_path: Path) -> str: for ppm_path in map(Path, glob.glob(str(tmppath / "image*.ppm"))): base = ppm_path.with_suffix("") - content = pytesseract.image_to_string(ppm_path, lang="eng+deu+fra") + 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: -- cgit v1.3.1 From 779c19ecca9612861b8d5694c6d8a0c3e27036cb Mon Sep 17 00:00:00 2001 From: GeorgeFI Date: Sun, 1 Oct 2023 14:26:05 +0200 Subject: fix: Fixed calling method on NoneType --- src/sec_certs/sample/cc_scheme.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/sec_certs/sample/cc_scheme.py b/src/sec_certs/sample/cc_scheme.py index 307dfb57..bc0c0792 100644 --- a/src/sec_certs/sample/cc_scheme.py +++ b/src/sec_certs/sample/cc_scheme.py @@ -107,7 +107,7 @@ def get_australia_in_evaluation(enhanced: bool = True) -> list[dict[str, Any]]: if enhanced: e: dict[str, Any] = {} cert_page = _get_page(cert["url"]) - article = cert_page.find("article", attrs={"role": "article"}) + article = cert_page.find("article") blocks = article.find("div").find_all("div", class_="flex", recursive=False) for h2 in blocks[0].find_all("h2"): val = sns(h2.find_next_sibling("span").text) -- cgit v1.3.1