animelover
commited on
Commit
•
908b98f
1
Parent(s):
984a710
Create princess-connect-images.py
Browse files- princess-connect-images.py +52 -0
princess-connect-images.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import datasets
|
3 |
+
from huggingface_hub import HfApi
|
4 |
+
from datasets import DownloadManager, DatasetInfo
|
5 |
+
from datasets.data_files import DataFilesDict
|
6 |
+
|
7 |
+
_EXTENSION = [".png", ".jpg", ".jpeg"]
|
8 |
+
_DESCRIPTION = ""
|
9 |
+
_NAME = "animelover/princess-connect-images"
|
10 |
+
_REVISION = "main"
|
11 |
+
|
12 |
+
|
13 |
+
class DanbooruDataset(datasets.GeneratorBasedBuilder):
|
14 |
+
|
15 |
+
def _info(self) -> DatasetInfo:
|
16 |
+
return datasets.DatasetInfo(
|
17 |
+
description=_DESCRIPTION,
|
18 |
+
features=datasets.Features(
|
19 |
+
{
|
20 |
+
"image": datasets.Image(),
|
21 |
+
"tags": datasets.Value("string")
|
22 |
+
}
|
23 |
+
),
|
24 |
+
supervised_keys=None,
|
25 |
+
citation="",
|
26 |
+
)
|
27 |
+
|
28 |
+
def _split_generators(self, dl_manager: DownloadManager):
|
29 |
+
hfh_dataset_info = HfApi().dataset_info(_NAME, revision=_REVISION, timeout=100.0)
|
30 |
+
data_files = DataFilesDict.from_hf_repo(
|
31 |
+
{datasets.Split.TRAIN: ["**"]},
|
32 |
+
dataset_info=hfh_dataset_info,
|
33 |
+
allowed_extensions=["zip"],
|
34 |
+
)
|
35 |
+
gs = []
|
36 |
+
for split, files in data_files.items():
|
37 |
+
downloaded_files = dl_manager.download_and_extract(files)
|
38 |
+
gs.append(datasets.SplitGenerator(name=split, gen_kwargs={"filepath": downloaded_files}))
|
39 |
+
return gs
|
40 |
+
|
41 |
+
def _generate_examples(self, filepath):
|
42 |
+
for path in filepath:
|
43 |
+
all_fnames = {os.path.relpath(os.path.join(root, fname), start=path)
|
44 |
+
for root, _dirs, files in os.walk(path) for fname in files}
|
45 |
+
image_fnames = sorted(fname for fname in all_fnames
|
46 |
+
if os.path.splitext(fname)[1].lower() in _EXTENSION)
|
47 |
+
for image_fname in image_fnames:
|
48 |
+
image_path = os.path.join(path, image_fname)
|
49 |
+
tags_path = os.path.join(path, os.path.splitext(image_fname)[0] + ".txt")
|
50 |
+
with open(tags_path, "r", encoding="utf-8") as f:
|
51 |
+
tags = f.read()
|
52 |
+
yield image_fname, {"image": image_path, "tags": tags}
|