Files
ZLD_POC/tests/backend/test_layout_cv.py
2026-04-15 17:18:49 +08:00

34 lines
1.1 KiB
Python

import cv2
import numpy as np
from backend.app.layout_cv import Box, detect_text_lines, merge_text_and_rectangles
def test_merge_text_and_rectangles_keeps_outer_table_box_and_drops_nested_cells() -> None:
text_lines = [
Box(20, 20, 120, 36, "line", "配料"),
Box(20, 40, 120, 56, "line", "糯米"),
Box(20, 60, 120, 76, "line", "红豆"),
]
rectangles = [
Box(10, 10, 150, 90, "rectangle"),
Box(12, 12, 78, 44, "rectangle"),
Box(82, 12, 148, 44, "rectangle"),
]
merged = merge_text_and_rectangles(text_lines, rectangles)
assert [box.kind for box in merged] == ["rectangle", "line", "line", "line"]
assert merged[0].as_tuple() == (10, 10, 150, 90)
def test_detect_text_lines_finds_two_text_rows_without_ocr() -> None:
image = np.full((220, 420, 3), 255, dtype=np.uint8)
cv2.putText(image, "LINE ONE", (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(image, "LINE TWO", (20, 140), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 0), 2, cv2.LINE_AA)
lines = detect_text_lines(image)
assert len(lines) == 2
assert lines[0].y1 < lines[1].y0