Spaces:
mashroo
/
Runtime error

YoussefAnso commited on
Commit
f2cf584
·
1 Parent(s): 3a77493

Refactor gen_image function in app.py to create temporary GLB files in a persistent directory, improving file management. Update CRM model to transform vertices and faces for correct GLB coordinate system compatibility.

Browse files
Files changed (2) hide show
  1. app.py +14 -7
  2. model/crm/model.py +7 -4
app.py CHANGED
@@ -96,7 +96,6 @@ def preprocess_image(image, background_choice, foreground_ratio, backgroud_color
96
 
97
 
98
  @spaces.GPU
99
-
100
  def gen_image(input_image, seed, scale, step):
101
  global pipeline, model, args
102
  pipeline.set_seed(seed)
@@ -111,16 +110,24 @@ def gen_image(input_image, seed, scale, step):
111
  # Create a temporary file with a proper name for the GLB data
112
  import tempfile
113
  import shutil
 
114
 
115
- # Create a temporary file with a proper extension
116
- temp_glb = tempfile.NamedTemporaryFile(suffix='.glb', delete=False)
117
- temp_glb.close()
 
118
 
119
  # Copy the generated GLB file to our temporary file
120
- shutil.copy2(glb_path, temp_glb.name)
 
 
 
 
 
 
121
 
122
  # Return images and the path to the temporary GLB file
123
- return Image.fromarray(np_imgs), Image.fromarray(np_xyzs), temp_glb.name
124
 
125
 
126
  parser = argparse.ArgumentParser()
@@ -221,7 +228,7 @@ with gr.Blocks() as demo:
221
  xyz_ouput = gr.Image(interactive=False, label="Output CCM image")
222
 
223
  output_model = gr.Model3D(
224
- label="Output OBJ",
225
  interactive=False,
226
  )
227
  gr.Markdown("Note: Ensure that the input image is correctly pre-processed into a grey background, otherwise the results will be unpredictable.")
 
96
 
97
 
98
  @spaces.GPU
 
99
  def gen_image(input_image, seed, scale, step):
100
  global pipeline, model, args
101
  pipeline.set_seed(seed)
 
110
  # Create a temporary file with a proper name for the GLB data
111
  import tempfile
112
  import shutil
113
+ import os
114
 
115
+ # Create a temporary file with a proper extension in a persistent location
116
+ temp_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "temp")
117
+ os.makedirs(temp_dir, exist_ok=True)
118
+ temp_glb = os.path.join(temp_dir, f"model_{seed}.glb")
119
 
120
  # Copy the generated GLB file to our temporary file
121
+ shutil.copy2(glb_path, temp_glb)
122
+
123
+ # Clean up the original GLB file
124
+ try:
125
+ os.remove(glb_path)
126
+ except:
127
+ pass
128
 
129
  # Return images and the path to the temporary GLB file
130
+ return Image.fromarray(np_imgs), Image.fromarray(np_xyzs), temp_glb
131
 
132
 
133
  parser = argparse.ArgumentParser()
 
228
  xyz_ouput = gr.Image(interactive=False, label="Output CCM image")
229
 
230
  output_model = gr.Model3D(
231
+ label="Output 3D Model (GLB)",
232
  interactive=False,
233
  )
234
  gr.Markdown("Note: Ensure that the input image is correctly pre-processed into a grey background, otherwise the results will be unpredictable.")
model/crm/model.py CHANGED
@@ -98,11 +98,14 @@ class CRM(nn.Module):
98
  # Expect predicted colors value range from [-1, 1]
99
  colors = (colors * 0.5 + 0.5).clip(0, 1)
100
 
101
- verts = verts[..., [0, 2, 1]]
102
- verts[..., 0]*= -1
103
- verts[..., 2]*= -1
 
104
  verts = verts.squeeze().cpu().numpy()
105
- faces = faces[..., [2, 1, 0]][..., [0, 2, 1]]
 
 
106
  faces = faces.squeeze().cpu().numpy()
107
 
108
  # export the final mesh
 
98
  # Expect predicted colors value range from [-1, 1]
99
  colors = (colors * 0.5 + 0.5).clip(0, 1)
100
 
101
+ # Transform vertices to match GLB coordinate system
102
+ # GLB uses right-handed coordinate system with Y up
103
+ verts = verts[..., [0, 2, 1]] # Swap Y and Z to get Y up
104
+ verts[..., 0] *= -1 # Flip X to get right-handed
105
  verts = verts.squeeze().cpu().numpy()
106
+
107
+ # Transform faces to maintain correct winding order
108
+ faces = faces[..., [2, 1, 0]] # Reverse winding order
109
  faces = faces.squeeze().cpu().numpy()
110
 
111
  # export the final mesh