fix sorting

This commit is contained in:
2026-09-08 17:00:18 +02:00
parent bf05272797
commit 13a08f6cbd
3 changed files with 111 additions and 15 deletions
+31
View File
@@ -340,6 +340,34 @@ def group_images(image_files: list[Path]) -> dict[str, list[Path]]:
return dict(groups)
def sort_boxes_for_image(
workspace: EvaluationWorkspace,
image_file: Path,
boxes: list[BoxItem],
) -> list[BoxItem]:
"""Sort boxes in the image's page-column reading order when schema exists."""
match = re.match(r"(.+)_(\d+)$", image_file.stem)
if not match:
return boxes
schema_path = workspace.cutleft_dir / f"{match.group(1)}_schema.json"
try:
schema = read_json(schema_path)
columns_per_file = schema["columns_per_file"]
column_count = int(columns_per_file[int(match.group(2)) - 1])
if column_count < 1:
return boxes
except (OSError, KeyError, IndexError, TypeError, ValueError):
return boxes
def position(item: BoxItem) -> tuple[int, int, int]:
ymin, xmin, _ymax, xmax = item.box_2d
center_x = (xmin + xmax) // 2
column = min(column_count - 1, max(0, center_x * column_count // 1000))
return column, ymin, xmin
return sorted(boxes, key=position)
def _existing_context(output_json: Path) -> list[str]:
try:
loaded = read_json(output_json)
@@ -452,6 +480,9 @@ def process_copy_group(
continue
annotation.name = "Unknown"
annotation.list = sort_boxes_for_image(
workspace, image_file, annotation.list
)
atomic_write_json(output_json, annotation.model_dump())
accumulated_labels.extend(box.label for box in annotation.list)
generated += 1
+3 -15
View File
@@ -333,28 +333,16 @@ class ImageViewer:
try:
current_data = read_json(self.current_json_path)
nb_pages = self.current_meta["schema"]["columns_per_file"][
self.current_meta["part"] - 1
]
original_items = current_data["list"]
ordered_items = sort_bounding_boxes(original_items, nb_pages)
if ordered_items != original_items:
current_data["list"] = ordered_items
atomic_write_json(self.current_json_path, current_data)
print(
f"Reordered labels by column in "
f"{self.current_json_path.name}."
)
items = current_data["list"]
# Perform the conversion now, post-edit
converted_items = convert_list(
ordered_items,
items,
self.current_meta["part"],
self.current_meta["schema"]
)
labels = normalized_labels(ordered_items)
labels = normalized_labels(items)
false_labels = [
label for label in labels if label not in self.valid_labels
]