| from datasets import ( |
| GeneratorBasedBuilder, DatasetInfo, SplitGenerator, Split, |
| Features, Value, Sequence, Image, Version |
| ) |
| import os, json |
|
|
| class TLPD(GeneratorBasedBuilder): |
| VERSION = Version("0.0.3") |
|
|
| def _info(self): |
| return DatasetInfo( |
| description="Taiwan License Plate Dataset with LabelMe polygon annotations.", |
| features=Features({ |
| "image": Image(), |
| "label": Value("string"), |
| |
| "points": Value("string"), |
| }), |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| root = dl_manager.manual_dir or dl_manager.download_and_extract({}) |
| return [SplitGenerator( |
| name=Split.TRAIN, |
| gen_kwargs={ |
| "images_dir": os.path.join(root, "images"), |
| "labels_dir": os.path.join(root, "labels"), |
| }, |
| )] |
|
|
| def _generate_examples(self, images_dir, labels_dir): |
| for fname in sorted(os.listdir(labels_dir)): |
| if not fname.endswith(".json"): |
| continue |
| with open(os.path.join(labels_dir, fname), "r") as f: |
| meta = json.load(f) |
|
|
| shape = meta["shapes"][0] |
| yield fname[:-5], { |
| "image": os.path.join(images_dir, meta["imagePath"]), |
| "label": shape["label"], |
| "points": json.dumps(shape["points"]), |
| } |
|
|