Datasets:
Add dataset loading script
Browse files- dataset.py +39 -0
dataset.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.# dataset.py
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from datasets import GeneratorBasedBuilder, DatasetInfo, SplitGenerator, Split, Features, Value, Sequence, Image
|
| 5 |
+
|
| 6 |
+
class TLPD(GeneratorBasedBuilder):
|
| 7 |
+
def _info(self):
|
| 8 |
+
return DatasetInfo(
|
| 9 |
+
features=Features({
|
| 10 |
+
"image": Image(),
|
| 11 |
+
"label": Value("string"),
|
| 12 |
+
"points": Sequence(Sequence(Value("float32")))
|
| 13 |
+
}),
|
| 14 |
+
description="TLPD: Taiwan License Plate Dataset with polygon labels from LabelMe.",
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
def _split_generators(self, dl_manager):
|
| 18 |
+
return [
|
| 19 |
+
SplitGenerator(
|
| 20 |
+
name=Split.TRAIN,
|
| 21 |
+
gen_kwargs={"images_dir": "images", "labels_dir": "labels"}
|
| 22 |
+
)
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
def _generate_examples(self, images_dir, labels_dir):
|
| 26 |
+
for fname in sorted(os.listdir(labels_dir)):
|
| 27 |
+
if fname.endswith(".json"):
|
| 28 |
+
json_path = os.path.join(labels_dir, fname)
|
| 29 |
+
with open(json_path, "r") as f:
|
| 30 |
+
data = json.load(f)
|
| 31 |
+
image_name = data["imagePath"]
|
| 32 |
+
image_path = os.path.join(images_dir, image_name)
|
| 33 |
+
points = data["shapes"][0]["points"]
|
| 34 |
+
label = data["shapes"][0]["label"]
|
| 35 |
+
yield fname, {
|
| 36 |
+
"image": image_path,
|
| 37 |
+
"label": label,
|
| 38 |
+
"points": points
|
| 39 |
+
}
|