From b0467f23352b7c2368cfe521049052c28254c476 Mon Sep 17 00:00:00 2001 From: iriley Date: Wed, 15 May 2024 16:33:58 +0200 Subject: [PATCH 1/2] chore: debug coordinate remapping logic, esp mirroring --- src/cv_analysis/utils/annotate.py | 16 +++++++++++- src/cv_analysis/utils/image_extraction.py | 31 +++++++++++++---------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/cv_analysis/utils/annotate.py b/src/cv_analysis/utils/annotate.py index 2689bac..9d37b14 100644 --- a/src/cv_analysis/utils/annotate.py +++ b/src/cv_analysis/utils/annotate.py @@ -4,7 +4,9 @@ from pathlib import Path from typing import Union import fitz # type: ignore -from kn_utils.logging import logger # type: ignore +from kn_utils.logging import logger + +from cv_analysis.utils.image_extraction import mirror_horizontal # type: ignore def annotate_pdf( @@ -28,6 +30,8 @@ def annotate_page(page: fitz.Page, prediction): label, probability, uuid = itemgetter("label", "probability", "uuid")(box) x0, y0, x1, y1 = bbox + + print(page.bound) page.draw_rect(fitz.Rect(x0, y0, x1, y1), color=(0, 0, 1), width=2) label_x, label_y = x0, y0 - 5 page.insert_text( @@ -39,6 +43,16 @@ def annotate_page(page: fitz.Page, prediction): for line in prediction.get("tableLines", []): start = itemgetter("x1", "y1")(line) end = itemgetter("x2", "y2")(line) + + bbox = *start, *end + height = page.bound()[3] + logger.info(f"{height=}") + bbox = mirror_horizontal(bbox, page_height=height) + assert bbox == mirror_horizontal(mirror_horizontal(bbox, height), height) + + start = tuple(bbox[:2]) + end = tuple(bbox[2:]) + page.draw_line(start, end, color=(1, 0, 0.5), width=1) return page diff --git a/src/cv_analysis/utils/image_extraction.py b/src/cv_analysis/utils/image_extraction.py index a3f396c..7a2dece 100644 --- a/src/cv_analysis/utils/image_extraction.py +++ b/src/cv_analysis/utils/image_extraction.py @@ -39,7 +39,7 @@ def transform_image_coordinates_to_pdf_coordinates( return rect.x0, rect.y0, rect.x1, rect.y1 -def rescale_to_pdf(bbox: BBoxType, page_info: PageInfo) -> tuple[float, float, float, float]: +def rescale_to_pdf(bbox: BBoxType, page_info: PageInfo) -> BBoxType: round3 = lambda x: tuple(map(lambda y: round(y, 3), x)) pdf_h, pdf_w = page_info.height, page_info.width @@ -54,20 +54,25 @@ def rescale_to_pdf(bbox: BBoxType, page_info: PageInfo) -> tuple[float, float, f return round3((bbox[0] * ratio_w, bbox[1] * ratio_h, bbox[2] * ratio_w, bbox[3] * ratio_h)) -def derotate_image(bbox: tuple[float, float, float, float], page_info: PageInfo) -> ...: - def mirror_horizontal(bbox, page_height): - x0, y0, x1, y1 = bbox - y0_new = page_height - y1 - y1_new = page_height - y0 +def mirror_horizontal(bbox: BBoxType, page_height: int | float) -> BBoxType: + x0, y0, x1, y1 = bbox + y0_new = page_height - y0 + y1_new = page_height - y1 + logger.info(f"Mirrored horizontally: {x0}, {y0} -> {y0_new}, {x1}, {y1} -> {y1_new}; page height: {page_height}") - return x0, y0_new, x1, y1_new + return x0, y0_new, x1, y1_new - def mirror_vertical(bbox, page_width): - x0, y0, x1, y1 = bbox - x0_new = page_width - x1 - x1_new = page_width - x0 - return x0_new, y0, x1_new, y1 +def mirror_vertical(bbox: BBoxType, page_width: int | float) -> BBoxType: + x0, y0, x1, y1 = bbox + x0_new = page_width - x1 + x1_new = page_width - x0 + logger.info("Mirrored vertically:", x0_new, y0, x1_new, y1) + + return x0_new, y0, x1_new, y1 + + +def derotate_image(bbox: BBoxType, page_info: PageInfo) -> BBoxType: logger.debug(f"{page_info.rotation=}") match page_info.rotation: @@ -103,7 +108,7 @@ def transform_table_lines_by_page_info(bboxes: dict, offsets: tuple, page_info: unpack = itemgetter("x1", "y1", "x2", "y2") pack = lambda x: {"x1": x[0], "y1": x[1], "x2": x[2], "y2": x[3]} - convert = compose(pack, apply_offsets, derotate, transform, unpack) + convert = compose(pack, derotate, apply_offsets, transform, unpack) table_lines = bboxes.get("tableLines", []) bboxes["tableLines"] = list(map(convert, table_lines)) From 23406004edd7a1b77744249b70fe749da3e22307 Mon Sep 17 00:00:00 2001 From: iriley Date: Wed, 15 May 2024 16:38:51 +0200 Subject: [PATCH 2/2] chore: remove debug code --- pyproject.toml | 2 +- src/cv_analysis/utils/annotate.py | 4 +--- src/cv_analysis/utils/image_extraction.py | 2 -- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 3822a1c..7546d28 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "cv-analysis-service" -version = "2.16.1" +version = "2.19.0" description = "" authors = ["Isaac Riley "] readme = "README.md" diff --git a/src/cv_analysis/utils/annotate.py b/src/cv_analysis/utils/annotate.py index 9d37b14..2b6e1df 100644 --- a/src/cv_analysis/utils/annotate.py +++ b/src/cv_analysis/utils/annotate.py @@ -44,11 +44,9 @@ def annotate_page(page: fitz.Page, prediction): start = itemgetter("x1", "y1")(line) end = itemgetter("x2", "y2")(line) - bbox = *start, *end + bbox = (*start, *end) height = page.bound()[3] - logger.info(f"{height=}") bbox = mirror_horizontal(bbox, page_height=height) - assert bbox == mirror_horizontal(mirror_horizontal(bbox, height), height) start = tuple(bbox[:2]) end = tuple(bbox[2:]) diff --git a/src/cv_analysis/utils/image_extraction.py b/src/cv_analysis/utils/image_extraction.py index 7a2dece..e727021 100644 --- a/src/cv_analysis/utils/image_extraction.py +++ b/src/cv_analysis/utils/image_extraction.py @@ -58,7 +58,6 @@ def mirror_horizontal(bbox: BBoxType, page_height: int | float) -> BBoxType: x0, y0, x1, y1 = bbox y0_new = page_height - y0 y1_new = page_height - y1 - logger.info(f"Mirrored horizontally: {x0}, {y0} -> {y0_new}, {x1}, {y1} -> {y1_new}; page height: {page_height}") return x0, y0_new, x1, y1_new @@ -67,7 +66,6 @@ def mirror_vertical(bbox: BBoxType, page_width: int | float) -> BBoxType: x0, y0, x1, y1 = bbox x0_new = page_width - x1 x1_new = page_width - x0 - logger.info("Mirrored vertically:", x0_new, y0, x1_new, y1) return x0_new, y0, x1_new, y1