From ab0e5fa4256095e8d027aca65664b550d2b4ac0e Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Fri, 19 Feb 2016 03:58:39 -0800 Subject: [PATCH] Improve error checking for tesseract -psm 0 (orientation) errors --- ocrmypdf/tesseract.py | 17 +++++++---------- tests/test_main.py | 9 +++++++++ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/ocrmypdf/tesseract.py b/ocrmypdf/tesseract.py index 0e1a60cf..0a205621 100644 --- a/ocrmypdf/tesseract.py +++ b/ocrmypdf/tesseract.py @@ -90,19 +90,16 @@ def get_orientation(input_file, language: list, timeout: float, log): 'stdout' ] - p = Popen(args_tesseract, close_fds=True, stdout=PIPE, stderr=STDOUT, - universal_newlines=True) try: - stdout, _ = p.communicate(timeout=timeout) + stdout = check_output( + args_tesseract, close_fds=True, stderr=STDOUT, + universal_newlines=True, timeout=timeout) except TimeoutExpired: - p.kill() - stdout, _ = p.communicate() return OrientationConfidence(angle=0, confidence=0.0) + except CalledProcessError as e: + tesseract_log_output(log, e.output, input_file) + raise e from e else: - if p.returncode != 0: - log.error(stdout) - return OrientationConfidence(angle=0, confidence=0.0) - osd = {} for line in stdout.splitlines(): line = line.strip() @@ -128,7 +125,7 @@ def tesseract_log_output(log, stdout, input_file): log.warning(prefix + "lots of diacritics - possibly poor OCR") elif line.startswith('OSD: Weak margin'): log.warning(prefix + "unsure about page orientation") - elif 'error' in line.lower(): + elif 'error' in line.lower() or 'exception' in line.lower(): log.error(prefix + line.strip()) else: log.info(prefix + line.strip()) diff --git a/tests/test_main.py b/tests/test_main.py index 217908b8..c6381137 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -460,3 +460,12 @@ def test_tesseract_crash(renderer, spoof_tesseract_crash): assert sh.returncode == ExitCode.child_process_error assert not os.path.exists(_outfile('wontwork.pdf')) assert "ERROR" in err + + +def test_tesseract_crash_autorotate(spoof_tesseract_crash): + sh, out, err = run_ocrmypdf_env( + 'ccitt.pdf', 'wontwork.pdf', + '-r', env=spoof_tesseract_crash) + assert sh.returncode == ExitCode.child_process_error + assert not os.path.exists(_outfile('wontwork.pdf')) + assert "ERROR" in err