cavargas10 commited on
Commit
5bdde9e
verified
1 Parent(s): d215cd3

Update gradio_app/gradio_3dgen.py

Browse files
Files changed (1) hide show
  1. gradio_app/gradio_3dgen.py +53 -22
gradio_app/gradio_3dgen.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import spaces
2
  import os
3
  import gradio as gr
@@ -8,8 +10,12 @@ from gradio_app.custom_models.mvimg_prediction import run_mvprediction
8
  from gradio_app.custom_models.normal_prediction import predict_normals
9
  from scripts.refine_lr_to_sr import run_sr_fast
10
  from scripts.utils import save_glb_and_video
11
- # from scripts.multiview_inference import geo_reconstruct
12
  from scripts.multiview_inference import geo_reconstruct_part1, geo_reconstruct_part2, geo_reconstruct_part3
 
 
 
 
 
13
 
14
  @spaces.GPU(duration=100)
15
  def run_mv(preview_img, input_processing, seed):
@@ -18,30 +24,57 @@ def run_mv(preview_img, input_processing, seed):
18
  rgb_pils, front_pil = run_mvprediction(preview_img, remove_bg=input_processing, seed=int(seed)) # 6s
19
  return rgb_pils, front_pil
20
 
21
- @spaces.GPU(duration=100) # seems split into multiple part will leads to `RuntimeError`, before fix it, still initialize here
22
  def generate3dv2(preview_img, input_processing, seed, render_video=True, do_refine=True, expansion_weight=0.1, init_type="std"):
23
- if preview_img is None:
24
- raise gr.Error("The input image is none!")
25
- if isinstance(preview_img, str):
26
- preview_img = Image.open(preview_img)
27
-
28
- rgb_pils, front_pil = run_mv(preview_img, input_processing, seed)
29
 
30
- vertices, faces, img_list = geo_reconstruct_part1(rgb_pils, None, front_pil, do_refine=do_refine, predict_normal=True, expansion_weight=expansion_weight, init_type=init_type)
 
 
 
 
 
 
 
31
 
32
- meshes = geo_reconstruct_part2(vertices, faces)
 
33
 
34
- new_meshes = geo_reconstruct_part3(meshes, img_list)
 
35
 
36
- vertices = new_meshes.verts_packed()
37
- vertices = vertices / 2 * 1.35
38
- vertices[..., [0, 2]] = - vertices[..., [0, 2]]
39
- new_meshes = Meshes(verts=[vertices], faces=new_meshes.faces_list(), textures=new_meshes.textures)
40
-
41
- ret_mesh, video = save_glb_and_video("/tmp/gradio/generated", new_meshes, with_timestamp=True, dist=3.5, fov_in_degrees=2 / 1.35, cam_type="ortho", export_video=render_video)
42
- return ret_mesh, video
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- #######################################
45
  def create_ui(concurrency_id="wkl"):
46
  with gr.Row():
47
  with gr.Column(scale=1):
@@ -57,9 +90,7 @@ def create_ui(concurrency_id="wkl"):
57
  examples_per_page=12
58
  )
59
 
60
-
61
  with gr.Column(scale=1):
62
- # export mesh display
63
  output_mesh = gr.Model3D(value=None, label="Mesh Model", show_label=True, height=320, camera_position=(90, 90, 2))
64
  output_video = gr.Video(label="Preview", show_label=True, show_share_button=True, height=320, visible=False)
65
 
@@ -81,5 +112,5 @@ def create_ui(concurrency_id="wkl"):
81
  outputs=[output_mesh, output_video],
82
  concurrency_id=concurrency_id,
83
  api_name="generate3dv2",
84
- ).success(clean_up, api_name=False)
85
  return input_image
 
1
+ # gradio_app/gradio_3dgen.py
2
+
3
  import spaces
4
  import os
5
  import gradio as gr
 
10
  from gradio_app.custom_models.normal_prediction import predict_normals
11
  from scripts.refine_lr_to_sr import run_sr_fast
12
  from scripts.utils import save_glb_and_video
 
13
  from scripts.multiview_inference import geo_reconstruct_part1, geo_reconstruct_part2, geo_reconstruct_part3
14
+ import uuid
15
+ import shutil
16
+ import logging
17
+
18
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - UNIQUE3D - %(levelname)s - %(message)s')
19
 
20
  @spaces.GPU(duration=100)
21
  def run_mv(preview_img, input_processing, seed):
 
24
  rgb_pils, front_pil = run_mvprediction(preview_img, remove_bg=input_processing, seed=int(seed)) # 6s
25
  return rgb_pils, front_pil
26
 
27
+ @spaces.GPU(duration=100)
28
  def generate3dv2(preview_img, input_processing, seed, render_video=True, do_refine=True, expansion_weight=0.1, init_type="std"):
29
+ job_id = str(uuid.uuid4())
30
+ output_dir = os.path.join("/tmp/gradio/", job_id)
31
+ os.makedirs(output_dir, exist_ok=True)
32
+ logging.info(f"[{job_id}] Nueva generaci贸n iniciada. Directorio temporal creado en: {output_dir}")
 
 
33
 
34
+ try:
35
+ if preview_img is None:
36
+ raise gr.Error("The input image is none!")
37
+ if isinstance(preview_img, str):
38
+ preview_img = Image.open(preview_img)
39
+
40
+ logging.info(f"[{job_id}] Ejecutando predicci贸n multivista...")
41
+ rgb_pils, front_pil = run_mv(preview_img, input_processing, seed)
42
 
43
+ logging.info(f"[{job_id}] Reconstruyendo geometr铆a (Parte 1)...")
44
+ vertices, faces, img_list = geo_reconstruct_part1(rgb_pils, None, front_pil, do_refine=do_refine, predict_normal=True, expansion_weight=expansion_weight, init_type=init_type)
45
 
46
+ logging.info(f"[{job_id}] Reconstruyendo geometr铆a (Parte 2)...")
47
+ meshes = geo_reconstruct_part2(vertices, faces)
48
 
49
+ logging.info(f"[{job_id}] Reconstruyendo geometr铆a (Parte 3)...")
50
+ new_meshes = geo_reconstruct_part3(meshes, img_list)
51
+
52
+ vertices = new_meshes.verts_packed()
53
+ vertices = vertices / 2 * 1.35
54
+ vertices[..., [0, 2]] = - vertices[..., [0, 2]]
55
+ new_meshes = Meshes(verts=[vertices], faces=new_meshes.faces_list(), textures=new_meshes.textures)
56
+
57
+ logging.info(f"[{job_id}] Guardando GLB y video en {output_dir}...")
58
+ ret_mesh, video = save_glb_and_video(output_dir, new_meshes, with_timestamp=False, dist=3.5, fov_in_degrees=2 / 1.35, cam_type="ortho", export_video=render_video, filename="model")
59
+
60
+ logging.info(f"[{job_id}] Generaci贸n completada. Archivos resultantes: {ret_mesh}, {video}")
61
+ return ret_mesh, video
62
+
63
+ except Exception as e:
64
+ logging.error(f"[{job_id}] Ha ocurrido un error durante la generaci贸n: {e}", exc_info=True)
65
+ raise gr.Error(f"Error en la generaci贸n: {e}")
66
+
67
+ finally:
68
+ logging.info(f"[{job_id}] Intentando limpiar el directorio temporal: {output_dir}")
69
+ if os.path.exists(output_dir):
70
+ try:
71
+ shutil.rmtree(output_dir)
72
+ logging.info(f"[{job_id}] Directorio temporal limpiado exitosamente.")
73
+ except Exception as e:
74
+ logging.error(f"[{job_id}] Error al limpiar el directorio temporal: {e}")
75
+ else:
76
+ logging.warning(f"[{job_id}] El directorio temporal no fue encontrado para la limpieza.")
77
 
 
78
  def create_ui(concurrency_id="wkl"):
79
  with gr.Row():
80
  with gr.Column(scale=1):
 
90
  examples_per_page=12
91
  )
92
 
 
93
  with gr.Column(scale=1):
 
94
  output_mesh = gr.Model3D(value=None, label="Mesh Model", show_label=True, height=320, camera_position=(90, 90, 2))
95
  output_video = gr.Video(label="Preview", show_label=True, show_share_button=True, height=320, visible=False)
96
 
 
112
  outputs=[output_mesh, output_video],
113
  concurrency_id=concurrency_id,
114
  api_name="generate3dv2",
115
+ )
116
  return input_image