Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -60,7 +60,91 @@ def preprocess_image(image: Image.Image) -> Tuple[str, Image.Image]:
|
|
| 60 |
processed_image.save(f"{TMP_DIR}/{trial_id}.png")
|
| 61 |
return trial_id, processed_image
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
@spaces.GPU
|
| 66 |
def text_to_image(prompt: str, height: int, width: int, steps: int, scales: float, seed: int) -> Image.Image:
|
|
|
|
| 60 |
processed_image.save(f"{TMP_DIR}/{trial_id}.png")
|
| 61 |
return trial_id, processed_image
|
| 62 |
|
| 63 |
+
def pack_state(gs: Gaussian, mesh: MeshExtractResult, trial_id: str) -> dict:
|
| 64 |
+
return {
|
| 65 |
+
'gaussian': {
|
| 66 |
+
**gs.init_params,
|
| 67 |
+
'_xyz': gs._xyz.cpu().numpy(),
|
| 68 |
+
'_features_dc': gs._features_dc.cpu().numpy(),
|
| 69 |
+
'_scaling': gs._scaling.cpu().numpy(),
|
| 70 |
+
'_rotation': gs._rotation.cpu().numpy(),
|
| 71 |
+
'_opacity': gs._opacity.cpu().numpy(),
|
| 72 |
+
},
|
| 73 |
+
'mesh': {
|
| 74 |
+
'vertices': mesh.vertices.cpu().numpy(),
|
| 75 |
+
'faces': mesh.faces.cpu().numpy(),
|
| 76 |
+
},
|
| 77 |
+
'trial_id': trial_id,
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:
|
| 82 |
+
gs = Gaussian(
|
| 83 |
+
aabb=state['gaussian']['aabb'],
|
| 84 |
+
sh_degree=state['gaussian']['sh_degree'],
|
| 85 |
+
mininum_kernel_size=state['gaussian']['mininum_kernel_size'],
|
| 86 |
+
scaling_bias=state['gaussian']['scaling_bias'],
|
| 87 |
+
opacity_bias=state['gaussian']['opacity_bias'],
|
| 88 |
+
scaling_activation=state['gaussian']['scaling_activation'],
|
| 89 |
+
)
|
| 90 |
+
gs._xyz = torch.tensor(state['gaussian']['_xyz'], device='cuda')
|
| 91 |
+
gs._features_dc = torch.tensor(state['gaussian']['_features_dc'], device='cuda')
|
| 92 |
+
gs._scaling = torch.tensor(state['gaussian']['_scaling'], device='cuda')
|
| 93 |
+
gs._rotation = torch.tensor(state['gaussian']['_rotation'], device='cuda')
|
| 94 |
+
gs._opacity = torch.tensor(state['gaussian']['_opacity'], device='cuda')
|
| 95 |
+
|
| 96 |
+
mesh = edict(
|
| 97 |
+
vertices=torch.tensor(state['mesh']['vertices'], device='cuda'),
|
| 98 |
+
faces=torch.tensor(state['mesh']['faces'], device='cuda'),
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
return gs, mesh, state['trial_id']
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
@spaces.GPU
|
| 105 |
+
def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_strength: float, ss_sampling_steps: int, slat_guidance_strength: float, slat_sampling_steps: int) -> Tuple[dict, str]:
|
| 106 |
+
if randomize_seed:
|
| 107 |
+
seed = np.random.randint(0, MAX_SEED)
|
| 108 |
+
outputs = pipeline.run(
|
| 109 |
+
Image.open(f"{TMP_DIR}/{trial_id}.png"),
|
| 110 |
+
seed=seed,
|
| 111 |
+
formats=["gaussian", "mesh"],
|
| 112 |
+
preprocess_image=False,
|
| 113 |
+
sparse_structure_sampler_params={
|
| 114 |
+
"steps": ss_sampling_steps,
|
| 115 |
+
"cfg_strength": ss_guidance_strength,
|
| 116 |
+
},
|
| 117 |
+
slat_sampler_params={
|
| 118 |
+
"steps": slat_sampling_steps,
|
| 119 |
+
"cfg_strength": slat_guidance_strength,
|
| 120 |
+
},
|
| 121 |
+
)
|
| 122 |
+
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
| 123 |
+
video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
|
| 124 |
+
video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
|
| 125 |
+
trial_id = uuid.uuid4()
|
| 126 |
+
video_path = f"{TMP_DIR}/{trial_id}.mp4"
|
| 127 |
+
os.makedirs(os.path.dirname(video_path), exist_ok=True)
|
| 128 |
+
imageio.mimsave(video_path, video, fps=15)
|
| 129 |
+
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], trial_id)
|
| 130 |
+
return state, video_path
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
@spaces.GPU
|
| 134 |
+
def extract_glb(state: dict, mesh_simplify: float, texture_size: int) -> Tuple[str, str]:
|
| 135 |
+
gs, mesh, trial_id = unpack_state(state)
|
| 136 |
+
glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
|
| 137 |
+
glb_path = f"{TMP_DIR}/{trial_id}.glb"
|
| 138 |
+
glb.export(glb_path)
|
| 139 |
+
return glb_path, glb_path
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def activate_button() -> gr.Button:
|
| 143 |
+
return gr.Button(interactive=True)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def deactivate_button() -> gr.Button:
|
| 147 |
+
return gr.Button(interactive=False)
|
| 148 |
|
| 149 |
@spaces.GPU
|
| 150 |
def text_to_image(prompt: str, height: int, width: int, steps: int, scales: float, seed: int) -> Image.Image:
|