From 7fbc0d6460835e88c8f1293096fb96e7450d86e2 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Mon, 8 Feb 2016 23:40:36 -0800 Subject: [PATCH] tesseract: unify logging function --- ocrmypdf/tesseract.py | 48 +++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/ocrmypdf/tesseract.py b/ocrmypdf/tesseract.py index 0e88431b..2f91bece 100644 --- a/ocrmypdf/tesseract.py +++ b/ocrmypdf/tesseract.py @@ -112,6 +112,22 @@ def get_orientation(input_file, language: list, timeout: float, log): return oc +def tesseract_log_output(log, stdout, input_file): + lines = stdout.splitlines() + prefix = "{0:4d}: [tesseract] ".format(page_number(input_file)) + for line in lines: + if line.startswith("Tesseract Open Source"): + continue + elif line.startswith("Warning in pixReadMem"): + continue + elif 'diacritics' in line: + log.warning(prefix + "lots of diacritics - possibly poor OCR") + elif line.startswith('OSD: Weak margin'): + log.warning(prefix + "unsure about page orientation") + else: + log.info(prefix + line.strip()) + + def generate_hocr(input_file, output_hocr, language: list, tessconfig: list, timeout: float, pageinfo_getter, pagesegmode: int, log): @@ -133,10 +149,10 @@ def generate_hocr(input_file, output_hocr, language: list, tessconfig: list, p = Popen(args_tesseract, close_fds=True, stdout=PIPE, stderr=STDOUT, universal_newlines=True) try: - stdout, stderr = p.communicate(timeout=timeout) + stdout, _ = p.communicate(timeout=timeout) except TimeoutExpired: p.kill() - stdout, stderr = p.communicate() + stdout, _ = p.communicate() # Generate a HOCR file with no recognized text if tesseract times out # Temporary workaround to hocrTransform not being able to function if # it does not have a valid hOCR file. @@ -146,22 +162,7 @@ def generate_hocr(input_file, output_hocr, language: list, tessconfig: list, pageinfo['width_pixels'], pageinfo['height_pixels'])) else: - lines = stdout.splitlines() - for line in lines: - if line.startswith("Tesseract Open Source"): - continue - elif line.startswith("Warning in pixReadMemPng"): - continue - elif 'diacritics' in line: - log.warning("{0:4d}: {1} - may indicate poor results".format( - page_number(input_file), line.strip())) - elif line.startswith('OSD: Weak margin'): - log.warning("{0:4d}: unsure about page orientation".format( - page_number(input_file))) - else: - log.info("{0:4d}: {1}".format( - page_number(input_file), line.strip())) - + tesseract_log_output(log, stdout, input_file) if p.returncode != 0: raise CalledProcessError(p.returncode, args_tesseract) @@ -212,17 +213,14 @@ def generate_pdf(input_image, skip_pdf, output_pdf, language: list, os.path.splitext(output_pdf)[0], # Tesseract appends suffix 'pdf' ] + tessconfig) - p = Popen(args_tesseract, close_fds=True, stdout=PIPE, stderr=PIPE, + p = Popen(args_tesseract, close_fds=True, stdout=PIPE, stderr=STDOUT, universal_newlines=True) try: - stdout, stderr = p.communicate(timeout=timeout) - if stdout: - log.info(stdout) - if stderr: - log.error(stderr) + stdout, _ = p.communicate() except TimeoutExpired: p.kill() log.info("Tesseract - page timed out") shutil.copy(skip_pdf, output_pdf) - + else: + tesseract_log_output(log, stdout, input_image)