File size: 2,217 Bytes
431eb84
 
 
 
 
 
 
 
 
 
 
 
 
30b7091
 
431eb84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b79b34
431eb84
 
 
 
 
 
 
 
 
 
 
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
import os
from xml.etree import ElementTree as ET
import datasets

_DESCRIPTION = """\
The dataset img2img data.
"""
_NAME = "i2i"

_HOMEPAGE = f"https://huggingface.co/datasets/pranjalipathre/{_NAME}"

_LICENSE = ""

_DATA = f"https://huggingface.co/datasets/pranjalipathre/{_NAME}/resolve/main/data/"
# _DATA = f"/home/pranjali/Documents/Research/pybullet/img2img/{_NAME}/data/"

class i2iDataset(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(
                {
                    "initial_image": datasets.Image(),
                    "edit_prompt": datasets.Value("string"),
                    "edited_image": datasets.Image(),
                }
            ),
            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)
        ele = root.find(f".//prompt[@frame='{idx}']")
        # print("Extracted text: ", ele.get("text"))
        dt = {
            "text": ele.get("text")
        }
        return dt

    def _generate_examples(self, data):
        tree = ET.parse(os.path.join(data, "annotations.xml"))
        root = tree.getroot()

        for idx, file in enumerate(sorted(os.listdir(os.path.join(data, "initial_images")))):
            dat = self.parse_text(root, file, idx)
            txt = dat["text"]

            yield idx, {
                "initial_image": os.path.join(data, "initial_images", file),
                "edit_prompt": txt,
                "edited_image": os.path.join(data, "edited_images", file.replace("_ip", "")),
            }