Ziyuan111 commited on
Commit
afc93ac
1 Parent(s): 8ccdc83

Update plantsdataset.py

Browse files
Files changed (1) hide show
  1. plantsdataset.py +33 -29
plantsdataset.py CHANGED
@@ -1,30 +1,20 @@
1
- # -*- coding: utf-8 -*-
2
- """PlantsDataset
3
-
4
- Automatically generated by Colaboratory.
5
-
6
- Original file is located at
7
- https://colab.research.google.com/drive/13QeVLQP2QGaxkGcaoMi25geoBxHQ_3rQ
8
- """
9
-
10
- from datasets import Dataset, DatasetInfo, Features, Value, Split, DownloadManager, GeneratorBasedBuilder
11
- from datasets import SplitGenerator
12
- # URL for the datasethttps://drive.google.com/file/d/1fXgVwhdU5YGj0SPIcHxSpxkhvRh54oEH/view?usp=share_link
13
- import os
14
  from datasets import Dataset, DatasetInfo, Features, Value, Split, GeneratorBasedBuilder
 
 
 
 
15
 
16
  # Google Drive ID for your ZIP file
17
  _DRIVE_ID = "1fXgVwhdU5YGj0SPIcHxSpxkhvRh54oEH"
18
  _URL = f"https://drive.google.com/uc?export=download&id={_DRIVE_ID}"
19
 
20
- # Custom Dataset Class
21
  class PlantsDataset(GeneratorBasedBuilder):
22
  VERSION = "1.0.0"
23
 
24
  def _info(self):
25
  features = Features({
26
- "image": Value("string"), # Replace with the actual datatype for your image data
27
- "label": Value("int64"), # Replace with the actual datatype for your label data
28
  })
29
  return DatasetInfo(
30
  description="Your dataset description",
@@ -53,23 +43,37 @@ class PlantsDataset(GeneratorBasedBuilder):
53
  ]
54
 
55
  def _generate_examples(self, data_folder):
56
- for label, subfolder in enumerate(["gigantic", "small"]):
57
- subfolder_path = os.path.join(data_folder, subfolder)
58
- for root, _, files in os.walk(subfolder_path):
59
- for file_name in files:
60
- file_path = os.path.join(root, file_name)
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- # Check if file_path is a file, not a directory
63
- if os.path.isfile(file_path):
64
- # Yield the example
65
- yield {
66
- "image": file_path,
67
- "label": label,
68
- }
69
 
 
 
 
 
 
70
 
71
  # Create an instance of the PlantsDataset class
72
  plants_dataset = PlantsDataset()
73
 
74
  # Build and upload the dataset
75
- plants_dataset.download_and_prepare()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from datasets import Dataset, DatasetInfo, Features, Value, Split, GeneratorBasedBuilder
2
+ import matplotlib.pyplot as plt
3
+ import random
4
+ import os
5
+ import cv2
6
 
7
  # Google Drive ID for your ZIP file
8
  _DRIVE_ID = "1fXgVwhdU5YGj0SPIcHxSpxkhvRh54oEH"
9
  _URL = f"https://drive.google.com/uc?export=download&id={_DRIVE_ID}"
10
 
 
11
  class PlantsDataset(GeneratorBasedBuilder):
12
  VERSION = "1.0.0"
13
 
14
  def _info(self):
15
  features = Features({
16
+ "image": Value("string"),
17
+ "label": Value("int64"),
18
  })
19
  return DatasetInfo(
20
  description="Your dataset description",
 
43
  ]
44
 
45
  def _generate_examples(self, data_folder):
46
+ displayed_index = -1
47
+
48
+ for label, subfolder in enumerate(["aleo vera", "calotropis gigantea"]):
49
+ subfolder_path = os.path.join(data_folder, subfolder)
50
+ for root, _, files in os.walk(subfolder_path):
51
+ for file_name in files:
52
+ file_path = os.path.join(root, file_name)
53
+
54
+ if os.path.isfile(file_path):
55
+ # Display the image
56
+ displayed_index = (displayed_index + 1) % (len(self._datasets['train']) + len(self._datasets['test']))
57
+ if displayed_index < len(self._datasets['train']):
58
+ subset_name = 'train'
59
+ example = self._datasets[subset_name][displayed_index]
60
+ else:
61
+ subset_name = 'test'
62
+ example = self._datasets[subset_name][displayed_index - len(self._datasets['train'])]
63
 
64
+ image = cv2.imread(example['image'])
65
+ plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
66
+ plt.title(f"Subset: {subset_name}, Label: {example['label']}")
67
+ plt.show()
 
 
 
68
 
69
+ # Yield the example
70
+ yield {
71
+ "image": file_path,
72
+ "label": label,
73
+ }
74
 
75
  # Create an instance of the PlantsDataset class
76
  plants_dataset = PlantsDataset()
77
 
78
  # Build and upload the dataset
79
+ plants_dataset.download_and_prepare()