Spaces:
Running
on
Zero
Running
on
Zero
xinjie.wang
commited on
Commit
·
44648c3
1
Parent(s):
2e0bac6
update
Browse files
common.py
CHANGED
@@ -35,7 +35,7 @@ from gradio.themes.utils.colors import gray, neutral, slate, stone, teal, zinc
|
|
35 |
from PIL import Image
|
36 |
from embodied_gen.data.backproject_v2 import entrypoint as backproject_api
|
37 |
from embodied_gen.data.differentiable_render import entrypoint as render_api
|
38 |
-
from embodied_gen.data.utils import trellis_preprocess
|
39 |
from embodied_gen.models.delight_model import DelightingModel
|
40 |
from embodied_gen.models.gs_model import GaussianOperator
|
41 |
from embodied_gen.models.segment_model import (
|
@@ -64,7 +64,7 @@ from embodied_gen.validators.quality_checkers import (
|
|
64 |
ImageSegChecker,
|
65 |
MeshGeoChecker,
|
66 |
)
|
67 |
-
from embodied_gen.validators.urdf_convertor import URDFGenerator
|
68 |
|
69 |
current_file_path = os.path.abspath(__file__)
|
70 |
current_dir = os.path.dirname(current_file_path)
|
|
|
35 |
from PIL import Image
|
36 |
from embodied_gen.data.backproject_v2 import entrypoint as backproject_api
|
37 |
from embodied_gen.data.differentiable_render import entrypoint as render_api
|
38 |
+
from embodied_gen.data.utils import trellis_preprocess, zip_files
|
39 |
from embodied_gen.models.delight_model import DelightingModel
|
40 |
from embodied_gen.models.gs_model import GaussianOperator
|
41 |
from embodied_gen.models.segment_model import (
|
|
|
64 |
ImageSegChecker,
|
65 |
MeshGeoChecker,
|
66 |
)
|
67 |
+
from embodied_gen.validators.urdf_convertor import URDFGenerator
|
68 |
|
69 |
current_file_path = os.path.abspath(__file__)
|
70 |
current_dir = os.path.dirname(current_file_path)
|
embodied_gen/data/differentiable_render.py
CHANGED
@@ -24,7 +24,10 @@ from collections import defaultdict
|
|
24 |
from typing import List, Union
|
25 |
|
26 |
import cv2
|
|
|
|
|
27 |
import nvdiffrast.torch as dr
|
|
|
28 |
import torch
|
29 |
from tqdm import tqdm
|
30 |
from embodied_gen.data.utils import (
|
@@ -39,10 +42,6 @@ from embodied_gen.data.utils import (
|
|
39 |
render_pbr,
|
40 |
save_images,
|
41 |
)
|
42 |
-
from embodied_gen.utils.process_media import (
|
43 |
-
create_gif_from_images,
|
44 |
-
create_mp4_from_images,
|
45 |
-
)
|
46 |
|
47 |
os.environ["OPENCV_IO_ENABLE_OPENEXR"] = "1"
|
48 |
os.environ["TORCH_EXTENSIONS_DIR"] = os.path.expanduser(
|
@@ -54,7 +53,66 @@ logging.basicConfig(
|
|
54 |
logger = logging.getLogger(__name__)
|
55 |
|
56 |
|
57 |
-
__all__ = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
|
60 |
class ImageRender(object):
|
|
|
24 |
from typing import List, Union
|
25 |
|
26 |
import cv2
|
27 |
+
import imageio
|
28 |
+
import numpy as np
|
29 |
import nvdiffrast.torch as dr
|
30 |
+
import PIL.Image as Image
|
31 |
import torch
|
32 |
from tqdm import tqdm
|
33 |
from embodied_gen.data.utils import (
|
|
|
42 |
render_pbr,
|
43 |
save_images,
|
44 |
)
|
|
|
|
|
|
|
|
|
45 |
|
46 |
os.environ["OPENCV_IO_ENABLE_OPENEXR"] = "1"
|
47 |
os.environ["TORCH_EXTENSIONS_DIR"] = os.path.expanduser(
|
|
|
53 |
logger = logging.getLogger(__name__)
|
54 |
|
55 |
|
56 |
+
__all__ = [
|
57 |
+
"ImageRender",
|
58 |
+
"create_mp4_from_images",
|
59 |
+
"create_gif_from_images",
|
60 |
+
]
|
61 |
+
|
62 |
+
|
63 |
+
def create_mp4_from_images(
|
64 |
+
images: list[np.ndarray],
|
65 |
+
output_path: str,
|
66 |
+
fps: int = 10,
|
67 |
+
prompt: str = None,
|
68 |
+
):
|
69 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
70 |
+
font_scale = 0.5
|
71 |
+
font_thickness = 1
|
72 |
+
color = (255, 255, 255)
|
73 |
+
position = (20, 25)
|
74 |
+
|
75 |
+
with imageio.get_writer(output_path, fps=fps) as writer:
|
76 |
+
for image in images:
|
77 |
+
image = image.clip(min=0, max=1)
|
78 |
+
image = (255.0 * image).astype(np.uint8)
|
79 |
+
image = image[..., :3]
|
80 |
+
if prompt is not None:
|
81 |
+
cv2.putText(
|
82 |
+
image,
|
83 |
+
prompt,
|
84 |
+
position,
|
85 |
+
font,
|
86 |
+
font_scale,
|
87 |
+
color,
|
88 |
+
font_thickness,
|
89 |
+
)
|
90 |
+
|
91 |
+
writer.append_data(image)
|
92 |
+
|
93 |
+
logger.info(f"MP4 video saved to {output_path}")
|
94 |
+
|
95 |
+
|
96 |
+
def create_gif_from_images(
|
97 |
+
images: list[np.ndarray], output_path: str, fps: int = 10
|
98 |
+
) -> None:
|
99 |
+
pil_images = []
|
100 |
+
for image in images:
|
101 |
+
image = image.clip(min=0, max=1)
|
102 |
+
image = (255.0 * image).astype(np.uint8)
|
103 |
+
image = Image.fromarray(image, mode="RGBA")
|
104 |
+
pil_images.append(image.convert("RGB"))
|
105 |
+
|
106 |
+
duration = 1000 // fps
|
107 |
+
pil_images[0].save(
|
108 |
+
output_path,
|
109 |
+
save_all=True,
|
110 |
+
append_images=pil_images[1:],
|
111 |
+
duration=duration,
|
112 |
+
loop=0,
|
113 |
+
)
|
114 |
+
|
115 |
+
logger.info(f"GIF saved to {output_path}")
|
116 |
|
117 |
|
118 |
class ImageRender(object):
|
embodied_gen/data/utils.py
CHANGED
@@ -139,7 +139,9 @@ class DiffrastRender(object):
|
|
139 |
vertices: torch.Tensor,
|
140 |
matrix: torch.Tensor,
|
141 |
) -> torch.Tensor:
|
142 |
-
verts_ones = torch.ones(
|
|
|
|
|
143 |
verts_homo = torch.cat([vertices, verts_ones], dim=-1)
|
144 |
trans_vertices = torch.matmul(verts_homo, matrix.permute(0, 2, 1))
|
145 |
|
|
|
139 |
vertices: torch.Tensor,
|
140 |
matrix: torch.Tensor,
|
141 |
) -> torch.Tensor:
|
142 |
+
verts_ones = torch.ones(
|
143 |
+
(len(vertices), 1), device=vertices.device, dtype=vertices.dtype
|
144 |
+
)
|
145 |
verts_homo = torch.cat([vertices, verts_ones], dim=-1)
|
146 |
trans_vertices = torch.matmul(verts_homo, matrix.permute(0, 2, 1))
|
147 |
|
embodied_gen/utils/process_media.py
CHANGED
@@ -19,7 +19,6 @@ import base64
|
|
19 |
import logging
|
20 |
import math
|
21 |
import os
|
22 |
-
import subprocess
|
23 |
import sys
|
24 |
from glob import glob
|
25 |
from io import BytesIO
|
@@ -33,6 +32,7 @@ import spaces
|
|
33 |
import torch
|
34 |
from moviepy.editor import VideoFileClip, clips_array
|
35 |
from tqdm import tqdm
|
|
|
36 |
|
37 |
current_file_path = os.path.abspath(__file__)
|
38 |
current_dir = os.path.dirname(current_file_path)
|
@@ -56,8 +56,6 @@ __all__ = [
|
|
56 |
"combine_images_to_base64",
|
57 |
"render_mesh",
|
58 |
"render_video",
|
59 |
-
"create_mp4_from_images",
|
60 |
-
"create_gif_from_images",
|
61 |
]
|
62 |
|
63 |
|
@@ -75,34 +73,25 @@ def render_asset3d(
|
|
75 |
gen_viewnormal_mp4: bool = False,
|
76 |
gen_glonormal_mp4: bool = False,
|
77 |
) -> list[str]:
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
str(distance),
|
89 |
-
"--num_images",
|
90 |
-
str(num_images),
|
91 |
-
"--elevation",
|
92 |
-
*map(str, elevation),
|
93 |
-
"--pbr_light_factor",
|
94 |
-
str(pbr_light_factor),
|
95 |
-
"--with_mtl",
|
96 |
-
]
|
97 |
if gen_color_mp4:
|
98 |
-
|
99 |
if gen_viewnormal_mp4:
|
100 |
-
|
101 |
if gen_glonormal_mp4:
|
102 |
-
|
103 |
try:
|
104 |
-
|
105 |
-
except
|
106 |
logger.error(f"Error occurred during rendering: {e}.")
|
107 |
|
108 |
dst_paths = glob(os.path.join(output_root, output_subdir, return_key))
|
@@ -263,54 +252,6 @@ def render_video(
|
|
263 |
return result
|
264 |
|
265 |
|
266 |
-
def create_mp4_from_images(images, output_path, fps=10, prompt=None):
|
267 |
-
font = cv2.FONT_HERSHEY_SIMPLEX
|
268 |
-
font_scale = 0.5
|
269 |
-
font_thickness = 1
|
270 |
-
color = (255, 255, 255)
|
271 |
-
position = (20, 25)
|
272 |
-
|
273 |
-
with imageio.get_writer(output_path, fps=fps) as writer:
|
274 |
-
for image in images:
|
275 |
-
image = image.clip(min=0, max=1)
|
276 |
-
image = (255.0 * image).astype(np.uint8)
|
277 |
-
image = image[..., :3]
|
278 |
-
if prompt is not None:
|
279 |
-
cv2.putText(
|
280 |
-
image,
|
281 |
-
prompt,
|
282 |
-
position,
|
283 |
-
font,
|
284 |
-
font_scale,
|
285 |
-
color,
|
286 |
-
font_thickness,
|
287 |
-
)
|
288 |
-
|
289 |
-
writer.append_data(image)
|
290 |
-
|
291 |
-
logger.info(f"MP4 video saved to {output_path}")
|
292 |
-
|
293 |
-
|
294 |
-
def create_gif_from_images(images, output_path, fps=10):
|
295 |
-
pil_images = []
|
296 |
-
for image in images:
|
297 |
-
image = image.clip(min=0, max=1)
|
298 |
-
image = (255.0 * image).astype(np.uint8)
|
299 |
-
image = Image.fromarray(image, mode="RGBA")
|
300 |
-
pil_images.append(image.convert("RGB"))
|
301 |
-
|
302 |
-
duration = 1000 // fps
|
303 |
-
pil_images[0].save(
|
304 |
-
output_path,
|
305 |
-
save_all=True,
|
306 |
-
append_images=pil_images[1:],
|
307 |
-
duration=duration,
|
308 |
-
loop=0,
|
309 |
-
)
|
310 |
-
|
311 |
-
logger.info(f"GIF saved to {output_path}")
|
312 |
-
|
313 |
-
|
314 |
if __name__ == "__main__":
|
315 |
# Example usage:
|
316 |
merge_video_video(
|
|
|
19 |
import logging
|
20 |
import math
|
21 |
import os
|
|
|
22 |
import sys
|
23 |
from glob import glob
|
24 |
from io import BytesIO
|
|
|
32 |
import torch
|
33 |
from moviepy.editor import VideoFileClip, clips_array
|
34 |
from tqdm import tqdm
|
35 |
+
from embodied_gen.data.differentiable_render import entrypoint as render_api
|
36 |
|
37 |
current_file_path = os.path.abspath(__file__)
|
38 |
current_dir = os.path.dirname(current_file_path)
|
|
|
56 |
"combine_images_to_base64",
|
57 |
"render_mesh",
|
58 |
"render_video",
|
|
|
|
|
59 |
]
|
60 |
|
61 |
|
|
|
73 |
gen_viewnormal_mp4: bool = False,
|
74 |
gen_glonormal_mp4: bool = False,
|
75 |
) -> list[str]:
|
76 |
+
input_args = dict(
|
77 |
+
mesh_path=mesh_path,
|
78 |
+
output_root=output_root,
|
79 |
+
uuid=output_subdir,
|
80 |
+
distance=distance,
|
81 |
+
num_images=num_images,
|
82 |
+
elevation=elevation,
|
83 |
+
pbr_light_factor=pbr_light_factor,
|
84 |
+
with_mtl=True,
|
85 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
if gen_color_mp4:
|
87 |
+
input_args["gen_color_mp4"] = True
|
88 |
if gen_viewnormal_mp4:
|
89 |
+
input_args["gen_viewnormal_mp4"] = True
|
90 |
if gen_glonormal_mp4:
|
91 |
+
input_args["gen_glonormal_mp4"] = True
|
92 |
try:
|
93 |
+
_ = render_api(input_args)
|
94 |
+
except Exception as e:
|
95 |
logger.error(f"Error occurred during rendering: {e}.")
|
96 |
|
97 |
dst_paths = glob(os.path.join(output_root, output_subdir, return_key))
|
|
|
252 |
return result
|
253 |
|
254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
if __name__ == "__main__":
|
256 |
# Example usage:
|
257 |
merge_video_video(
|
embodied_gen/validators/urdf_convertor.py
CHANGED
@@ -24,7 +24,6 @@ from xml.dom.minidom import parseString
|
|
24 |
|
25 |
import numpy as np
|
26 |
import trimesh
|
27 |
-
from embodied_gen.data.utils import zip_files
|
28 |
from embodied_gen.utils.gpt_clients import GPT_CLIENT, GPTclient
|
29 |
from embodied_gen.utils.process_media import render_asset3d
|
30 |
from embodied_gen.utils.tags import VERSION
|
|
|
24 |
|
25 |
import numpy as np
|
26 |
import trimesh
|
|
|
27 |
from embodied_gen.utils.gpt_clients import GPT_CLIENT, GPTclient
|
28 |
from embodied_gen.utils.process_media import render_asset3d
|
29 |
from embodied_gen.utils.tags import VERSION
|