chore: debug coordinate remapping logic, esp mirroring

This commit is contained in:
iriley 2024-05-15 16:33:58 +02:00
parent 3b8d6eda04
commit b0467f2335
2 changed files with 33 additions and 14 deletions

View File

@ -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

View File

@ -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))