jamarks commited on
Commit
7830d46
1 Parent(s): 7935eb3

Create fiftyone.py

Browse files
Files changed (1) hide show
  1. fiftyone.py +46 -0
fiftyone.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import fiftyone as fo
2
+ import fiftyone.utils.hf_hub as fouh
3
+
4
+ class FashionopediaLoader(fouh.BaseHuggingFaceLoader):
5
+
6
+ def load(self):
7
+ if "name" in self.kwargs:
8
+ self.kwargs.pop("name")
9
+ dataset = fo.Dataset(name="fashionopedia-val", **self.kwargs)
10
+
11
+ label_classes = self.hf_dataset.features['objects'].feature['category'].names
12
+
13
+ samples = []
14
+
15
+ download_dir = fouh._get_download_dir(self.repo_id)
16
+
17
+ for i, item in enumerate(self.hf_dataset):
18
+ image = item['image']
19
+ basename = f"image_{i}"
20
+ save_path = _save_PIL_image_to_disk(image, download_dir, basename)
21
+
22
+ width, height = item['width'], item['height']
23
+
24
+ objs = item['objects']
25
+ categories = objs['category']
26
+ bboxes = objs['bbox']
27
+ dets = []
28
+
29
+ for cat, bbox in zip(categories, bboxes):
30
+ x0, y0, x1, y1 = bbox
31
+ x0n, y0n, x1n, y1n = x0/width, y0/height, x1/width, y1/height
32
+ fo_bbox = [x0n, y0n, x1n-x0n, y1n-y0n]
33
+ label = label_classes[cat]
34
+ dets.append(fo.Detection(label=label, bounding_box=fo_bbox))
35
+
36
+ detections = fo.Detections(detections=dets)
37
+
38
+ samples.append(fo.Sample(filepath=save_path, objs=detections))
39
+ dataset.add_samples(samples)
40
+ return dataset
41
+
42
+
43
+
44
+
45
+
46
+