AisingioroHao0 commited on
Commit
c860465
·
1 Parent(s): 4031149
Files changed (2) hide show
  1. comics.py +97 -0
  2. upload.py +34 -0
comics.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from logging import config
2
+ import datasets
3
+ import os
4
+ from PIL import Image
5
+ import re
6
+
7
+
8
+ class ComicsConfig(datasets.BuilderConfig):
9
+ def __init__(self, **kwargs):
10
+ super(ComicsConfig, self).__init__(**kwargs)
11
+
12
+
13
+ class Comics(datasets.GeneratorBasedBuilder):
14
+ def _split_generators(self, dl_manager: datasets.DownloadManager):
15
+ return [
16
+ datasets.SplitGenerator(
17
+ name=datasets.Split.TRAIN,
18
+ gen_kwargs={},
19
+ )
20
+ ]
21
+
22
+ BUILDER_CONFIGS = [
23
+ ComicsConfig(
24
+ name="chapter split",
25
+ description="comic dataset,item is a chapter",
26
+ ),
27
+ ComicsConfig(
28
+ name="page split",
29
+ description="comic dataset,item is a page",
30
+ ),
31
+ ]
32
+
33
+ def _info(self):
34
+ if self.config.name == "page split":
35
+ return datasets.Features(
36
+ {
37
+ "comic_name": datasets.Value("string"),
38
+ "chapter_name": datasets.Value("string"),
39
+ "cover_image": datasets.features.Image(),
40
+ "image": datasets.features.Image(),
41
+ "prompt": datasets.Value("string"),
42
+ }
43
+ )
44
+ else:
45
+ return datasets.Features(
46
+ {
47
+ "comic_name": datasets.Value("string"),
48
+ "chapter_name": datasets.Value("string"),
49
+ "cover_image": datasets.features.Image(),
50
+ "images": datasets.features.Sequence(
51
+ {
52
+ "idx": datasets.Value("int32"),
53
+ "image": datasets.features.Image(),
54
+ }
55
+ ),
56
+ "prompt": datasets.Value("string"),
57
+ }
58
+ )
59
+
60
+ def _generate_examples(self):
61
+ data_path = os.path.join(self.config.data_dir, "data")
62
+ for comic_name in os.listdir(data_path):
63
+ comic_path = os.path.join(data_path, comic_name)
64
+ cover_image = (
65
+ Image.open(os.path.join(comic_path, "cover.jpg"))
66
+ if os.path.exists(os.path.join(comic_path, "cover.jpg"))
67
+ else None
68
+ )
69
+
70
+ for chapter_name in os.listdir(comic_path):
71
+ chapter_path = os.path.join(comic_path, chapter_name)
72
+ if os.path.isfile(chapter_path):
73
+ continue
74
+ pages = list()
75
+ for idx, page_name in enumerate(os.listdir(chapter_path)):
76
+ page_path = os.path.join(chapter_path, page_name)
77
+ pages.append({"idx": idx, "image": Image.open(page_path)})
78
+ if self.config.name == "page split":
79
+ for page in pages:
80
+ yield comic_name + ":" + chapter_name + ":" + str(
81
+ page["idx"]
82
+ ), {
83
+ "comic_name": comic_name,
84
+ "chapter_name": chapter_name,
85
+ "cover_image": cover_image,
86
+ "image": page["image"],
87
+ "idx": page["idx"],
88
+ "prompt": f"a image in the syle of manga.{comic_name}.{chapter_name}.{page['idx']}",
89
+ }
90
+ else:
91
+ yield comic_name + ":" + chapter_name + ":", {
92
+ "comic_name": comic_name,
93
+ "chapter_name": chapter_name,
94
+ "cover_image": cover_image,
95
+ "images": pages,
96
+ "prompt": f"a image in the syle of manga.{comic_name}.{chapter_name}",
97
+ }
upload.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ from tqdm import tqdm
4
+ import git
5
+
6
+ repo_path = "."
7
+ os.chdir(repo_path)
8
+ repo = git.Repo(repo_path)
9
+ repo.index.commit("add images")
10
+ while True:
11
+ try:
12
+ repo.remote().push()
13
+ except Exception as e:
14
+ print(str(e))
15
+ else:
16
+ break
17
+ cnt = 0
18
+ for file_path in tqdm(repo.untracked_files):
19
+ if os.path.isfile(file_path):
20
+ repo.git().add([file_path])
21
+ cnt += 1
22
+ if cnt == 100:
23
+ repo.index.commit("add images")
24
+ while True:
25
+ try:
26
+ repo.remote().push()
27
+ except Exception as e:
28
+ print(str(e))
29
+ else:
30
+ break
31
+ cnt = 0
32
+ if cnt:
33
+ repo.index.commit("add images")
34
+ repo.remote().push()