|
import fiftyone as fo |
|
import fiftyone.utils.hf_hub as fouh |
|
import datasets |
|
|
|
class FashionpediaLoader(fouh.BaseHuggingFaceLoader): |
|
|
|
def load(self): |
|
if isinstance(self.hf_dataset, datasets.DatasetDict): |
|
split_names = list(self.hf_dataset.keys()) |
|
self.hf_dataset = self.hf_dataset[split_names[0]] |
|
if "name" in self.kwargs: |
|
self.kwargs.pop("name") |
|
dataset = fo.Dataset(name="fashionopedia-val", **self.kwargs) |
|
|
|
label_classes = self.hf_dataset.features['objects'].feature['category'].names |
|
|
|
samples = [] |
|
|
|
download_dir = fouh._get_download_dir(self.repo_id) |
|
|
|
for i, item in enumerate(self.hf_dataset): |
|
image = item['image'] |
|
basename = f"image_{i}" |
|
save_path = fouh._save_PIL_image_to_disk(image, download_dir, basename) |
|
|
|
width, height = item['width'], item['height'] |
|
|
|
objs = item['objects'] |
|
categories = objs['category'] |
|
bboxes = objs['bbox'] |
|
dets = [] |
|
|
|
for cat, bbox in zip(categories, bboxes): |
|
x0, y0, x1, y1 = bbox |
|
x0n, y0n, x1n, y1n = x0/width, y0/height, x1/width, y1/height |
|
fo_bbox = [x0n, y0n, x1n-x0n, y1n-y0n] |
|
label = label_classes[cat] |
|
dets.append(fo.Detection(label=label, bounding_box=fo_bbox)) |
|
|
|
detections = fo.Detections(detections=dets) |
|
|
|
samples.append(fo.Sample(filepath=save_path, objs=detections)) |
|
dataset.add_samples(samples) |
|
return dataset |
|
|
|
|
|
|
|
|
|
|
|
|