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 2689bac..2b6e1df 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,14 @@ 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] + bbox = mirror_horizontal(bbox, page_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..e727021 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,23 @@ 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 - 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 + + 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 +106,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))