starpig commited on
Commit
b080d28
·
1 Parent(s): d48696a

add dataset loader

Browse files
Files changed (1) hide show
  1. dataset.py +30 -42
dataset.py CHANGED
@@ -1,56 +1,44 @@
1
  VERSION = "0.0.2"
2
-
3
- import os
4
- import json
5
- from datasets import GeneratorBasedBuilder, DatasetInfo, SplitGenerator, Split, Features, Value, Sequence, Image
 
6
 
7
  class TLPD(GeneratorBasedBuilder):
 
 
8
  def _info(self):
9
  return DatasetInfo(
10
- description="Taiwan License Plate Dataset with LabelMe polygon annotations.",
11
  features=Features({
12
- "image": Image(), # auto-decodes to PIL.Image
13
- "label": Value("string"), # e.g., 'carplate'
14
- "points": Sequence(Sequence(Value("float32"))) # list of [x, y] coords
15
  }),
16
  )
17
 
18
  def _split_generators(self, dl_manager):
19
- data_dir = dl_manager.manual_dir
20
- return [
21
- SplitGenerator(
22
- name=Split.TRAIN,
23
- gen_kwargs={
24
- "images_dir": os.path.join(data_dir, "images"),
25
- "labels_dir": os.path.join(data_dir, "labels")
26
- }
27
- )
28
- ]
29
 
30
  def _generate_examples(self, images_dir, labels_dir):
31
- for fname in sorted(os.listdir(labels_dir)):
32
- if not fname.endswith(".json"):
33
  continue
34
-
35
- label_path = os.path.join(labels_dir, fname)
36
- with open(label_path, "r") as f:
37
- data = json.load(f)
38
-
39
- image_filename = data.get("imagePath")
40
- image_path = os.path.join(images_dir, image_filename)
41
-
42
- shapes = data.get("shapes", [])
43
- if not shapes:
44
- continue # skip if no annotation
45
-
46
- label = shapes[0].get("label", "unknown")
47
- points = shapes[0].get("points", [])
48
-
49
- key = os.path.splitext(fname)[0]
50
-
51
- print("Yielding:", key, label, points)
52
- yield key, {
53
- "image": image_path,
54
- "label": label,
55
- "points": points
56
  }
 
 
1
  VERSION = "0.0.2"
2
+ import os, json
3
+ from datasets import (
4
+ GeneratorBasedBuilder, DatasetInfo, SplitGenerator, Split,
5
+ Features, Value, Sequence, Image
6
+ )
7
 
8
  class TLPD(GeneratorBasedBuilder):
9
+ VERSION = "0.0.2" # ← 只要改了版本,Hub 就會重建
10
+
11
  def _info(self):
12
  return DatasetInfo(
13
+ description="Taiwan License Plate Dataset",
14
  features=Features({
15
+ "image": Image(),
16
+ "label": Value("string"),
17
+ "points": Sequence(Sequence(Value("float32"))),
18
  }),
19
  )
20
 
21
  def _split_generators(self, dl_manager):
22
+ data_dir = dl_manager.manual_dir # = repo 根目錄
23
+ return [SplitGenerator(
24
+ name=Split.TRAIN,
25
+ gen_kwargs={
26
+ "images_dir": os.path.join(data_dir, "images"),
27
+ "labels_dir": os.path.join(data_dir, "labels"),
28
+ },
29
+ )]
 
 
30
 
31
  def _generate_examples(self, images_dir, labels_dir):
32
+ for file in sorted(os.listdir(labels_dir)):
33
+ if not file.endswith(".json"):
34
  continue
35
+ with open(os.path.join(labels_dir, file), "r") as f:
36
+ meta = json.load(f)
37
+
38
+ shape = meta["shapes"][0]
39
+ yield file[:-5], {
40
+ "image": os.path.join(images_dir, meta["imagePath"]),
41
+ "label": shape["label"],
42
+ "points": shape["points"],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  }
44
+