Skip to content

Commit

Permalink
Allow rendering word boxes for debug
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarlow83 committed Jan 29, 2024
1 parent 6e0bf0a commit 330a7a1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
38 changes: 21 additions & 17 deletions ocrmypdf_easyocr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@
from __future__ import annotations

import logging
import os
import multiprocessing.managers
import os
import threading
import traceback
from typing import Optional, Tuple

import cv2 as cv
import easyocr
import numpy.typing as npt
import pluggy
from ocrmypdf import OcrEngine, hookimpl
from ocrmypdf._exec import tesseract
import traceback

from ocrmypdf_easyocr._cv import detect_skew
from ocrmypdf_easyocr._easyocr import tidy_easyocr_result
from ocrmypdf_easyocr._pdf import easyocr_to_pikepdf

from typing import Optional, Tuple
import numpy.typing as npt

log = logging.getLogger(__name__)

ISO_639_3_2: dict[str, str] = {
Expand Down Expand Up @@ -94,14 +93,14 @@

Task = Tuple[npt.NDArray, multiprocessing.Value, threading.Event]


def _ocrProcess(q: multiprocessing.Queue[Task], options):
reader: Optional[easyocr.Reader] = None

# TODO: signal _ocrProcess to quit after OCR completes.
while True:
(gray, outputDict, event) = q.get()


# Init reader on first OCR attempt: Wait until `options` variable is fully initialized.
# Note: `options` variable is on the same process with the main thread.
try:
Expand All @@ -110,8 +109,7 @@ def _ocrProcess(q: multiprocessing.Queue[Task], options):
languages = [ISO_639_3_2[lang] for lang in options.languages]
reader = easyocr.Reader(languages, useGPU)
outputDict["output"] = reader.readtext(
gray,
batch_size=options.easyocr_batch_size
gray, batch_size=options.easyocr_batch_size
)
except Exception as e:
traceback.print_exception(e)
Expand All @@ -137,19 +135,20 @@ def check_options(options):

# TODO : proper cleanup code for `ocrProcessList`

options._easyocr_struct = {
"manager": m,
"queue": q
}
options._easyocr_struct = {"manager": m, "queue": q}


@hookimpl
def add_options(parser):
easyocr_options = parser.add_argument_group(
"EasyOCR", "Advanced control of EasyOCR"
)
easyocr_options = parser.add_argument_group("EasyOCR", "EasyOCR options")
easyocr_options.add_argument("--easyocr-no-gpu", action="store_false", dest="gpu")
easyocr_options.add_argument("--easyocr-batch-size", type=int, default=4)
easyocr_options.add_argument("--easyocr-workers", type=int, default=1)
easyocr_options.add_argument(
"--easyocr-debug-suppress-images",
action="store_true",
dest="easyocr_debug_suppress_images",
)


class EasyOCREngine(OcrEngine):
Expand Down Expand Up @@ -208,8 +207,13 @@ def generate_pdf(input_file, output_pdf, output_text, options):
text = " ".join([result.text for result in results])
output_text.write_text(text)

# easyocr_to_pdf(input_file, 1.0, results, output_pdf)
easyocr_to_pikepdf(input_file, 1.0, results, output_pdf)
easyocr_to_pikepdf(
input_file,
1.0,
results,
output_pdf,
boxes=options.easyocr_debug_suppress_images,
)


@hookimpl
Expand Down
3 changes: 2 additions & 1 deletion ocrmypdf_easyocr/_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ def easyocr_to_pikepdf(
image_scale: float,
results: Iterable[EasyOCRResult],
output_pdf: Path,
boxes: bool,
):
"""Convert EasyOCR results to a PDF with text annotations (no images).
Expand Down Expand Up @@ -302,7 +303,7 @@ def easyocr_to_pikepdf(
Font=Dictionary({"/f-0-0": register_glyphlessfont(pdf)})
)

cs = generate_text_content_stream(results, scale, height, boxes=False)
cs = generate_text_content_stream(results, scale, height, boxes=boxes)
pdf.pages[0].Contents = pdf.make_stream(unparse_content_stream(cs))

pdf.save(output_pdf)
Expand Down

0 comments on commit 330a7a1

Please sign in to comment.