tesseract: unify logging function

This commit is contained in:
James R. Barlow
2016-02-08 23:40:36 -08:00
parent 1ba8b1aa4b
commit 7fbc0d6460
+23 -25
View File
@@ -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)