diff --git a/src/ocrpage.py b/src/ocrpage.py
index 37a19f8d..0fbbdc95 100755
--- a/src/ocrpage.py
+++ b/src/ocrpage.py
@@ -7,7 +7,8 @@ import sys
import os.path
from parse import parse
-from subprocess import Popen, check_call, PIPE, CalledProcessError
+from subprocess import Popen, check_call, PIPE, CalledProcessError, \
+ TimeoutExpired
try:
from subprocess import DEVNULL
except ImportError:
@@ -346,6 +347,29 @@ def select_ocr_image(infiles, output_file):
re_symlink(infiles[-1], output_file, logger, logger_mutex)
+hocr_template = '''
+
+
+
+
+
+
+
+
+
+
+
+'''
+
+
@transform(select_ocr_image, suffix(".for_ocr.tif"), ".hocr")
def ocr_tesseract(
input_file,
@@ -361,19 +385,29 @@ def ocr_tesseract(
]
p = Popen(args_tesseract, close_fds=True, stdout=PIPE, stderr=PIPE,
universal_newlines=True)
- stdout, stderr = p.communicate()
+ try:
+ stdout, stderr = p.communicate(timeout=180)
+ except TimeoutExpired:
+ p.kill()
+ stdout, stderr = 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.
+ with open(output_file, 'w', encoding="utf-8") as f:
+ f.write(hocr_template.format(pageinfo['width_pixels'],
+ pageinfo['height_pixels']))
+ else:
+ with logger_mutex:
+ if stdout:
+ logger.info(stdout)
+ if stderr:
+ logger.error(stderr)
- with logger_mutex:
- if stdout:
- logger.info(stdout)
- if stderr:
- logger.error(stderr)
+ if p.returncode != 0:
+ raise CalledProcessError(p.returncode, args_tesseract)
- if p.returncode != 0:
- raise CalledProcessError(p.returncode, args_tesseract)
-
- # Tesseract appends suffix ".html" on its own
- re_symlink(output_file + ".html", output_file, logger, logger_mutex)
+ # Tesseract appends suffix ".html" on its own
+ re_symlink(output_file + ".html", output_file, logger, logger_mutex)
@merge([convert_to_tiff, deskew_imagemagick, deskew_leptonica,