| |
| 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(), |
| "label": Value("string"), |
| "points": Sequence(Sequence(Value("float32"))) |
| }), |
| ) |
|
|
| 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 |
|
|
| 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 |
| } |
|
|