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, urls, categories, **kwargs): """BuilderConfig for Food-101. Args: repository: `string`, the name of the repository. urls: `dict`, the urls to the data. categories: `list`, 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.urls = urls self.categories = categories class WatermarkPita(datasets.GeneratorBasedBuilder): """Watermark Dataset""" BUILDER_CONFIGS = [ WatermarkPitaConfig( name="text", urls={"train": "data/text/train.zip", "valid": "data/text/valid.zip"}, categories=["text"], ), WatermarkPitaConfig( name="logo", urls={"train": "data/logo/train.zip", "valid": "data/logo/valid.zip"}, categories=["logo"], ), WatermarkPitaConfig( name="mixed", urls={"train": "data/mixed/train.zip", "valid": "data/mixed/valid.zip"}, categories=["logo", "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.features.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: bboxes = json.load(f) yield idx, {"image": image_path, "objects": bboxes}