3232from easydict import EasyDict as edict
3333from PIL import Image
3434from embodied_gen .data .backproject_v2 import entrypoint as backproject_api
35+ from embodied_gen .data .backproject_v3 import entrypoint as backproject_api_v3
3536from embodied_gen .data .differentiable_render import entrypoint as render_api
36- from embodied_gen .data .utils import trellis_preprocess , zip_files
37+ from embodied_gen .data .utils import resize_pil , trellis_preprocess , zip_files
3738from embodied_gen .models .delight_model import DelightingModel
3839from embodied_gen .models .gs_model import GaussianOperator
3940from embodied_gen .models .segment_model import (
@@ -131,8 +132,8 @@ def build_covariance_from_scaling_rotation(
131132Gaussian .setup_functions = patched_setup_functions
132133
133134
134- DELIGHT = DelightingModel ()
135- IMAGESR_MODEL = ImageRealESRGAN (outscale = 4 )
135+ # DELIGHT = DelightingModel()
136+ # IMAGESR_MODEL = ImageRealESRGAN(outscale=4)
136137# IMAGESR_MODEL = ImageStableSR()
137138if os .getenv ("GRADIO_APP" ) == "imageto3d" :
138139 RBG_REMOVER = RembgRemover ()
@@ -169,6 +170,8 @@ def build_covariance_from_scaling_rotation(
169170 )
170171 os .makedirs (TMP_DIR , exist_ok = True )
171172elif os .getenv ("GRADIO_APP" ) == "texture_edit" :
173+ DELIGHT = DelightingModel ()
174+ IMAGESR_MODEL = ImageRealESRGAN (outscale = 4 )
172175 PIPELINE_IP = build_texture_gen_pipe (
173176 base_ckpt_dir = "./weights" ,
174177 ip_adapt_scale = 0.7 ,
@@ -205,7 +208,7 @@ def preprocess_image_fn(
205208 elif isinstance (image , np .ndarray ):
206209 image = Image .fromarray (image )
207210
208- image_cache = image .copy (). resize (( 512 , 512 ) )
211+ image_cache = resize_pil ( image .copy (), 1024 )
209212
210213 bg_remover = RBG_REMOVER if rmbg_tag == "rembg" else RBG14_REMOVER
211214 image = bg_remover (image )
@@ -221,7 +224,7 @@ def preprocess_sam_image_fn(
221224 image = Image .fromarray (image )
222225
223226 sam_image = SAM_PREDICTOR .preprocess_image (image )
224- image_cache = Image . fromarray ( sam_image ). resize (( 512 , 512 ) )
227+ image_cache = sam_image . copy ( )
225228 SAM_PREDICTOR .predictor .set_image (sam_image )
226229
227230 return sam_image , image_cache
@@ -512,6 +515,60 @@ def extract_3d_representations_v2(
512515 return mesh_glb_path , gs_path , mesh_obj_path , aligned_gs_path
513516
514517
518+ def extract_3d_representations_v3 (
519+ state : dict ,
520+ enable_delight : bool ,
521+ texture_size : int ,
522+ req : gr .Request ,
523+ ):
524+ output_root = TMP_DIR
525+ user_dir = os .path .join (output_root , str (req .session_hash ))
526+ gs_model , mesh_model = unpack_state (state , device = "cpu" )
527+
528+ filename = "sample"
529+ gs_path = os .path .join (user_dir , f"{ filename } _gs.ply" )
530+ gs_model .save_ply (gs_path )
531+
532+ # Rotate mesh and GS by 90 degrees around Z-axis.
533+ rot_matrix = [[0 , 0 , - 1 ], [0 , 1 , 0 ], [1 , 0 , 0 ]]
534+ gs_add_rot = [[1 , 0 , 0 ], [0 , - 1 , 0 ], [0 , 0 , - 1 ]]
535+ mesh_add_rot = [[1 , 0 , 0 ], [0 , 0 , - 1 ], [0 , 1 , 0 ]]
536+
537+ # Addtional rotation for GS to align mesh.
538+ gs_rot = np .array (gs_add_rot ) @ np .array (rot_matrix )
539+ pose = GaussianOperator .trans_to_quatpose (gs_rot )
540+ aligned_gs_path = gs_path .replace (".ply" , "_aligned.ply" )
541+ GaussianOperator .resave_ply (
542+ in_ply = gs_path ,
543+ out_ply = aligned_gs_path ,
544+ instance_pose = pose ,
545+ device = "cpu" ,
546+ )
547+
548+ mesh = trimesh .Trimesh (
549+ vertices = mesh_model .vertices .cpu ().numpy (),
550+ faces = mesh_model .faces .cpu ().numpy (),
551+ )
552+ mesh .vertices = mesh .vertices @ np .array (mesh_add_rot )
553+ mesh .vertices = mesh .vertices @ np .array (rot_matrix )
554+
555+ mesh_obj_path = os .path .join (user_dir , f"{ filename } .obj" )
556+ mesh .export (mesh_obj_path )
557+
558+ mesh = backproject_api_v3 (
559+ gs_path = aligned_gs_path ,
560+ mesh_path = mesh_obj_path ,
561+ output_path = mesh_obj_path ,
562+ skip_fix_mesh = False ,
563+ texture_size = texture_size ,
564+ )
565+
566+ mesh_glb_path = os .path .join (user_dir , f"{ filename } .glb" )
567+ mesh .export (mesh_glb_path )
568+
569+ return mesh_glb_path , gs_path , mesh_obj_path , aligned_gs_path
570+
571+
515572def extract_urdf (
516573 gs_path : str ,
517574 mesh_obj_path : str ,
0 commit comments