flow_data / flow_data.py
pranjalipathre's picture
5600 door flow exs
f462593
import os
from xml.etree import ElementTree as ET
import datasets
_DESCRIPTION = """\
The dataset img2flow data.
"""
_NAME = "flow_data"
_HOMEPAGE = f"https://huggingface.co/datasets/pranjalipathre/{_NAME}"
_LICENSE = ""
_DATA = f"https://huggingface.co/datasets/pranjalipathre/{_NAME}/resolve/main/data/"
# _DATA = f"./data/"
class img2flowDataset(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="video_01", data_dir=f"{_DATA}video_01.zip"),
]
DEFAULT_CONFIG_NAME = "video_01"
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"original_image": datasets.Image(),
"edit_prompt": datasets.Value("string"),
"edited_image": datasets.Value("string"),
}
),
supervised_keys=None,
homepage=_HOMEPAGE,
citation=None,
)
def _split_generators(self, dl_manager):
data = dl_manager.download_and_extract(self.config.data_dir)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"data": data,
},
),
]
@staticmethod
def parse_text(root: ET.Element, file: str, index: int) -> dict:
idx = str(index).zfill(6)
ele = root.find(f".//*[@frame='{idx}']")
dt = {
"text": ele.get("text")
}
return dt
def _generate_examples(self, data):
treePath = os.path.join(data, "annotations.xml")
tree = ET.parse(treePath)
root = tree.getroot()
for idx, file in enumerate(sorted(os.listdir(os.path.join(data, "original_images")))):
dat = self.parse_text(root, file, idx)
txt = dat["text"]
yield idx, {
"original_image": os.path.join(data, "original_images", file),
"edit_prompt": txt,
"edited_image": os.path.join(data, "edited_images", file).replace(".png", ".flo"),
}