alexnasa commited on
Commit
308fe7a
·
verified ·
1 Parent(s): 6f8a30f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -37
app.py CHANGED
@@ -102,15 +102,13 @@ def preprocess_image(image_array, state):
102
  if image_array is None:
103
  return "❌ Please upload an image first.", None, state, gr.update(interactive=True), gr.update(interactive=True)
104
 
105
- session_id = request.session_hash
106
  base_dir = os.path.join(os.environ["PIXEL3DMM_PREPROCESSED_DATA"], session_id)
107
  os.makedirs(base_dir, exist_ok=True)
108
- state.update({"session_id": session_id, "base_dir": base_dir})
109
 
110
  img = Image.fromarray(image_array)
111
  saved_image_path = os.path.join(base_dir, f"{session_id}.png")
112
  img.save(saved_image_path)
113
- state["image_path"] = saved_image_path
114
 
115
  try:
116
  p = subprocess.run([
@@ -218,7 +216,11 @@ def step4_track(state):
218
 
219
  # New: run all steps sequentially
220
  @spaces.GPU()
221
- def run_pipeline(image_array, state):
 
 
 
 
222
  # Step 1
223
  status1, crop_img, state, _, _ = preprocess_image(image_array, state)
224
  if "❌" in status1:
@@ -245,9 +247,15 @@ def cleanup(request: gr.Request):
245
  shutil.rmtree(d1, ignore_errors=True)
246
  shutil.rmtree(d2, ignore_errors=True)
247
 
 
 
 
 
 
 
 
248
  # Build Gradio UI
249
- demo = gr.Blocks()
250
- with demo:
251
  gr.Markdown("## Pixel3dmm [Image Mode]")
252
  gr.Markdown("Versatile Screen-Space Priors for Single-Image 3D Face Reconstruction.")
253
  gr.HTML(
@@ -260,38 +268,40 @@ with demo:
260
  </div>
261
  """
262
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
 
264
- with gr.Row():
265
- with gr.Column():
266
- image_in = gr.Image(label="Upload Image", type="numpy", height=512)
267
- status = gr.Textbox(label="Status", lines=6, interactive=True, value="Upload an image to start.")
268
- state = gr.State({})
269
- with gr.Column():
270
- with gr.Row():
271
- crop_img = gr.Image(label="Preprocessed", height=256)
272
- normals_img = gr.Image(label="Normals", height=256)
273
- with gr.Row():
274
- uv_img = gr.Image(label="UV Map", height=256)
275
- track_img = gr.Image(label="Tracking", height=256)
276
- mesh_file = gr.Model3D(label="3D Model Preview")
277
-
278
- examples = gr.Examples(
279
- examples=["example_images/jim_carrey.png", "example_images/margaret_qualley.png"],
280
- inputs=image_in,
281
- outputs=[], # outputs are specified via the callback
282
- fn=run_pipeline,
283
- cache_examples=True # cache results of examples for faster loading
284
- )
285
-
286
- run_btn = gr.Button("Run Pipeline")
287
-
288
- run_btn.click(
289
- fn=run_pipeline,
290
- inputs=[image_in, state],
291
- outputs=[status, crop_img, normals_img, uv_img, track_img, mesh_file, state]
292
- )
293
- examples.outputs = [status, crop_img, normals_img, uv_img, track_img, mesh_file, state]
294
- image_in.upload(fn=reset_all, inputs=None, outputs=[crop_img, normals_img, uv_img, track_img, mesh_file, status, state, run_btn])
295
 
296
  demo.unload(cleanup)
297
 
 
102
  if image_array is None:
103
  return "❌ Please upload an image first.", None, state, gr.update(interactive=True), gr.update(interactive=True)
104
 
 
105
  base_dir = os.path.join(os.environ["PIXEL3DMM_PREPROCESSED_DATA"], session_id)
106
  os.makedirs(base_dir, exist_ok=True)
107
+ state.update({"base_dir": base_dir})
108
 
109
  img = Image.fromarray(image_array)
110
  saved_image_path = os.path.join(base_dir, f"{session_id}.png")
111
  img.save(saved_image_path)
 
112
 
113
  try:
114
  p = subprocess.run([
 
216
 
217
  # New: run all steps sequentially
218
  @spaces.GPU()
219
+ def run_pipeline(image_array, state, request: gr.Request):
220
+
221
+ session_id = request.session_hash
222
+ state.update({"session_id": session_id, "base_dir": base_dir})
223
+
224
  # Step 1
225
  status1, crop_img, state, _, _ = preprocess_image(image_array, state)
226
  if "❌" in status1:
 
247
  shutil.rmtree(d1, ignore_errors=True)
248
  shutil.rmtree(d2, ignore_errors=True)
249
 
250
+ css = """
251
+ #col-container {
252
+ margin: 0 auto;
253
+ max-width: 1024px;
254
+ }
255
+ """
256
+
257
  # Build Gradio UI
258
+ with gr.Blocks(css=css) as demo:
 
259
  gr.Markdown("## Pixel3dmm [Image Mode]")
260
  gr.Markdown("Versatile Screen-Space Priors for Single-Image 3D Face Reconstruction.")
261
  gr.HTML(
 
268
  </div>
269
  """
270
  )
271
+
272
+ with gr.Column(elem_id="col-container"):
273
+
274
+ with gr.Row():
275
+ with gr.Column():
276
+ image_in = gr.Image(label="Upload Image", type="numpy", height=512)
277
+ status = gr.Textbox(label="Status", lines=6, interactive=True, value="Upload an image to start.")
278
+ state = gr.State({})
279
+ with gr.Column():
280
+ with gr.Row():
281
+ crop_img = gr.Image(label="Preprocessed", height=256)
282
+ normals_img = gr.Image(label="Normals", height=256)
283
+ with gr.Row():
284
+ uv_img = gr.Image(label="UV Map", height=256)
285
+ track_img = gr.Image(label="Tracking", height=256)
286
+ mesh_file = gr.Model3D(label="3D Model Preview")
287
 
288
+ examples = gr.Examples(
289
+ examples=["example_images/jim_carrey.png", "example_images/margaret_qualley.png"],
290
+ inputs=image_in,
291
+ outputs=[], # outputs are specified via the callback
292
+ fn=run_pipeline,
293
+ cache_examples=True # cache results of examples for faster loading
294
+ )
295
+
296
+ run_btn = gr.Button("Run Pipeline")
297
+
298
+ run_btn.click(
299
+ fn=run_pipeline,
300
+ inputs=[image_in, state, Request],
301
+ outputs=[status, crop_img, normals_img, uv_img, track_img, mesh_file, state]
302
+ )
303
+ examples.outputs = [status, crop_img, normals_img, uv_img, track_img, mesh_file, state]
304
+ image_in.upload(fn=reset_all, inputs=None, outputs=[crop_img, normals_img, uv_img, track_img, mesh_file, status, state, run_btn])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
 
306
  demo.unload(cleanup)
307