File size: 1,687 Bytes
7830d46
 
642fb36
7830d46
e23beaf
7830d46
 
642fb36
a6dc4a3
 
7830d46
 
 
 
 
 
 
 
 
 
 
 
 
b8562c6
7830d46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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