TLPD / dataset.py
starpig's picture
Add dataset.py and data files (images + labels) using LFS
a2feaaf
raw
history blame
1.87 kB
#ya
import os
import json
from datasets import GeneratorBasedBuilder, DatasetInfo, SplitGenerator, Split, Features, Value, Sequence, Image
class TLPD(GeneratorBasedBuilder):
def _info(self):
return DatasetInfo(
description="Taiwan License Plate Dataset with LabelMe polygon annotations.",
features=Features({
"image": Image(), # auto-decodes to PIL.Image
"label": Value("string"), # e.g., 'carplate'
"points": Sequence(Sequence(Value("float32"))) # list of [x, y] coords
}),
)
def _split_generators(self, dl_manager):
data_dir = dl_manager.manual_dir
return [
SplitGenerator(
name=Split.TRAIN,
gen_kwargs={
"images_dir": os.path.join(data_dir, "images"),
"labels_dir": os.path.join(data_dir, "labels")
}
)
]
def _generate_examples(self, images_dir, labels_dir):
for fname in sorted(os.listdir(labels_dir)):
if not fname.endswith(".json"):
continue
label_path = os.path.join(labels_dir, fname)
with open(label_path, "r") as f:
data = json.load(f)
image_filename = data.get("imagePath")
image_path = os.path.join(images_dir, image_filename)
shapes = data.get("shapes", [])
if not shapes:
continue # skip if no annotation
label = shapes[0].get("label", "unknown")
points = shapes[0].get("points", [])
key = os.path.splitext(fname)[0]
print("Yielding:", key, label, points)
yield key, {
"image": image_path,
"label": label,
"points": points
}