ariG23498 HF Staff commited on
Commit
7ae95b7
·
verified ·
1 Parent(s): a133287

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +64 -53
README.md CHANGED
@@ -32,7 +32,70 @@ task_categories:
32
 
33
  # MS-COCO2017
34
 
35
- Here is the code to build the dataset
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  ```py
38
  !wget http://images.cocodataset.org/zips/train2017.zip
@@ -116,56 +179,4 @@ from datasets import load_dataset
116
 
117
  dataset = load_dataset("imagefolder", data_dir="/content/coco_imagefolder")
118
  dataset.push_to_hub("ariG23498/coco2017")
119
- ```
120
-
121
- ## Use the dataset
122
-
123
- ```py
124
- from datasets import load_dataset
125
- ds = load_dataset("ariG23498/coco2017", streaming=True, split="validation")
126
-
127
- sample = next(iter(ds))
128
-
129
- from PIL import Image, ImageDraw
130
-
131
- def draw_bboxes_on_image(
132
- image: Image.Image,
133
- objects: dict,
134
- category_names: dict = None,
135
- box_color: str = "red",
136
- text_color: str = "white"
137
- ):
138
- draw = ImageDraw.Draw(image)
139
- bboxes = objects.get("bbox", [])
140
- categories = objects.get("categories", [])
141
-
142
- for i, bbox in enumerate(bboxes):
143
- x, y, width, height = bbox
144
- # PIL expects (x_min, y_min, x_max, y_max) for rectangle
145
- x_min, y_min, x_max, y_max = x, y, x + width, y + height
146
-
147
- # Draw the rectangle
148
- draw.rectangle([x_min, y_min, x_max, y_max], outline=box_color, width=2)
149
-
150
- # Get category label
151
- category_id = categories[i]
152
- label = str(category_id)
153
- if category_names and category_id in category_names:
154
- label = category_names[category_id]
155
-
156
- # Draw the category label
157
- text_bbox = draw.textbbox((x_min, y_min), label) # Use textbbox to get text size
158
- text_width = text_bbox[2] - text_bbox[0]
159
- text_height = text_bbox[3] - text_bbox[1]
160
-
161
- # Draw a filled rectangle behind the text for better readability
162
- draw.rectangle([x_min, y_min - text_height - 5, x_min + text_width + 5, y_min], fill=box_color)
163
- draw.text((x_min + 2, y_min - text_height - 2), label, fill=text_color)
164
-
165
- return image
166
-
167
- draw_bboxes_on_image(
168
- image=sample["image"],
169
- objects=sample["objects"],
170
- )
171
  ```
 
32
 
33
  # MS-COCO2017
34
 
35
+ ## Use the dataset
36
+
37
+ ```py
38
+ from datasets import load_dataset
39
+ ds = load_dataset("ariG23498/coco2017", streaming=True, split="validation")
40
+
41
+ sample = next(iter(ds))
42
+
43
+ from PIL import Image, ImageDraw
44
+
45
+ def draw_bboxes_on_image(
46
+ image: Image.Image,
47
+ objects: dict,
48
+ category_names: dict = None,
49
+ box_color: str = "red",
50
+ text_color: str = "white"
51
+ ):
52
+ draw = ImageDraw.Draw(image)
53
+ bboxes = objects.get("bbox", [])
54
+ categories = objects.get("categories", [])
55
+
56
+ for i, bbox in enumerate(bboxes):
57
+ x, y, width, height = bbox
58
+ # PIL expects (x_min, y_min, x_max, y_max) for rectangle
59
+ x_min, y_min, x_max, y_max = x, y, x + width, y + height
60
+
61
+ # Draw the rectangle
62
+ draw.rectangle([x_min, y_min, x_max, y_max], outline=box_color, width=2)
63
+
64
+ # Get category label
65
+ category_id = categories[i]
66
+ label = str(category_id)
67
+ if category_names and category_id in category_names:
68
+ label = category_names[category_id]
69
+
70
+ # Draw the category label
71
+ text_bbox = draw.textbbox((x_min, y_min), label) # Use textbbox to get text size
72
+ text_width = text_bbox[2] - text_bbox[0]
73
+ text_height = text_bbox[3] - text_bbox[1]
74
+
75
+ # Draw a filled rectangle behind the text for better readability
76
+ draw.rectangle([x_min, y_min - text_height - 5, x_min + text_width + 5, y_min], fill=box_color)
77
+ draw.text((x_min + 2, y_min - text_height - 2), label, fill=text_color)
78
+
79
+ return image
80
+
81
+ draw_bboxes_on_image(
82
+ image=sample["image"],
83
+ objects=sample["objects"],
84
+ )
85
+ ```
86
+
87
+ ## Get the categories
88
+
89
+ ```py
90
+ import json
91
+
92
+ with open("/content/annotations/instances_train2017.json") as f:
93
+ instances = json.load(f)
94
+
95
+ instances["categories"]
96
+ ```
97
+
98
+ ## Build the dataset and upload to Hub
99
 
100
  ```py
101
  !wget http://images.cocodataset.org/zips/train2017.zip
 
179
 
180
  dataset = load_dataset("imagefolder", data_dir="/content/coco_imagefolder")
181
  dataset.push_to_hub("ariG23498/coco2017")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  ```