Spaces:
Sleeping
Sleeping
Commit ·
c0ce48a
1
Parent(s): d57cc64
fix: revert to shape_generation + export with textures as second step
Browse files
app.py
CHANGED
|
@@ -68,8 +68,8 @@ def process_vehicle(
|
|
| 68 |
|
| 69 |
return no_bg, with_bg, upscaled
|
| 70 |
|
| 71 |
-
def generate_3d_model(image: Image.Image) -> tuple[str, str]:
|
| 72 |
-
"""Generate 3D model
|
| 73 |
|
| 74 |
if image is None:
|
| 75 |
raise gr.Error("Please upload an image first")
|
|
@@ -82,7 +82,7 @@ def generate_3d_model(image: Image.Image) -> tuple[str, str]:
|
|
| 82 |
image.save(f.name)
|
| 83 |
temp_path = f.name
|
| 84 |
|
| 85 |
-
#
|
| 86 |
result = client.predict(
|
| 87 |
caption=None,
|
| 88 |
image=handle_file(temp_path),
|
|
@@ -97,18 +97,36 @@ def generate_3d_model(image: Image.Image) -> tuple[str, str]:
|
|
| 97 |
check_box_rembg=True,
|
| 98 |
num_chunks=8000,
|
| 99 |
randomize_seed=True,
|
| 100 |
-
api_name="/
|
| 101 |
)
|
| 102 |
|
| 103 |
os.unlink(temp_path)
|
| 104 |
|
| 105 |
-
# Result: (file,
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
except Exception as e:
|
| 114 |
raise gr.Error(f"3D generation failed: {str(e)}")
|
|
@@ -149,25 +167,26 @@ with gr.Blocks(title="DCR Vehicle Studio") as demo:
|
|
| 149 |
)
|
| 150 |
|
| 151 |
with gr.Tab("🎮 3D Generation"):
|
| 152 |
-
gr.Markdown("### Generate
|
| 153 |
-
gr.Markdown("⚠️ **Takes
|
| 154 |
|
| 155 |
with gr.Row():
|
| 156 |
with gr.Column(scale=1):
|
| 157 |
input_3d = gr.Image(type="pil", label="Vehicle Image")
|
|
|
|
| 158 |
generate_3d_btn = gr.Button("🎮 Generate 3D Model", variant="primary")
|
| 159 |
output_file = gr.File(label="Download GLB")
|
| 160 |
|
| 161 |
with gr.Column(scale=2):
|
| 162 |
output_3d_viewer = gr.Model3D(
|
| 163 |
-
label="3D Model Viewer
|
| 164 |
clear_color=[0.1, 0.1, 0.1, 1.0],
|
| 165 |
height=500
|
| 166 |
)
|
| 167 |
|
| 168 |
generate_3d_btn.click(
|
| 169 |
fn=generate_3d_model,
|
| 170 |
-
inputs=[input_3d],
|
| 171 |
outputs=[output_3d_viewer, output_file]
|
| 172 |
)
|
| 173 |
|
|
|
|
| 68 |
|
| 69 |
return no_bg, with_bg, upscaled
|
| 70 |
|
| 71 |
+
def generate_3d_model(image: Image.Image, with_texture: bool = True) -> tuple[str, str]:
|
| 72 |
+
"""Generate 3D model from image using Hunyuan 3D"""
|
| 73 |
|
| 74 |
if image is None:
|
| 75 |
raise gr.Error("Please upload an image first")
|
|
|
|
| 82 |
image.save(f.name)
|
| 83 |
temp_path = f.name
|
| 84 |
|
| 85 |
+
# Primero generar la forma
|
| 86 |
result = client.predict(
|
| 87 |
caption=None,
|
| 88 |
image=handle_file(temp_path),
|
|
|
|
| 97 |
check_box_rembg=True,
|
| 98 |
num_chunks=8000,
|
| 99 |
randomize_seed=True,
|
| 100 |
+
api_name="/shape_generation"
|
| 101 |
)
|
| 102 |
|
| 103 |
os.unlink(temp_path)
|
| 104 |
|
| 105 |
+
# Result: (file, output, mesh_stats, seed)
|
| 106 |
+
glb_path = result[0] if isinstance(result, tuple) else result
|
| 107 |
+
|
| 108 |
+
# Si pidió texturas, intentar exportar con texturas
|
| 109 |
+
if with_texture and isinstance(result, tuple) and len(result) >= 1:
|
| 110 |
+
try:
|
| 111 |
+
# Intentar exportar con texturas usando /on_export_click
|
| 112 |
+
export_result = client.predict(
|
| 113 |
+
file_out=glb_path,
|
| 114 |
+
file_out2=glb_path, # Usamos el mismo archivo
|
| 115 |
+
file_type="glb",
|
| 116 |
+
reduce_face=False,
|
| 117 |
+
export_texture=True,
|
| 118 |
+
target_face_num=10000,
|
| 119 |
+
api_name="/on_export_click"
|
| 120 |
+
)
|
| 121 |
+
# export_result: (output, download) - download es el archivo con texturas
|
| 122 |
+
if isinstance(export_result, tuple) and len(export_result) >= 2:
|
| 123 |
+
textured_glb = export_result[1]
|
| 124 |
+
if textured_glb:
|
| 125 |
+
return textured_glb, textured_glb
|
| 126 |
+
except Exception as tex_error:
|
| 127 |
+
print(f"Texture export failed, using base model: {tex_error}")
|
| 128 |
+
|
| 129 |
+
return glb_path, glb_path
|
| 130 |
|
| 131 |
except Exception as e:
|
| 132 |
raise gr.Error(f"3D generation failed: {str(e)}")
|
|
|
|
| 167 |
)
|
| 168 |
|
| 169 |
with gr.Tab("🎮 3D Generation"):
|
| 170 |
+
gr.Markdown("### Generate 3D Model from Vehicle Image")
|
| 171 |
+
gr.Markdown("⚠️ **Takes 2-5 minutes.** Uses Tencent Hunyuan3D-2.")
|
| 172 |
|
| 173 |
with gr.Row():
|
| 174 |
with gr.Column(scale=1):
|
| 175 |
input_3d = gr.Image(type="pil", label="Vehicle Image")
|
| 176 |
+
with_texture_cb = gr.Checkbox(label="Include Textures", value=True)
|
| 177 |
generate_3d_btn = gr.Button("🎮 Generate 3D Model", variant="primary")
|
| 178 |
output_file = gr.File(label="Download GLB")
|
| 179 |
|
| 180 |
with gr.Column(scale=2):
|
| 181 |
output_3d_viewer = gr.Model3D(
|
| 182 |
+
label="3D Model Viewer",
|
| 183 |
clear_color=[0.1, 0.1, 0.1, 1.0],
|
| 184 |
height=500
|
| 185 |
)
|
| 186 |
|
| 187 |
generate_3d_btn.click(
|
| 188 |
fn=generate_3d_model,
|
| 189 |
+
inputs=[input_3d, with_texture_cb],
|
| 190 |
outputs=[output_3d_viewer, output_file]
|
| 191 |
)
|
| 192 |
|