visible-watermark-pita / visible-watermark-pita.py
bastienp's picture
feat: text config
41f1c8a
raw
history blame
3.21 kB
import os
from glob import glob
import datasets
import json
from PIL import Image
_DESCRIPTION = """\
Watermark Dataset
"""
_VERSION = datasets.Version("1.0.0")
class WatermarkPitaConfig(datasets.BuilderConfig):
"""Builder Config for Food-101"""
def __init__(self, repository, urls, categories, **kwargs):
"""BuilderConfig for Food-101.
Args:
repository: `string`, the name of the repository.
urls: `dict<string, string>`, the urls to the data.
categories: `list<string>`, the categories of the data.
**kwargs: keyword arguments forwarded to super.
"""
_VERSION = datasets.Version("1.0.0")
super(WatermarkPitaConfig, self).__init__(version=_VERSION, **kwargs)
self.repository = repository
self.urls = categories
self.categories = categories
class WatermarkPita(datasets.GeneratorBasedBuilder):
"""Watermark Dataset"""
BUILDER_CONFIGS = [
WatermarkPitaConfig(
name="text",
repository="data",
urls={"train": "data/train.zip", "valid": "data/valid.zip"},
categories=["text"],
)
]
_DEFAULT_CONFIG_NAME = "text"
def _info(self):
return datasets.DatasetInfo(
features=datasets.Features(
{
"image": datasets.Image(),
"objects": datasets.Sequence(
{
"label": datasets.ClassLabel(names=self.config.categories),
"bbox": datasets.Sequence(
datasets.Value("int32"), length=4
),
}
),
}
),
description=_DESCRIPTION,
)
def _split_generators(self, dl_manager):
data_dir = dl_manager.download_and_extract(self.config.urls)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"split": "train", "data_dir": data_dir["train"]},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={"split": "valid", "data_dir": data_dir["valid"]},
),
]
def _generate_examples(self, split, data_dir):
image_dir = os.path.join(data_dir, "images")
label_dir = os.path.join(data_dir, "labels")
image_paths = sorted(glob(image_dir + "/*.jpg"))
label_paths = sorted(glob(label_dir + "/*.json"))
for idx, (image_path, label_path) in enumerate(zip(image_paths, label_paths)):
with open(label_path, "r") as f:
bbox = json.load(f)
objects = []
objects.append(
{
"label": bbox["label"],
"bbox": [
bbox["x"],
bbox["y"],
bbox["width"],
bbox["height"],
],
}
)
yield idx, {"image": image_path, "objects": objects}