ghost233lism commited on
Commit
0fde3f5
·
verified ·
1 Parent(s): c2b7efe

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -20
app.py CHANGED
@@ -304,7 +304,7 @@ with gr.Blocks(title="Depth Anything AC - Depth Estimation Demo", theme=gr.theme
304
 
305
  with gr.Row():
306
  input_source = gr.Radio(
307
- choices=["Upload Image", "Use Camera"],
308
  value="Upload Image",
309
  label="Input Source"
310
  )
@@ -325,13 +325,24 @@ with gr.Blocks(title="Depth Anything AC - Depth Estimation Demo", theme=gr.theme
325
 
326
  with gr.Row(equal_height=True):
327
  with gr.Column(scale=1):
328
- upload_file = gr.File(
329
- file_types=["image", "video"],
 
330
  height=450,
331
  visible=True,
332
  show_label=False,
333
  container=False,
334
- label="Upload Image or Video"
 
 
 
 
 
 
 
 
 
 
335
  )
336
 
337
  # Camera component
@@ -369,18 +380,27 @@ with gr.Blocks(title="Depth Anything AC - Depth Estimation Demo", theme=gr.theme
369
 
370
  def switch_input_source(source):
371
  if source == "Upload Image":
372
- return gr.update(visible=True), gr.update(visible=False)
373
- else:
374
- return gr.update(visible=False), gr.update(visible=True)
 
 
375
 
376
  input_source.change(
377
  fn=switch_input_source,
378
  inputs=[input_source],
379
- outputs=[upload_file, camera_image]
380
  )
381
 
382
- def handle_prediction(input_source, upload_file_path, camera_img, colormap):
383
  if input_source == "Upload Image":
 
 
 
 
 
 
 
384
  if upload_file_path is None:
385
  return None, None, gr.update(visible=False), gr.update(visible=False)
386
 
@@ -390,33 +410,35 @@ with gr.Blocks(title="Depth Anything AC - Depth Estimation Demo", theme=gr.theme
390
  return None, result, gr.update(visible=False), download_update
391
  else:
392
  return result, None, gr.update(visible=True), download_update
393
- else:
394
  result, download_update = predict_depth(camera_img, colormap)
395
  return result, None, gr.update(visible=True), download_update
396
 
397
- example_files = []
 
 
398
  if os.path.exists("toyset"):
399
  for img_file in ["1.png", "2.png", "good.png"]:
400
  if os.path.exists(f"toyset/{img_file}"):
401
- example_files.append([f"toyset/{img_file}", "Spectral"])
402
 
403
  for vid_file in ["fog_2_processed_1s-6s_1.0x.mp4", "snow_processed_1s-6s_1.0x.mp4"]:
404
  if os.path.exists(f"toyset/{vid_file}"):
405
- example_files.append([f"toyset/{vid_file}", "Spectral"])
406
 
407
- if example_files:
408
  gr.Examples(
409
- examples=example_files,
410
- inputs=[upload_file, colormap_choice],
411
- outputs=[output_image, output_file],
412
- fn=lambda file_path, colormap: predict_depth(file_path, colormap),
413
  cache_examples=False,
414
- label="Try these example files"
415
  )
416
 
417
  submit_btn.click(
418
  fn=handle_prediction,
419
- inputs=[input_source, upload_file, camera_image, colormap_choice],
420
  outputs=[output_image, output_file, output_image, download_btn],
421
  show_progress=True
422
  )
 
304
 
305
  with gr.Row():
306
  input_source = gr.Radio(
307
+ choices=["Upload Image", "Upload Video", "Use Camera"],
308
  value="Upload Image",
309
  label="Input Source"
310
  )
 
325
 
326
  with gr.Row(equal_height=True):
327
  with gr.Column(scale=1):
328
+ # Image input component for preview and examples
329
+ upload_image = gr.Image(
330
+ type="pil",
331
  height=450,
332
  visible=True,
333
  show_label=False,
334
  container=False,
335
+ label="Upload Image"
336
+ )
337
+
338
+ # File component for video uploads
339
+ upload_file = gr.File(
340
+ file_types=["video"],
341
+ height=200,
342
+ visible=False,
343
+ show_label=False,
344
+ container=False,
345
+ label="Upload Video"
346
  )
347
 
348
  # Camera component
 
380
 
381
  def switch_input_source(source):
382
  if source == "Upload Image":
383
+ return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
384
+ elif source == "Upload Video":
385
+ return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
386
+ else: # Use Camera
387
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
388
 
389
  input_source.change(
390
  fn=switch_input_source,
391
  inputs=[input_source],
392
+ outputs=[upload_image, upload_file, camera_image]
393
  )
394
 
395
+ def handle_prediction(input_source, upload_img, upload_file_path, camera_img, colormap):
396
  if input_source == "Upload Image":
397
+ if upload_img is None:
398
+ return None, None, gr.update(visible=False), gr.update(visible=False)
399
+
400
+ result, download_update = predict_depth(upload_img, colormap)
401
+ return result, None, gr.update(visible=True), download_update
402
+
403
+ elif input_source == "Upload Video":
404
  if upload_file_path is None:
405
  return None, None, gr.update(visible=False), gr.update(visible=False)
406
 
 
410
  return None, result, gr.update(visible=False), download_update
411
  else:
412
  return result, None, gr.update(visible=True), download_update
413
+ else: # Use Camera
414
  result, download_update = predict_depth(camera_img, colormap)
415
  return result, None, gr.update(visible=True), download_update
416
 
417
+ # Separate image and video examples
418
+ image_examples = []
419
+ video_examples = []
420
  if os.path.exists("toyset"):
421
  for img_file in ["1.png", "2.png", "good.png"]:
422
  if os.path.exists(f"toyset/{img_file}"):
423
+ image_examples.append([f"toyset/{img_file}", "Spectral"])
424
 
425
  for vid_file in ["fog_2_processed_1s-6s_1.0x.mp4", "snow_processed_1s-6s_1.0x.mp4"]:
426
  if os.path.exists(f"toyset/{vid_file}"):
427
+ video_examples.append([f"toyset/{vid_file}", "Spectral"])
428
 
429
+ if image_examples:
430
  gr.Examples(
431
+ examples=image_examples,
432
+ inputs=[upload_image, colormap_choice],
433
+ outputs=[output_image],
434
+ fn=lambda image, colormap: predict_depth(image, colormap)[0] if predict_depth(image, colormap) else None,
435
  cache_examples=False,
436
+ label="Try these example images"
437
  )
438
 
439
  submit_btn.click(
440
  fn=handle_prediction,
441
+ inputs=[input_source, upload_image, upload_file, camera_image, colormap_choice],
442
  outputs=[output_image, output_file, output_image, download_btn],
443
  show_progress=True
444
  )