Delete main.py
Browse files
main.py
DELETED
@@ -1,199 +0,0 @@
|
|
1 |
-
from pathlib import Path as pth
|
2 |
-
import json
|
3 |
-
import tempfile
|
4 |
-
from shutil import rmtree
|
5 |
-
from typing import Union, Optional
|
6 |
-
from tqdm import tqdm
|
7 |
-
|
8 |
-
import fire
|
9 |
-
|
10 |
-
from prepare_data import (
|
11 |
-
get_text_dataset,
|
12 |
-
gdrive_download_and_extract,
|
13 |
-
mega_download_and_extract,
|
14 |
-
get_hdris,
|
15 |
-
get_materials,
|
16 |
-
get_fonts,
|
17 |
-
DownloadError
|
18 |
-
)
|
19 |
-
from generate_samples_2d import generate_samples
|
20 |
-
from blender_render_samples_3d import run_blender_command
|
21 |
-
from postprocess import postprocess_samples
|
22 |
-
|
23 |
-
|
24 |
-
def download_from_grdrive_or_mega(
|
25 |
-
path: Union[str, pth],
|
26 |
-
gdrive_file_id: str,
|
27 |
-
mega_file_id_and_key: str,
|
28 |
-
desc: Optional[str]=None,
|
29 |
-
) -> pth:
|
30 |
-
path = pth(path)
|
31 |
-
if desc is None:
|
32 |
-
desc = str(path.stem)
|
33 |
-
|
34 |
-
print(f"Downloading {desc}...")
|
35 |
-
|
36 |
-
# download from google drive, if failed, download from mega
|
37 |
-
try:
|
38 |
-
download_path = gdrive_download_and_extract(path, gdrive_file_id)
|
39 |
-
if not download_path.is_dir():
|
40 |
-
raise DownloadError(f"{desc} from Google Drive")
|
41 |
-
except Exception as e:
|
42 |
-
print(e)
|
43 |
-
print(f"Attempting to download {desc} from Mega.nz")
|
44 |
-
|
45 |
-
download_path = mega_download_and_extract(path, mega_file_id_and_key)
|
46 |
-
if not download_path.is_dir():
|
47 |
-
raise DownloadError(f"{desc} from Mega.nz")
|
48 |
-
|
49 |
-
return download_path
|
50 |
-
|
51 |
-
|
52 |
-
def download_and_prepare_data():
|
53 |
-
download_path = pth("../assets/").resolve()
|
54 |
-
if not download_path.is_dir():
|
55 |
-
download_path.mkdir(parents=True, exist_ok=True)
|
56 |
-
|
57 |
-
print("Preparing text dataset...")
|
58 |
-
_, texts_iter = get_text_dataset()
|
59 |
-
|
60 |
-
fonts_path = download_from_grdrive_or_mega(
|
61 |
-
download_path / "fonts",
|
62 |
-
"1-K8EE0QsXfxaAV-5uOE6lhTLGicRZbW2",
|
63 |
-
"itg2TKoJ#UCOEQqX7pPwAf9pguWVUuksX7orWtzK5n4SdI6CQqGc"
|
64 |
-
)
|
65 |
-
|
66 |
-
hdris_path = download_from_grdrive_or_mega(
|
67 |
-
download_path / "hdris",
|
68 |
-
"1BNCTqw5fenCK-D48-a7VQ234Aq3k45hu",
|
69 |
-
"D5YWQKgR#fFRHe-HpbCc7-yAm3h5zxbR905o1hkrxKJDvQOsGOKk"
|
70 |
-
)
|
71 |
-
|
72 |
-
materials_path = download_from_grdrive_or_mega(
|
73 |
-
download_path / "materials",
|
74 |
-
"1-5dz5DMce-braCrhVIsqB58PvcyB6qyy",
|
75 |
-
"W0hGyCzB#-Gldvyt6uGt9D6iT8kDL-T4CKGNwIKD0Yc4jR2MAgxo"
|
76 |
-
)
|
77 |
-
|
78 |
-
fonts_iter = get_fonts(fonts_path)
|
79 |
-
hdris_iter = get_hdris(hdris_path)
|
80 |
-
materials_iter = get_materials(materials_path)
|
81 |
-
|
82 |
-
return texts_iter, fonts_iter, hdris_iter, materials_iter
|
83 |
-
|
84 |
-
|
85 |
-
def main(
|
86 |
-
n_samples: int,
|
87 |
-
blender_path: str,
|
88 |
-
output_dir: str,
|
89 |
-
device: str,
|
90 |
-
resolution_x: int = 512,
|
91 |
-
resolution_y: int = 512,
|
92 |
-
compression_level: int = 9,
|
93 |
-
):
|
94 |
-
"""
|
95 |
-
Runs the main pipeline for rendering handwritten text on a virtual piece of paper using Blender.
|
96 |
-
It downloads and prepares data for rendering 3D documents using Blender.
|
97 |
-
It generates samples, renders them using Blender, and post-processes them.
|
98 |
-
The generated samples are saved in the specified output directory.
|
99 |
-
|
100 |
-
Args:
|
101 |
-
n_samples (int): The number of sample images to generate.
|
102 |
-
blender_path (str): The path to the Blender executable.
|
103 |
-
output_dir (str): The path to the directory where the rendered images will be saved.
|
104 |
-
device (str): The device to use for rendering ('cpu', 'cuda' or 'optix').
|
105 |
-
output_image_resolution (Tuple[int, int], optional): The resolution of the output images. Defaults to (512, 512).
|
106 |
-
compression_level (int, optional): The png compression level to use when saving the output images.
|
107 |
-
Must be between 0 and 9, with 0 being no compression and 9 being maximum compression.
|
108 |
-
Defaults to 9.
|
109 |
-
|
110 |
-
Raises:
|
111 |
-
ValueError: If the output directory is not empty.
|
112 |
-
|
113 |
-
Returns:
|
114 |
-
None
|
115 |
-
"""
|
116 |
-
|
117 |
-
device = device.upper()
|
118 |
-
|
119 |
-
blender_path: pth = pth(blender_path)
|
120 |
-
output_dir: pth = pth(output_dir)
|
121 |
-
|
122 |
-
if device not in ["CPU", "CUDA", "OPTIX"]:
|
123 |
-
raise ValueError(f"Invalid device: {device}")
|
124 |
-
|
125 |
-
if not blender_path.is_file():
|
126 |
-
raise ValueError(f"Blender path {blender_path} is not a valid file")
|
127 |
-
|
128 |
-
if not output_dir.is_dir():
|
129 |
-
output_dir.mkdir(parents=True, exist_ok=True)
|
130 |
-
|
131 |
-
blender_path = blender_path.resolve()
|
132 |
-
output_dir = output_dir.resolve()
|
133 |
-
|
134 |
-
# Check if the output directory is empty
|
135 |
-
if output_dir.is_dir() and list(output_dir.iterdir()):
|
136 |
-
raise ValueError(f"Output directory {output_dir} is not empty")
|
137 |
-
|
138 |
-
resolution = resolution_x, resolution_y
|
139 |
-
|
140 |
-
texts, fonts, hdris, materials = download_and_prepare_data()
|
141 |
-
|
142 |
-
root_dir = pth.cwd() / "Blender_3D_document_rendering_pipeline"
|
143 |
-
|
144 |
-
temp_dir = pth(tempfile.mkdtemp())
|
145 |
-
print(f"Saving temporary files to: {temp_dir}")
|
146 |
-
|
147 |
-
config_dir = temp_dir / "configs"
|
148 |
-
image_dir = temp_dir / "images"
|
149 |
-
config_dir.mkdir()
|
150 |
-
image_dir.mkdir()
|
151 |
-
|
152 |
-
print("Generating samples...")
|
153 |
-
generated_samples = generate_samples(
|
154 |
-
n_samples=n_samples,
|
155 |
-
device=device,
|
156 |
-
output_image_resolution=resolution,
|
157 |
-
compression_level=compression_level,
|
158 |
-
root_dir=root_dir,
|
159 |
-
output_dir=output_dir,
|
160 |
-
config_dir=config_dir,
|
161 |
-
image_dir=image_dir,
|
162 |
-
random_font_iter=fonts,
|
163 |
-
random_hdri_iter=hdris,
|
164 |
-
random_material_iter=materials,
|
165 |
-
shuffled_dataset_iter=texts,
|
166 |
-
)
|
167 |
-
|
168 |
-
print("Rendering samples using Blender...")
|
169 |
-
output_dir.mkdir(parents=True, exist_ok=True)
|
170 |
-
run_blender_command(blender_path, config_dir, output_dir, device)
|
171 |
-
|
172 |
-
postprocess_samples(generated_samples)
|
173 |
-
|
174 |
-
for k in tqdm(generated_samples, desc="Processing samples"):
|
175 |
-
output_dict = {
|
176 |
-
"text": k.text,
|
177 |
-
"bboxes": k.bounding_boxes,
|
178 |
-
"font_path": str(k.font_path),
|
179 |
-
"font_color": k.font_color.tolist(),
|
180 |
-
"text_rotation_angle": k.text_rotation_angle,
|
181 |
-
"resolution": k.output_image_resolution,
|
182 |
-
}
|
183 |
-
|
184 |
-
if k.bounding_boxes is None:
|
185 |
-
print(f"Failed to calculate bounding boxes for {k.text}! Skipping")
|
186 |
-
print(json.dumps(output_dict, indent=4))
|
187 |
-
sample_output_dir = k.output_image_path.parent
|
188 |
-
rmtree(sample_output_dir)
|
189 |
-
continue
|
190 |
-
|
191 |
-
with open(k.output_image_path.with_suffix(".json"), "w") as f:
|
192 |
-
json.dump(output_dict, f, indent=4)
|
193 |
-
|
194 |
-
print("Cleaning up temporary files...")
|
195 |
-
rmtree(temp_dir)
|
196 |
-
|
197 |
-
|
198 |
-
if __name__ == "__main__":
|
199 |
-
fire.Fire(main)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|