sugarquark commited on
Commit
1db33f3
·
verified ·
1 Parent(s): 6628eb4

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +2 -183
README.md CHANGED
@@ -1,186 +1,5 @@
1
  ---
2
- dataset_info:
3
- features:
4
- - name: image
5
- dtype: image
6
- - name: objects
7
- struct:
8
- - name: bbox
9
- sequence:
10
- sequence: float64
11
- - name: segmentation
12
- sequence:
13
- sequence:
14
- sequence: float64
15
- - name: categories
16
- sequence: int64
17
- splits:
18
- - name: train
19
- num_bytes: 17598458856.47
20
- num_examples: 117266
21
- - name: validation
22
- num_bytes: 795110726.04
23
- num_examples: 4952
24
- download_size: 20170024873
25
- dataset_size: 18393569582.510002
26
- configs:
27
- - config_name: default
28
- data_files:
29
- - split: train
30
- path: data/train-*
31
- - split: validation
32
- path: data/validation-*
33
- task_categories:
34
- - object-detection
35
  ---
36
 
37
- # MS-COCO2017
38
-
39
- ## Use the dataset
40
-
41
- ```py
42
- from datasets import load_dataset
43
- ds = load_dataset("ariG23498/coco2017", streaming=True, split="validation")
44
-
45
- sample = next(iter(ds))
46
-
47
- from PIL import Image, ImageDraw
48
-
49
- def draw_bboxes_on_image(
50
- image: Image.Image,
51
- objects: dict,
52
- category_names: dict = None,
53
- box_color: str = "red",
54
- text_color: str = "white"
55
- ):
56
- draw = ImageDraw.Draw(image)
57
- bboxes = objects.get("bbox", [])
58
- categories = objects.get("categories", [])
59
-
60
- for i, bbox in enumerate(bboxes):
61
- x, y, width, height = bbox
62
- # PIL expects (x_min, y_min, x_max, y_max) for rectangle
63
- x_min, y_min, x_max, y_max = x, y, x + width, y + height
64
-
65
- # Draw the rectangle
66
- draw.rectangle([x_min, y_min, x_max, y_max], outline=box_color, width=2)
67
-
68
- # Get category label
69
- category_id = categories[i]
70
- label = str(category_id)
71
- if category_names and category_id in category_names:
72
- label = category_names[category_id]
73
-
74
- # Draw the category label
75
- text_bbox = draw.textbbox((x_min, y_min), label) # Use textbbox to get text size
76
- text_width = text_bbox[2] - text_bbox[0]
77
- text_height = text_bbox[3] - text_bbox[1]
78
-
79
- # Draw a filled rectangle behind the text for better readability
80
- draw.rectangle([x_min, y_min - text_height - 5, x_min + text_width + 5, y_min], fill=box_color)
81
- draw.text((x_min + 2, y_min - text_height - 2), label, fill=text_color)
82
-
83
- return image
84
-
85
- draw_bboxes_on_image(
86
- image=sample["image"],
87
- objects=sample["objects"],
88
- )
89
- ```
90
-
91
- ## Get the categories
92
-
93
- ```py
94
- import json
95
-
96
- with open("/content/annotations/instances_train2017.json") as f:
97
- instances = json.load(f)
98
-
99
- instances["categories"]
100
- ```
101
-
102
- ## Build the dataset and upload to Hub
103
-
104
- ```py
105
- !wget http://images.cocodataset.org/zips/train2017.zip
106
- !wget http://images.cocodataset.org/zips/val2017.zip
107
- !wget http://images.cocodataset.org/annotations/annotations_trainval2017.zip
108
-
109
- !unzip train2017.zip
110
- !unzip val2017.zip
111
- !unzip annotations_trainval2017.zip
112
-
113
- import json
114
- from pathlib import Path
115
- from tqdm import tqdm
116
- from huggingface_hub import upload_folder
117
- from datasets import Dataset, DatasetDict, Features, Value, Sequence, Array2D
118
- import shutil
119
-
120
- # === Paths ===
121
- base_dir = Path("/content")
122
- splits = {
123
- "train": {
124
- "image_dir": base_dir / "train2017",
125
- "annotation_file": base_dir / "annotations" / "instances_train2017.json",
126
- },
127
- "val": {
128
- "image_dir": base_dir / "val2017",
129
- "annotation_file": base_dir / "annotations" / "instances_val2017.json",
130
- }
131
- }
132
- output_dir = base_dir / "coco_imagefolder"
133
-
134
- # Clean existing directory
135
- if output_dir.exists():
136
- shutil.rmtree(output_dir)
137
- output_dir.mkdir(parents=True)
138
-
139
- def convert_coco_to_jsonl(image_dir, annotation_path, output_metadata_path):
140
- with open(annotation_path) as f:
141
- data = json.load(f)
142
-
143
- id_to_filename = {img['id']: img['file_name'] for img in data['images']}
144
- annotations_by_image = {}
145
-
146
- for ann in data['annotations']:
147
- img_id = ann['image_id']
148
- bbox = ann['bbox'] # [x, y, width, height]
149
- category = ann['category_id']
150
-
151
- if img_id not in annotations_by_image:
152
- annotations_by_image[img_id] = {
153
- "file_name": id_to_filename[img_id],
154
- "objects": {
155
- "bbox": [],
156
- "categories": []
157
- }
158
- }
159
-
160
- annotations_by_image[img_id]["objects"]["bbox"].append(bbox)
161
- annotations_by_image[img_id]["objects"]["categories"].append(category)
162
-
163
- with open(output_metadata_path, "w") as f:
164
- for img_id, metadata in annotations_by_image.items():
165
- json.dump(metadata, f)
166
- f.write("\n")
167
-
168
- # Convert and copy files to imagefolder-style structure
169
- for split, info in splits.items():
170
- split_dir = output_dir / split
171
- split_dir.mkdir(parents=True)
172
-
173
- # Copy images
174
- for img_path in tqdm(info["image_dir"].glob("*.jpg"), desc=f"Copying {split} images"):
175
- shutil.copy(img_path, split_dir / img_path.name)
176
-
177
- # Convert annotations
178
- metadata_path = split_dir / "metadata.jsonl"
179
- convert_coco_to_jsonl(split_dir, info["annotation_file"], metadata_path)
180
-
181
- # HF Dataset
182
- from datasets import load_dataset
183
-
184
- dataset = load_dataset("imagefolder", data_dir="/content/coco_imagefolder")
185
- dataset.push_to_hub("ariG23498/coco2017")
186
- ```
 
1
  ---
2
+ viewer: false
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  ---
4
 
5
+ Cloned from ariG23498.