2022-07-28 01:06:46 -07:00
|
|
|
# SPDX-FileCopyrightText: 2022 James R. Barlow
|
|
|
|
# SPDX-License-Identifier: MPL-2.0
|
2017-01-26 12:28:51 -08:00
|
|
|
|
2022-07-23 00:39:24 -07:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-01-04 02:35:14 -08:00
|
|
|
import logging
|
2017-03-29 23:31:44 -07:00
|
|
|
import os
|
2020-01-04 02:35:14 -08:00
|
|
|
import subprocess
|
2018-12-30 01:28:15 -08:00
|
|
|
from os import fspath
|
2018-03-24 15:07:02 -07:00
|
|
|
from pathlib import Path
|
2017-03-29 23:31:44 -07:00
|
|
|
|
2018-12-30 00:23:26 -08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from ocrmypdf import pdfinfo
|
2020-06-09 14:55:54 -07:00
|
|
|
from ocrmypdf._exec import tesseract
|
2019-01-02 13:34:45 -08:00
|
|
|
from ocrmypdf.exceptions import MissingDependencyError
|
2018-12-30 00:23:26 -08:00
|
|
|
|
2021-04-07 02:09:45 -07:00
|
|
|
from .conftest import check_ocrmypdf
|
2017-03-24 13:23:03 -07:00
|
|
|
|
2021-04-07 01:56:51 -07:00
|
|
|
# pylint: disable=redefined-outer-name
|
2017-01-26 12:28:51 -08:00
|
|
|
|
|
|
|
|
2017-03-29 23:31:44 -07:00
|
|
|
@pytest.mark.parametrize('basename', ['graph_ocred.pdf', 'cardinal.pdf'])
|
2019-06-03 01:33:24 -07:00
|
|
|
def test_skip_pages_does_not_replicate(resources, basename, outdir):
|
2017-03-29 23:31:44 -07:00
|
|
|
infile = resources / basename
|
|
|
|
outpdf = outdir / basename
|
|
|
|
|
|
|
|
check_ocrmypdf(
|
|
|
|
infile,
|
2018-12-30 01:27:49 -08:00
|
|
|
outpdf,
|
|
|
|
'--pdf-renderer',
|
|
|
|
'sandwich',
|
|
|
|
'--force-ocr',
|
|
|
|
'--tesseract-timeout',
|
|
|
|
'0',
|
2017-03-29 23:31:44 -07:00
|
|
|
)
|
|
|
|
|
2017-05-19 15:48:23 -07:00
|
|
|
info_in = pdfinfo.PdfInfo(infile)
|
2017-03-29 23:31:44 -07:00
|
|
|
|
2017-05-19 15:48:23 -07:00
|
|
|
info = pdfinfo.PdfInfo(outpdf)
|
2017-03-29 23:31:44 -07:00
|
|
|
for page in info:
|
2017-05-29 13:51:21 -07:00
|
|
|
assert len(page.images) == 1, "skipped page was replicated"
|
2017-03-29 23:31:44 -07:00
|
|
|
|
2020-05-03 00:51:17 -07:00
|
|
|
for n, info_out_n in enumerate(info):
|
2021-01-08 15:10:43 -08:00
|
|
|
assert info_out_n.width_inches == info_in[n].width_inches, "output resized"
|
|
|
|
assert info_out_n.height_inches == info_in[n].height_inches, "output resized"
|
2017-03-29 23:44:12 -07:00
|
|
|
|
|
|
|
|
2019-06-03 01:33:24 -07:00
|
|
|
def test_content_preservation(resources, outpdf):
|
2017-03-29 23:44:12 -07:00
|
|
|
infile = resources / 'masks.pdf'
|
|
|
|
|
|
|
|
check_ocrmypdf(
|
2019-06-03 01:33:24 -07:00
|
|
|
infile, outpdf, '--pdf-renderer', 'sandwich', '--tesseract-timeout', '0'
|
2017-03-29 23:44:12 -07:00
|
|
|
)
|
|
|
|
|
2017-05-19 15:48:23 -07:00
|
|
|
info = pdfinfo.PdfInfo(outpdf)
|
2017-03-29 23:44:12 -07:00
|
|
|
page = info[0]
|
2018-02-24 12:38:58 -08:00
|
|
|
assert len(page.images) > 1, "masks were rasterized"
|
2018-11-09 01:40:01 -08:00
|
|
|
|
|
|
|
|
2023-09-25 00:59:16 -07:00
|
|
|
@pytest.mark.skipif(
|
2023-10-16 00:41:25 -07:00
|
|
|
tesseract.version() >= tesseract.TesseractVersion('5'), reason="doesn't fool Tess 5"
|
2023-09-25 00:59:16 -07:00
|
|
|
)
|
2020-06-09 00:39:53 -07:00
|
|
|
def test_no_languages(tmp_path, monkeypatch):
|
2019-06-01 01:55:51 -07:00
|
|
|
(tmp_path / 'tessdata').mkdir()
|
2020-06-09 00:39:53 -07:00
|
|
|
monkeypatch.setenv('TESSDATA_PREFIX', fspath(tmp_path))
|
2019-06-03 01:33:24 -07:00
|
|
|
with pytest.raises(MissingDependencyError):
|
2020-06-09 00:39:53 -07:00
|
|
|
tesseract.get_languages()
|
2020-01-04 02:35:14 -08:00
|
|
|
|
|
|
|
|
|
|
|
def test_image_too_large_hocr(monkeypatch, resources, outdir):
|
|
|
|
def dummy_run(args, *, env=None, **kwargs):
|
|
|
|
raise subprocess.CalledProcessError(1, 'tesseract', output=b'Image too large')
|
|
|
|
|
|
|
|
monkeypatch.setattr(tesseract, 'run', dummy_run)
|
|
|
|
tesseract.generate_hocr(
|
|
|
|
input_file=resources / 'crom.png',
|
2020-05-06 12:37:44 -07:00
|
|
|
output_hocr=outdir / 'out.hocr',
|
2020-05-14 03:23:25 -07:00
|
|
|
output_text=outdir / 'out.txt',
|
|
|
|
languages=['eng'],
|
2020-01-04 02:35:14 -08:00
|
|
|
engine_mode=None,
|
|
|
|
tessconfig=[],
|
|
|
|
timeout=180.0,
|
|
|
|
pagesegmode=None,
|
2021-12-04 16:52:23 -08:00
|
|
|
thresholding=0,
|
2020-01-04 02:35:14 -08:00
|
|
|
user_words=None,
|
|
|
|
user_patterns=None,
|
|
|
|
)
|
2023-10-14 14:41:11 -07:00
|
|
|
assert Path(outdir / 'out.hocr').read_text() == ''
|
2020-01-04 02:35:14 -08:00
|
|
|
|
|
|
|
|
|
|
|
def test_image_too_large_pdf(monkeypatch, resources, outdir):
|
|
|
|
def dummy_run(args, *, env=None, **kwargs):
|
|
|
|
raise subprocess.CalledProcessError(1, 'tesseract', output=b'Image too large')
|
|
|
|
|
|
|
|
monkeypatch.setattr(tesseract, 'run', dummy_run)
|
|
|
|
tesseract.generate_pdf(
|
2020-05-14 03:23:25 -07:00
|
|
|
input_file=resources / 'crom.png',
|
2020-01-04 02:35:14 -08:00
|
|
|
output_pdf=outdir / 'pdf.pdf',
|
|
|
|
output_text=outdir / 'txt.txt',
|
2020-05-14 03:23:25 -07:00
|
|
|
languages=['eng'],
|
2020-01-04 02:35:14 -08:00
|
|
|
engine_mode=None,
|
|
|
|
tessconfig=[],
|
|
|
|
timeout=180.0,
|
|
|
|
pagesegmode=None,
|
2021-12-04 16:52:23 -08:00
|
|
|
thresholding=0,
|
2020-01-04 02:35:14 -08:00
|
|
|
user_words=None,
|
|
|
|
user_patterns=None,
|
|
|
|
)
|
|
|
|
assert Path(outdir / 'txt.txt').read_text() == '[skipped page]'
|
2020-01-06 02:02:05 -08:00
|
|
|
if os.name != 'nt': # different semantics
|
2020-05-12 04:09:29 -07:00
|
|
|
assert Path(outdir / 'pdf.pdf').stat().st_size == 0
|
2020-01-04 02:35:14 -08:00
|
|
|
|
|
|
|
|
|
|
|
def test_timeout(caplog):
|
2020-05-03 00:51:17 -07:00
|
|
|
tesseract.page_timedout(5)
|
2020-01-04 02:35:14 -08:00
|
|
|
assert "took too long" in caplog.text
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'in_, logged',
|
|
|
|
[
|
|
|
|
(b'Tesseract Open Source', ''),
|
|
|
|
(b'lots of diacritics blah blah', 'diacritics'),
|
|
|
|
(b'Warning in pixReadMem', ''),
|
|
|
|
(b'OSD: Weak margin', 'unsure about page orientation'),
|
|
|
|
(b'Error in pixScanForForeground', ''),
|
|
|
|
(b'Error in boxClipToRectangle', ''),
|
|
|
|
(b'an unexpected error', 'an unexpected error'),
|
|
|
|
(b'a dire warning', 'a dire warning'),
|
|
|
|
(b'read_params_file something', 'read_params_file'),
|
|
|
|
(b'an innocent message', 'innocent'),
|
|
|
|
(b'\x7f\x7f\x80innocent unicode failure', 'innocent'),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_tesseract_log_output(caplog, in_, logged):
|
2020-03-04 21:24:13 -08:00
|
|
|
caplog.set_level(logging.INFO)
|
2020-05-06 12:37:44 -07:00
|
|
|
tesseract.tesseract_log_output(in_)
|
2020-01-04 02:35:14 -08:00
|
|
|
if logged == '':
|
|
|
|
assert caplog.text == ''
|
|
|
|
else:
|
|
|
|
assert logged in caplog.text
|
|
|
|
|
|
|
|
|
|
|
|
def test_tesseract_log_output_raises(caplog):
|
|
|
|
with pytest.raises(tesseract.TesseractConfigError):
|
2020-05-06 12:37:44 -07:00
|
|
|
tesseract.tesseract_log_output(b'parameter not found: moo')
|
2020-01-04 02:35:14 -08:00
|
|
|
assert 'not found' in caplog.text
|