Chore: pass is_image to zone OCR instead of sniffing the file type

This commit is contained in:
stumpylog committed 2026-11-20 09:20:25 -08:00
1 parent ccbff94208
commit f0c91bbd59
5 files changed
+102 -64

No files matched your search

+3
View File
@@ -1040,10 +1040,12 @@ class ConsumerPlugin(
)
if not templates:
return []
is_image = False
if archive_path is not None and Path(archive_path).is_file():
doc_path: Path | None = Path(archive_path)
elif mime_type == "application/pdf" or mime_type.startswith("image/"):
doc_path = self.working_copy
is_image = mime_type.startswith("image/")
else:
doc_path = None
return plan_zone_extraction(
@@ -1051,6 +1053,7 @@ class ConsumerPlugin(
page_count,
templates,
label=self.filename,
is_image=is_image,
)
except Exception:
self.log.exception(
@@ -331,6 +331,7 @@ class TestZoneOcrIndexing:
file_document.page_count,
[template],
label="ingestion",
is_image=True,
)
apply_zone_plans(document, plans, label="ingestion")
document_consumption_finished.send(sender=None, document=file_document)
+66 -35
View File
@@ -35,7 +35,6 @@ from documents.zone_ocr import _combine_field_value
from documents.zone_ocr import _convert_value
from documents.zone_ocr import _crop_zone
from documents.zone_ocr import _date_rules
from documents.zone_ocr import _detect_mime
from documents.zone_ocr import _extract_zone
from documents.zone_ocr import _number_locale
from documents.zone_ocr import _parse_date
@@ -386,7 +385,10 @@ class TestZoneExtraction:
- An empty result is returned and the reader is never run
"""
setattr(zone, attribute, value)
assert extract_zone_preview(page, zone, 100, 50, 1) == ZoneExtraction()
assert (
extract_zone_preview(page, zone, 100, 50, 1, is_image=True)
== ZoneExtraction()
)
mock_zone_ocr_run.assert_not_called()
def test_date_transform_uses_zone_format(
@@ -490,7 +492,14 @@ class TestZoneExtraction:
- The raw text and transformed value are returned
"""
mock_zone_ocr_run.return_value.stdout = "account abc"
assert extract_zone_preview(page, zone, 0, 0, 1) == ZoneExtraction(
assert extract_zone_preview(
page,
zone,
0,
0,
1,
is_image=True,
) == ZoneExtraction(
raw_text="account abc",
value="ACCOUNT ABC",
)
@@ -510,7 +519,7 @@ class TestSavedZoneExtraction:
@pytest.fixture
def document(self) -> Document:
return DocumentFactory(title="Original")
return DocumentFactory(title="Original", mime_type="image/png")
@pytest.fixture
def template(self) -> OcrTemplate:
@@ -894,6 +903,7 @@ class TestSavedZoneExtraction:
- Each page is rendered only once and the rendered files are removed
"""
document.page_count = 2
document.mime_type = "application/pdf"
make_zone(template, "First", page=1)
make_zone(template, "Last", page=-1)
second = make_template("Second")
@@ -1305,7 +1315,7 @@ class TestSavedZoneExtraction:
def preview_and_save(raw_text: str) -> None:
mock_zone_ocr_run.return_value.stdout = raw_text
preview = extract_zone_preview(page, zone, 100, 50, 1)
preview = extract_zone_preview(page, zone, 100, 50, 1, is_image=True)
assert preview.raw_text == raw_text
assert preview.value == raw_text.upper()
_process_template(
@@ -3490,7 +3500,7 @@ class TestRenderPages:
- Only the in range pages are rendered, in order, as 1-based pages
at 300 DPI with a 60 second timeout
"""
pages = _render_pages(pdf, {-1, 0, 2, 3}, tmp_path, 3)
pages = _render_pages(pdf, {-1, 0, 2, 3}, tmp_path, 3, is_image=False)
assert list(pages) == [0, 2]
assert mock_rasterize.call_count == 2
@@ -3514,7 +3524,7 @@ class TestRenderPages:
"""
mock_rasterize.side_effect = None
assert _render_pages(pdf, {0}, tmp_path, None) == {}
assert _render_pages(pdf, {0}, tmp_path, None, is_image=False) == {}
@pytest.mark.parametrize(
"error",
@@ -3549,7 +3559,7 @@ class TestRenderPages:
mock_rasterize.side_effect = render
assert list(_render_pages(pdf, {0, 1}, tmp_path, 2)) == [1]
assert list(_render_pages(pdf, {0, 1}, tmp_path, 2, is_image=False)) == [1]
def test_image_is_used_directly_only_for_first_page(
self,
@@ -3568,36 +3578,13 @@ class TestRenderPages:
image_path = tmp_path / "image.png"
with Image.new("RGB", (10, 10)) as image:
image.save(image_path)
assert _render_pages(image_path, {0}, tmp_path, 1) == {0: image_path}
assert _render_pages(image_path, {1}, tmp_path, 1) == {}
assert _render_pages(image_path, {0}, tmp_path, 1, is_image=True) == {
0: image_path,
}
assert _render_pages(image_path, {1}, tmp_path, 1, is_image=True) == {}
mock_rasterize.assert_not_called()
class TestDetectMime:
"""Tests for _detect_mime."""
@pytest.mark.parametrize(
("filename", "expected"),
[
pytest.param("test.pdf", "application/pdf", id="pdf-extension"),
pytest.param("test.png", "image/png", id="png-extension"),
pytest.param("test.jpg", "image/jpeg", id="jpg-extension"),
pytest.param("test.xyz", None, id="unknown-extension"),
pytest.param("test.webp", "image/webp", id="webp-extension"),
],
)
def test_detect_mime(self, filename: str, expected: str | None) -> None:
"""
GIVEN:
- A path with a given file extension
WHEN:
- The MIME type is detected
THEN:
- The expected MIME type, or None for an unknown extension, is returned
"""
assert _detect_mime(Path(filename)) == expected
class TestResolveDocPath:
"""Tests for _resolve_doc_path."""
@@ -3709,6 +3696,50 @@ class TestRunZoneExtraction:
stderr="",
)
@pytest.mark.parametrize(
("mime_type", "has_archive", "expected_is_image"),
[
pytest.param("image/png", False, True, id="image-original"),
pytest.param("image/png", True, False, id="image-with-archive"),
pytest.param("application/pdf", False, False, id="pdf-original"),
],
)
def test_tells_the_planner_whether_the_file_is_an_image(
self,
mocker: MockerFixture,
paperless_dirs: PaperlessDirs,
template_with_zone: Callable[..., OcrTemplate],
mime_type: str,
*,
has_archive: bool,
expected_is_image: bool,
) -> None:
"""
GIVEN:
- A stored document with an original file and optionally an archive
WHEN:
- Zone extraction runs for it
THEN:
- The archive is never treated as an image, the original is when
its mime type is an image
"""
document = DocumentFactory(
mime_type=mime_type,
filename="original.bin",
archive_filename="archive.pdf" if has_archive else None,
)
document.source_path.parent.mkdir(parents=True, exist_ok=True)
document.source_path.write_bytes(b"original")
if has_archive:
document.archive_path.parent.mkdir(parents=True, exist_ok=True)
document.archive_path.write_bytes(b"archive")
plan = mocker.patch("documents.zone_ocr.plan_zone_extraction", return_value=[])
mocker.patch("documents.zone_ocr.apply_zone_plans", return_value=[])
run_zone_extraction(document, templates=[template_with_zone("T")])
assert plan.call_args.kwargs["is_image"] is expected_is_image
@pytest.fixture
def template_with_zone(self) -> Callable[..., OcrTemplate]:
"""Build a template with one default zone, matching "test" by default."""
+2
View File
@@ -5966,6 +5966,8 @@ class OcrTemplateViewSet(BulkPermissionMixin, PassUserMixin, ModelViewSet[OcrTem
zone.zone_source_width or 0,
zone.zone_source_height or 0,
document.page_count,
is_image=document.archive_path is None
and document.mime_type.startswith("image/"),
)
regex_match = None
+30 -29
View File
@@ -21,7 +21,6 @@ from typing import TypeAlias
from typing import assert_never
import dateparser
import magic
import zxingcpp
from babel import Locale
from babel import UnknownLocaleError
@@ -270,11 +269,14 @@ def run_zone_extraction(
return []
label = f"document {document.pk}"
doc_path = _resolve_doc_path(document, original_file)
is_archive = doc_path is not None and doc_path == document.archive_path
plans = plan_zone_extraction(
_resolve_doc_path(document, original_file),
doc_path,
document.page_count,
templates,
label=label,
is_image=not is_archive and document.mime_type.startswith("image/"),
)
results = apply_zone_plans(get_root_document(document), plans, label=label)
if zone_results_written(results):
@@ -290,12 +292,14 @@ def plan_zone_extraction(
templates: list[OcrTemplate],
*,
label: str,
is_image: bool,
) -> list[TemplatePlan]:
"""
Render the zones' pages from doc_path and compute each template's field
updates without writing anything. A template that fails yields only
TEMPLATE_FAILED results, and a missing file yields MISSING_FILE results for
every zone. The label names the document in log messages.
every zone. The label names the document in log messages. is_image says
doc_path is an image rather than a PDF.
"""
template_zones = [(template, list(template.zones.all())) for template in templates]
if doc_path is None:
@@ -315,7 +319,13 @@ def plan_zone_extraction(
plans = []
with tempfile.TemporaryDirectory(dir=settings.SCRATCH_DIR) as tmp_dir:
tmp_path = Path(tmp_dir)
page_images = _render_pages(doc_path, pages, tmp_path, page_count)
page_images = _render_pages(
doc_path,
pages,
tmp_path,
page_count,
is_image=is_image,
)
for template, zones in template_zones:
try:
plans.append(
@@ -671,12 +681,16 @@ def _render_pages(
pages: set[int],
tmp_dir: Path,
page_count: int | None,
*,
is_image: bool,
) -> dict[int, Path]:
"""Render specific PDF pages as PNG images using pdftoppm (poppler-utils)."""
"""
Render specific PDF pages as PNG images using pdftoppm (poppler-utils).
An image file is used as-is for page 0 only.
"""
result: dict[int, Path] = {}
mime = _detect_mime(doc_path)
if mime and mime.startswith("image/"):
if is_image:
if 0 in pages:
result[0] = doc_path
return result
@@ -826,12 +840,20 @@ def extract_zone_preview(
source_width: int,
source_height: int,
page_count: int | None,
*,
is_image: bool,
) -> ZoneExtraction:
"""Extract a zone without writing its value to the document."""
page_idx = _resolve_page_idx(zone.page, page_count)
with tempfile.TemporaryDirectory(dir=settings.SCRATCH_DIR) as tmp_dir:
tmp_path = Path(tmp_dir)
page_images = _render_pages(doc_path, {page_idx}, tmp_path, page_count)
page_images = _render_pages(
doc_path,
{page_idx},
tmp_path,
page_count,
is_image=is_image,
)
if page_idx not in page_images:
return ZoneExtraction()
@@ -1311,24 +1333,3 @@ def _convert_value(
except (ValueError, InvalidOperation):
logger.info("Zone OCR: could not convert value to %s", data_type)
return None
def _detect_mime(path: Path) -> str | None:
"""Detect MIME type of a file."""
try:
return magic.from_file(str(path), mime=True)
except (OSError, magic.MagicException):
logger.debug("Zone OCR: magic failed for %s, falling back to extension", path)
suffix = path.suffix.lower()
return {
".pdf": "application/pdf",
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".tiff": "image/tiff",
".tif": "image/tiff",
".webp": "image/webp",
".bmp": "image/bmp",
".gif": "image/gif",
}.get(suffix)