Maats commited on
Commit
ebdcebe
·
verified ·
1 Parent(s): 95de0e0

Create format-test.py

Browse files
Files changed (1) hide show
  1. format-test.py +43 -0
format-test.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from datasets import DatasetInfo, GeneratorBasedBuilder, SplitGenerator, Split, Value, Features
3
+
4
+ class MyDataset(GeneratorBasedBuilder):
5
+ def _info(self):
6
+ return DatasetInfo(
7
+ description="Custom dataset with images and labels in separate folders",
8
+ features=Features({
9
+ "image": Value("string"), # filepath, will cast later
10
+ "label": Value("string")
11
+ }),
12
+ )
13
+
14
+ def _split_generators(self, dl_manager):
15
+ data_dir = self.config.data_dir or ""
16
+ return [
17
+ SplitGenerator(name=Split.TRAIN, gen_kwargs={"split_dir": os.path.join(data_dir, "train")}),
18
+ SplitGenerator(name=Split.TEST, gen_kwargs={"split_dir": os.path.join(data_dir, "test")}),
19
+ ]
20
+
21
+ def _generate_examples(self, split_dir):
22
+ image_dir = os.path.join(split_dir, "images")
23
+ label_dir = os.path.join(split_dir, "labels")
24
+
25
+ idx = 0
26
+ for image_name in sorted(os.listdir(image_dir)):
27
+ if not image_name.lower().endswith((".jpg", ".png", ".jpeg")):
28
+ continue
29
+
30
+ image_path = os.path.join(image_dir, image_name)
31
+ label_path = os.path.join(label_dir, os.path.splitext(image_name)[0] + ".txt")
32
+
33
+ if not os.path.exists(label_path):
34
+ continue
35
+
36
+ with open(label_path, "r") as f:
37
+ label = f.read().strip()
38
+
39
+ yield idx, {
40
+ "image": image_path,
41
+ "label": label,
42
+ }
43
+ idx += 1