Update dataset.py
Browse files- dataset.py +27 -9
dataset.py
CHANGED
@@ -1,18 +1,36 @@
|
|
1 |
from datasets import load_dataset, Features, ClassLabel, Image
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
# Define features including metadata
|
4 |
features = Features({
|
5 |
'image': Image(),
|
6 |
'label': ClassLabel(names=['apple', 'orange', 'banana', 'phone', 'halfapple', 'applephone', 'fountainpen', 'teacherstudent', 'teacherandstudent'])
|
7 |
})
|
8 |
|
9 |
-
|
10 |
-
dataset =
|
11 |
-
'path/to/dataset/script',
|
12 |
-
data_files={
|
13 |
-
'metadata': 'metadata.csv',
|
14 |
-
'images': 'data/*'
|
15 |
-
},
|
16 |
-
features=features
|
17 |
-
)
|
18 |
|
|
|
1 |
from datasets import load_dataset, Features, ClassLabel, Image
|
2 |
|
3 |
+
|
4 |
+
def process_data(images_directory, metadata_file):
|
5 |
+
data = []
|
6 |
+
|
7 |
+
# Read the metadata file
|
8 |
+
with open(metadata_file, mode='r', encoding='utf-8') as file:
|
9 |
+
reader = csv.DictReader(file)
|
10 |
+
for row in reader:
|
11 |
+
image_path = os.path.join(images_directory, row['image_id'])
|
12 |
+
if os.path.exists(image_path):
|
13 |
+
# Open the image and convert it to a consistent format (e.g., RGB)
|
14 |
+
with Image.open(image_path) as img:
|
15 |
+
img = img.convert('RGB')
|
16 |
+
data.append({
|
17 |
+
'image': img,
|
18 |
+
'label': row['label']
|
19 |
+
})
|
20 |
+
else:
|
21 |
+
print(f"Image {image_path} not found.")
|
22 |
+
|
23 |
+
return data
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
# Define features including metadata
|
29 |
features = Features({
|
30 |
'image': Image(),
|
31 |
'label': ClassLabel(names=['apple', 'orange', 'banana', 'phone', 'halfapple', 'applephone', 'fountainpen', 'teacherstudent', 'teacherandstudent'])
|
32 |
})
|
33 |
|
34 |
+
data = process_data("/data", "metadata.csv")
|
35 |
+
dataset = Dataset.from_dict(data, features=features)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|