Fixing annotations in database

This commit is contained in:
2026-01-21 08:51:39 +02:00
parent d03ffdc4d0
commit 3c8247b3bc
6 changed files with 589 additions and 17 deletions

View File

@@ -905,9 +905,14 @@ class TrainingTab(QWidget):
if stats["registered_images"]:
message += f" {stats['registered_images']} image(s) had database-backed annotations."
if stats["missing_records"]:
message += (
f" {stats['missing_records']} image(s) had no database entry; empty label files were written."
)
preserved = stats.get("preserved_existing_labels", 0)
if preserved:
message += (
f" {stats['missing_records']} image(s) had no database annotations; "
f"preserved {preserved} existing label file(s) (no overwrite)."
)
else:
message += f" {stats['missing_records']} image(s) had no database annotations; empty label files were written."
split_messages.append(message)
for msg in split_messages:
@@ -929,6 +934,7 @@ class TrainingTab(QWidget):
processed_images = 0
registered_images = 0
missing_records = 0
preserved_existing_labels = 0
total_annotations = 0
for image_file in images_dir.rglob("*"):
@@ -950,6 +956,12 @@ class TrainingTab(QWidget):
else:
missing_records += 1
# If the database has no entry for this image, do not overwrite an existing label file
# with an empty one (preserve any manually created labels on disk).
if not found and label_path.exists():
preserved_existing_labels += 1
continue
annotations_written = 0
with open(label_path, "w", encoding="utf-8") as handle:
for entry in annotation_entries:
@@ -979,6 +991,7 @@ class TrainingTab(QWidget):
"processed_images": processed_images,
"registered_images": registered_images,
"missing_records": missing_records,
"preserved_existing_labels": preserved_existing_labels,
"total_annotations": total_annotations,
}
@@ -1008,6 +1021,10 @@ class TrainingTab(QWidget):
resolved_image = image_path.resolve()
candidates: List[str] = []
# Some DBs store absolute paths in `images.relative_path`.
# Include the absolute resolved path as a lookup candidate.
candidates.append(resolved_image.as_posix())
for base in (dataset_root, images_dir):
try:
relative = resolved_image.relative_to(base.resolve()).as_posix()
@@ -1032,6 +1049,13 @@ class TrainingTab(QWidget):
return False, []
annotations = self.db_manager.get_annotations_for_image(image_row["id"]) or []
# Treat "found" as "has database-backed annotations".
# If the image exists in DB but has no annotations yet, we don't want to overwrite
# an existing label file on disk with an empty one.
if not annotations:
return False, []
yolo_entries: List[Dict[str, Any]] = []
for ann in annotations: