Datasets:
Upload MCFDataset.py loader
Browse files- MCFDataset.py +69 -0
MCFDataset.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
import cv2
|
| 5 |
+
|
| 6 |
+
REPO_ID = "OCEAN-Lab/MCF-Endface"
|
| 7 |
+
LOCAL_DATASET_DIR = Path("train_data_216")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class MCFDataset:
|
| 11 |
+
"""MCF end-face images + annotations loaded straight from annotations.json.
|
| 12 |
+
|
| 13 |
+
root: dataset folder (annotations.json + images/) or the annotations.json path.
|
| 14 |
+
None -> LOCAL_DATASET_DIR if it exists, else download REPO_ID from Hugging Face.
|
| 15 |
+
Each item is the annotation dict from annotations.json (cores/marker unchanged)
|
| 16 |
+
plus "image": the BGR numpy array from cv2.imread(..., cv2.IMREAD_UNCHANGED).
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
def __init__(self, root=None):
|
| 20 |
+
root = Path(root) if root is not None else self._default_root()
|
| 21 |
+
json_path = root if root.suffix == ".json" else root / "annotations.json"
|
| 22 |
+
self.images_dir = json_path.parent / "images"
|
| 23 |
+
|
| 24 |
+
with open(json_path, "r") as f:
|
| 25 |
+
data = json.load(f)
|
| 26 |
+
self.info = data.get("info", {})
|
| 27 |
+
|
| 28 |
+
self.images_data = []
|
| 29 |
+
for img_data in sorted(data["images"], key=lambda d: d["id"]):
|
| 30 |
+
if (self.images_dir / img_data["file_name"]).is_file():
|
| 31 |
+
self.images_data.append(img_data)
|
| 32 |
+
else:
|
| 33 |
+
print(f" Missing {img_data['file_name']} — skipping.")
|
| 34 |
+
self._index_by_id = {d["id"]: i for i, d in enumerate(self.images_data)}
|
| 35 |
+
|
| 36 |
+
@staticmethod
|
| 37 |
+
def _default_root():
|
| 38 |
+
if LOCAL_DATASET_DIR.is_dir():
|
| 39 |
+
return LOCAL_DATASET_DIR
|
| 40 |
+
from huggingface_hub import snapshot_download
|
| 41 |
+
return Path(snapshot_download(repo_id=REPO_ID, repo_type="dataset",
|
| 42 |
+
allow_patterns=["annotations.json", "images/*.jpg"]))
|
| 43 |
+
|
| 44 |
+
def __len__(self):
|
| 45 |
+
return len(self.images_data)
|
| 46 |
+
|
| 47 |
+
def __getitem__(self, idx):
|
| 48 |
+
img_data = self.images_data[idx]
|
| 49 |
+
img_path = self.images_dir / img_data["file_name"]
|
| 50 |
+
image = cv2.imread(str(img_path), cv2.IMREAD_UNCHANGED)
|
| 51 |
+
if image is None:
|
| 52 |
+
raise FileNotFoundError(f"Could not read {img_path}")
|
| 53 |
+
return {**img_data, "image": image}
|
| 54 |
+
|
| 55 |
+
def __iter__(self):
|
| 56 |
+
for idx in range(len(self)):
|
| 57 |
+
yield self[idx]
|
| 58 |
+
|
| 59 |
+
def get_by_id(self, image_id):
|
| 60 |
+
return self[self._index_by_id[image_id]]
|
| 61 |
+
|
| 62 |
+
def get_pair(self, idx):
|
| 63 |
+
"""(X item, Y item) for the pair containing idx, or None if unpaired."""
|
| 64 |
+
item = self[idx]
|
| 65 |
+
partner_id = item.get("pair_image_id")
|
| 66 |
+
if partner_id not in self._index_by_id:
|
| 67 |
+
return None
|
| 68 |
+
partner = self.get_by_id(partner_id)
|
| 69 |
+
return (item, partner) if item["side"] == "X" else (partner, item)
|