diff --git a/ocrmypdf/tesseract.py b/ocrmypdf/tesseract.py index 105a2cbd..3057cc53 100644 --- a/ocrmypdf/tesseract.py +++ b/ocrmypdf/tesseract.py @@ -7,6 +7,7 @@ import re import shutil from functools import lru_cache from . import ExitCode, get_program +from collections import namedtuple from subprocess import Popen, PIPE, CalledProcessError, \ TimeoutExpired, check_output, STDOUT @@ -16,6 +17,10 @@ except ImportError: DEVNULL = open(os.devnull, 'wb') +OrientationConfidence = namedtuple( + 'OrientationConfidence', + ('angle', 'confidence')) + HOCR_TEMPLATE = ''' @@ -76,6 +81,37 @@ def languages(): return set(lang.strip() for lang in langs.splitlines()[1:]) +def get_orientation(input_file, language: list, timeout: float, log): + args_tesseract = [ + get_program('tesseract'), + '-l', '+'.join(language), + '-psm', '0', + input_file, + 'stdout' + ] + + p = Popen(args_tesseract, close_fds=True, stdout=PIPE, stderr=STDOUT, + universal_newlines=True) + try: + stdout, stderr = p.communicate(timeout=timeout) + except TimeoutExpired: + p.kill() + stdout, stderr = p.communicate() + return OrientationConfidence(angle=0, confidence=0.0) + else: + osd = {} + for line in stdout.splitlines(): + line = line.strip() + parts = line.split(':', maxsplit=2) + if len(parts) == 2: + osd[parts[0].strip()] = parts[1].strip() + print(osd) + oc = OrientationConfidence( + angle=int(osd['Orientation in degrees']), + confidence=float(osd['Orientation confidence'])) + return oc + + def generate_hocr(input_file, output_hocr, language: list, tessconfig: list, timeout: float, pageinfo_getter, pagesegmode: int, log):