File size: 2,241 Bytes
f462593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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"),
            }