forked from wangwenho/HanziGen
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprepare_dataset.py
More file actions
74 lines (61 loc) · 2.18 KB
/
prepare_dataset.py
File metadata and controls
74 lines (61 loc) · 2.18 KB
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
74
import argparse
import shutil
from pathlib import Path
from configs import FontProcessingConfig
from utils.argparse.argparse_utils import update_config_from_args
from utils.image import GlyphImageGenerator
def parse_args() -> argparse.Namespace:
""" """
parser = argparse.ArgumentParser(description="Prepare image dataset for training")
parser.add_argument("--target_font_path", type=str, help="Target font path")
parser.add_argument(
"--reference_fonts_dir", type=str, help="Reference fonts directory"
)
parser.add_argument("--source_charset_path", type=str, help="Source charset path")
parser.add_argument(
"--img_size", type=int, nargs=2, help="Image size (width height)"
)
parser.add_argument("--sample_ratio", type=float, help="Sampling ratio (0-1)")
return parser.parse_args()
def prepare_image_dataset(
target_font_path: str,
reference_fonts_dir: str,
source_charset_path: str,
font_processing_config: FontProcessingConfig,
) -> None:
""" """
data_dir = Path("data")
if data_dir.exists():
shutil.rmtree(data_dir)
tgt_generator = GlyphImageGenerator.from_target_font(
target_font_path=target_font_path,
font_processing_config=font_processing_config,
)
ref_generators = GlyphImageGenerator.from_reference_fonts(
reference_fonts_dir=reference_fonts_dir,
font_processing_config=font_processing_config,
)
tgt_generator.generate_glyph_images(
source_charset_path=source_charset_path,
font_role="target",
)
for ref_generator in ref_generators:
ref_generator.generate_glyph_images(
source_charset_path=source_charset_path,
font_role="reference",
)
def main() -> None:
""" """
args = parse_args()
font_processing_config = update_config_from_args(
converting_config=FontProcessingConfig(),
args=args,
)
prepare_image_dataset(
target_font_path=args.target_font_path,
reference_fonts_dir=args.reference_fonts_dir,
source_charset_path=args.source_charset_path,
font_processing_config=font_processing_config,
)
if __name__ == "__main__":
main()