File size: 6,388 Bytes
a1838d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from pathlib import Path as pth
import json
import tempfile
from shutil import rmtree
from typing import Union, Optional
from tqdm import tqdm

import fire

from prepare_data import (
    get_text_dataset,
    gdrive_download_and_extract,
    mega_download_and_extract,
    get_hdris,
    get_materials,
    get_fonts,
    DownloadError
)
from generate_samples_2d import generate_samples
from blender_render_samples_3d import run_blender_command
from postprocess import postprocess_samples


def download_from_grdrive_or_mega(
    path: Union[str, pth],
    gdrive_file_id: str,
    mega_file_id_and_key: str,
    desc: Optional[str]=None,
) -> pth:
    path = pth(path)
    if desc is None:
        desc = str(path.stem)

    print(f"Downloading {desc}...")

    # download from google drive, if failed, download from mega
    try:
        download_path = gdrive_download_and_extract(path, gdrive_file_id)
        if not download_path.is_dir():
            raise DownloadError(f"{desc} from Google Drive")
    except Exception as e:
        print(e)
        print(f"Attempting to download {desc} from Mega.nz")

        download_path = mega_download_and_extract(path, mega_file_id_and_key)
        if not download_path.is_dir():
            raise DownloadError(f"{desc} from Mega.nz")

    return download_path


def download_and_prepare_data():
    download_path = pth("../assets/").resolve()
    if not download_path.is_dir():
        download_path.mkdir(parents=True, exist_ok=True)

    print("Preparing text dataset...")
    _, texts_iter = get_text_dataset()

    fonts_path = download_from_grdrive_or_mega(
        download_path / "fonts",
        "1-K8EE0QsXfxaAV-5uOE6lhTLGicRZbW2",
        "itg2TKoJ#UCOEQqX7pPwAf9pguWVUuksX7orWtzK5n4SdI6CQqGc"
    )

    hdris_path = download_from_grdrive_or_mega(
        download_path / "hdris",
        "1BNCTqw5fenCK-D48-a7VQ234Aq3k45hu",
        "D5YWQKgR#fFRHe-HpbCc7-yAm3h5zxbR905o1hkrxKJDvQOsGOKk"
    )

    materials_path = download_from_grdrive_or_mega(
        download_path / "materials",
        "1-5dz5DMce-braCrhVIsqB58PvcyB6qyy",
        "W0hGyCzB#-Gldvyt6uGt9D6iT8kDL-T4CKGNwIKD0Yc4jR2MAgxo"
    )

    fonts_iter = get_fonts(fonts_path)
    hdris_iter = get_hdris(hdris_path)
    materials_iter = get_materials(materials_path)

    return texts_iter, fonts_iter, hdris_iter, materials_iter


def main(
    n_samples: int,
    blender_path: str,
    output_dir: str,
    device: str,
    resolution_x: int = 512,
    resolution_y: int = 512,
    compression_level: int = 9,
):
    """
    Runs the main pipeline for rendering handwritten text on a virtual piece of paper using Blender.
    It downloads and prepares data for rendering 3D documents using Blender.
    It generates samples, renders them using Blender, and post-processes them.
    The generated samples are saved in the specified output directory.

    Args:
        n_samples (int): The number of sample images to generate.
        blender_path (str): The path to the Blender executable.
        output_dir (str): The path to the directory where the rendered images will be saved.
        device (str): The device to use for rendering ('cpu', 'cuda' or 'optix').
        output_image_resolution (Tuple[int, int], optional): The resolution of the output images. Defaults to (512, 512).
        compression_level (int, optional): The png compression level to use when saving the output images.
                                           Must be between 0 and 9, with 0 being no compression and 9 being maximum compression.
                                           Defaults to 9.

    Raises:
        ValueError: If the output directory is not empty.

    Returns:
        None
    """

    device = device.upper()

    blender_path: pth = pth(blender_path)
    output_dir: pth = pth(output_dir)

    if device not in ["CPU", "CUDA", "OPTIX"]:
        raise ValueError(f"Invalid device: {device}")

    if not blender_path.is_file():
        raise ValueError(f"Blender path {blender_path} is not a valid file")

    if not output_dir.is_dir():
        output_dir.mkdir(parents=True, exist_ok=True)

    blender_path = blender_path.resolve()
    output_dir = output_dir.resolve()

    # Check if the output directory is empty
    if output_dir.is_dir() and list(output_dir.iterdir()):
        raise ValueError(f"Output directory {output_dir} is not empty")

    resolution = resolution_x, resolution_y

    texts, fonts, hdris, materials = download_and_prepare_data()

    root_dir = pth.cwd() / "Blender_3D_document_rendering_pipeline"

    temp_dir = pth(tempfile.mkdtemp())
    print(f"Saving temporary files to: {temp_dir}")

    config_dir = temp_dir / "configs"
    image_dir = temp_dir / "images"
    config_dir.mkdir()
    image_dir.mkdir()

    print("Generating samples...")
    generated_samples = generate_samples(
        n_samples=n_samples,
        device=device,
        output_image_resolution=resolution,
        compression_level=compression_level,
        root_dir=root_dir,
        output_dir=output_dir,
        config_dir=config_dir,
        image_dir=image_dir,
        random_font_iter=fonts,
        random_hdri_iter=hdris,
        random_material_iter=materials,
        shuffled_dataset_iter=texts,
    )

    print("Rendering samples using Blender...")
    output_dir.mkdir(parents=True, exist_ok=True)
    run_blender_command(blender_path, config_dir, output_dir, device)

    postprocess_samples(generated_samples)

    for k in tqdm(generated_samples, desc="Processing samples"):
        output_dict = {
            "text": k.text,
            "bboxes": k.bounding_boxes,
            "font_path": str(k.font_path),
            "font_color": k.font_color.tolist(),
            "text_rotation_angle": k.text_rotation_angle,
            "resolution": k.output_image_resolution,
        }

        if k.bounding_boxes is None:
            print(f"Failed to calculate bounding boxes for {k.text}! Skipping")
            print(json.dumps(output_dict, indent=4))
            sample_output_dir = k.output_image_path.parent
            rmtree(sample_output_dir)
            continue

        with open(k.output_image_path.with_suffix(".json"), "w") as f:
            json.dump(output_dict, f, indent=4)

    print("Cleaning up temporary files...")
    rmtree(temp_dir)


if __name__ == "__main__":
    fire.Fire(main)</document_content>