Datasets:
add dataset loader
Browse files- dataset.py +30 -42
dataset.py
CHANGED
|
@@ -1,56 +1,44 @@
|
|
| 1 |
VERSION = "0.0.2"
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
class TLPD(GeneratorBasedBuilder):
|
|
|
|
|
|
|
| 8 |
def _info(self):
|
| 9 |
return DatasetInfo(
|
| 10 |
-
description="Taiwan License Plate Dataset
|
| 11 |
features=Features({
|
| 12 |
-
"image":
|
| 13 |
-
"label":
|
| 14 |
-
"points": Sequence(Sequence(Value("float32")))
|
| 15 |
}),
|
| 16 |
)
|
| 17 |
|
| 18 |
def _split_generators(self, dl_manager):
|
| 19 |
-
data_dir = dl_manager.manual_dir
|
| 20 |
-
return [
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
)
|
| 28 |
-
]
|
| 29 |
|
| 30 |
def _generate_examples(self, images_dir, labels_dir):
|
| 31 |
-
for
|
| 32 |
-
if not
|
| 33 |
continue
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 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 |
+
|