"""View COCO boxes and masks of an IASSET synthetic dataset in FiftyOne. The COCO file_name values look like "images/000000.png", but the images live in yolo/images/{train,val}. This script builds a temporary folder of symlinks in which those names resolve, drops COCO images that have no file on disk, and opens the result in the FiftyOne app. Example python annotations_visualizer.py --dataset /media/rtx5090/IASSET/Synthetic_Train_Sets/Additive_Ablation_Study/4K_Baseline """ import argparse import json from pathlib import Path import fiftyone as fo p = argparse.ArgumentParser() p.add_argument("--dataset", default="/media/rtx5090/IASSET/Synthetic_Train_Sets/Additive_Ablation_Study/4K_Baseline") p.add_argument("--coco", help="COCO json (default: /coco/coco_annotations.json)") p.add_argument("--images", help="folder with the train/val image folders (default: /yolo/images)") p.add_argument("--tmp", default="/tmp/fiftyone_view") a = p.parse_args() root = Path(a.dataset) coco_path = Path(a.coco) if a.coco else root / "coco" / "coco_annotations.json" img_root = Path(a.images) if a.images else root / "yolo" / "images" tmp = Path(a.tmp) / root.name (tmp / "images").mkdir(parents=True, exist_ok=True) # 1) flat folder in which "images/" (the COCO file_name) resolves n_link, n_clash = 0, 0 for split in sorted(d for d in img_root.iterdir() if d.is_dir()): for f in split.iterdir(): link = tmp / "images" / f.name if link.is_symlink() and not link.exists(): link.unlink() # broken link (original moved): recreate it if link.is_symlink() and link.resolve() == f.resolve(): continue # already linked by an earlier run if link.is_symlink() or link.exists(): n_clash += 1 # same name already linked to a different file: the first one wins continue link.symlink_to(f) n_link += 1 print(f"{n_link} image links created, {n_clash} name clashes") # 2) keep only COCO images that exist on disk coco = json.load(open(coco_path)) keep = {im["id"] for im in coco["images"] if (tmp / im["file_name"]).exists()} print(f"{len(coco['images']) - len(keep)} of {len(coco['images'])} COCO images have no file and are dropped") coco["images"] = [im for im in coco["images"] if im["id"] in keep] coco["annotations"] = [an for an in coco["annotations"] if an["image_id"] in keep] filtered = tmp / "coco_filtered.json" json.dump(coco, open(filtered, "w")) # 3) load and show (boxes and, if present in the file, instance masks) name = f"coco_{root.name}" if fo.dataset_exists(name): fo.delete_dataset(name) ds = fo.Dataset.from_dir( dataset_type=fo.types.COCODetectionDataset, data_path=str(tmp), labels_path=str(filtered), label_types=["detections", "segmentations"], name=name, ) print(ds) session = fo.launch_app(ds) session.wait()