Ziyuan111 commited on
Commit
7799d1d
1 Parent(s): 3893cfc

Update plantsdataset.py

Browse files
Files changed (1) hide show
  1. plantsdataset.py +17 -39
plantsdataset.py CHANGED
@@ -1,31 +1,21 @@
1
- from datasets import DatasetInfo, Features, Value, ClassLabel, Split, SplitGenerator, GeneratorBasedBuilder, BuilderConfig
2
  import os
3
  from PIL import Image
4
  import matplotlib.pyplot as plt
5
  import numpy as np
6
 
7
- # Rest of your code...
8
-
9
- # Google Drive ID for your ZIP file
10
  _DRIVE_ID = "1fXgVwhdU5YGj0SPIcHxSpxkhvRh54oEH"
11
  _URL = f"https://drive.google.com/uc?export=download&id={_DRIVE_ID}"
12
 
13
  class PlantsDataset(GeneratorBasedBuilder):
14
- class MyConfig(BuilderConfig):
15
- def __init__(self, **kwargs):
16
- super().__init__(**kwargs)
17
-
18
- BUILDER_CONFIGS = [
19
- MyConfig(name="plants_default", description="Default configuration for PlantsDataset"),
20
- ]
21
-
22
  BUILDER_CONFIGS = [
23
- MyConfig(name="default", description="Default configuration"),
24
  ]
25
 
26
  def _info(self):
27
  features = Features({
28
- "image": Value("string"),
29
  "label": ClassLabel(names=["aleo vera", "calotropis gigantea"]),
30
  })
31
  return DatasetInfo(
@@ -56,31 +46,19 @@ class PlantsDataset(GeneratorBasedBuilder):
56
 
57
  def _generate_examples(self, data_folder):
58
  label_names = self.info.features['label'].names
59
- for label, subfolder in enumerate(label_names):
60
- subfolder_path = os.path.join(data_folder, subfolder)
 
61
  for root, _, files in os.walk(subfolder_path):
62
  for file_name in files:
63
  file_path = os.path.join(root, file_name)
64
- if os.path.isfile(file_path):
65
- # Open the image using Pillow and convert it to an array
66
- with Image.open(file_path) as image:
67
- image_array = np.array(image)
68
-
69
- # Yield the example with the image data and label
70
- yield file_path, {
71
- "image": image_array.tolist(), # Convert array to list
72
- "label": label,
73
- }
74
-
75
- def _display_image(self, image_path, label):
76
- with Image.open(image_path) as img:
77
- plt.imshow(img)
78
- plt.title(f"Label: {self.info.features['label'].int2str(label)}")
79
- plt.axis('off') # Hide the axis
80
- plt.show()
81
-
82
- # Create an instance of the PlantsDataset class
83
- plants_dataset = PlantsDataset()
84
-
85
- # Build and upload the dataset
86
- plants_dataset.download_and_prepare()
 
1
+ from datasets import DatasetInfo, Features, Value, ClassLabel, Split, SplitGenerator, GeneratorBasedBuilder, BuilderConfig, Array3D
2
  import os
3
  from PIL import Image
4
  import matplotlib.pyplot as plt
5
  import numpy as np
6
 
 
 
 
7
  _DRIVE_ID = "1fXgVwhdU5YGj0SPIcHxSpxkhvRh54oEH"
8
  _URL = f"https://drive.google.com/uc?export=download&id={_DRIVE_ID}"
9
 
10
  class PlantsDataset(GeneratorBasedBuilder):
11
+ VERSION = datasets.Version("1.0.0")
 
 
 
 
 
 
 
12
  BUILDER_CONFIGS = [
13
+ BuilderConfig(name="default", version=VERSION, description="Default configuration for PlantsDataset"),
14
  ]
15
 
16
  def _info(self):
17
  features = Features({
18
+ "image": Array3D(dtype="uint8", shape=(None, None, 3)), # Change to Array3D to store image arrays
19
  "label": ClassLabel(names=["aleo vera", "calotropis gigantea"]),
20
  })
21
  return DatasetInfo(
 
46
 
47
  def _generate_examples(self, data_folder):
48
  label_names = self.info.features['label'].names
49
+ for label_name in label_names:
50
+ subfolder_path = os.path.join(data_folder, label_name)
51
+ label = label_names.index(label_name)
52
  for root, _, files in os.walk(subfolder_path):
53
  for file_name in files:
54
  file_path = os.path.join(root, file_name)
55
+ if os.path.isfile(file_path) and file_name.lower().endswith(('.png', '.jpg', '.jpeg')):
56
+ try:
57
+ with Image.open(file_path) as image:
58
+ image_array = np.array(image)
59
+ yield file_path, {
60
+ "image": image_array, # Keep as numpy array
61
+ "label": label,
62
+ }
63
+ except (IOError, OSError):
64
+ print(f"Skipped file {file_path}, due to an error opening the image.")