cubuvl commited on
Commit
cfa1332
·
verified ·
1 Parent(s): 7a98e98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -1,7 +1,5 @@
1
  #!/usr/bin/env python
2
-
3
  import pathlib
4
-
5
  import gradio as gr
6
  import matplotlib as mpl
7
  import numpy as np
@@ -15,8 +13,8 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
  image_processor = DepthProImageProcessorFast.from_pretrained("apple/DepthPro-hf")
16
  model = DepthProForDepthEstimation.from_pretrained("apple/DepthPro-hf").to(device)
17
 
18
- cmap = mpl.colormaps.get_cmap("Spectral_r")
19
-
20
 
21
  @spaces.GPU(duration=20)
22
  @torch.inference_mode()
@@ -24,27 +22,28 @@ def run(image: PIL.Image.Image) -> tuple[tuple[PIL.Image.Image, PIL.Image.Image]
24
  inputs = image_processor(images=image, return_tensors="pt").to(device)
25
  outputs = model(**inputs)
26
  post_processed_output = image_processor.post_process_depth_estimation(
27
- outputs,
28
- target_sizes=[(image.height, image.width)],
29
  )
 
30
  depth_raw = post_processed_output[0]["predicted_depth"]
31
  depth_min = depth_raw.min().item()
32
  depth_max = depth_raw.max().item()
33
-
34
  inverse_depth = 1 / depth_raw
35
-
36
  normalized_inverse_depth = (inverse_depth - inverse_depth.min()) / (inverse_depth.max() - inverse_depth.min())
37
  normalized_inverse_depth = normalized_inverse_depth * 255.0
38
  normalized_inverse_depth = normalized_inverse_depth.detach().cpu().numpy()
39
  normalized_inverse_depth = PIL.Image.fromarray(normalized_inverse_depth.astype("uint8"))
40
-
 
 
41
  colored_inverse_depth = PIL.Image.fromarray(
42
  (cmap(np.array(normalized_inverse_depth))[:, :, :3] * 255).astype(np.uint8)
43
  )
44
-
45
  field_of_view = post_processed_output[0]["field_of_view"].item()
46
  focal_length = post_processed_output[0]["focal_length"].item()
47
-
48
  return (
49
  (image, colored_inverse_depth),
50
  f"{field_of_view:.2f}",
@@ -53,7 +52,6 @@ def run(image: PIL.Image.Image) -> tuple[tuple[PIL.Image.Image, PIL.Image.Image]
53
  f"{depth_max:.2f}",
54
  )
55
 
56
-
57
  with gr.Blocks(css="style.css") as demo:
58
  gr.Markdown("# DepthPro")
59
  with gr.Row():
@@ -62,11 +60,12 @@ with gr.Blocks(css="style.css") as demo:
62
  run_button = gr.Button()
63
  with gr.Column():
64
  output_image = ImageSlider()
65
- with gr.Row():
66
- output_field_of_view = gr.Textbox(label="Field of View")
67
- output_focal_length = gr.Textbox(label="Focal Length")
68
- output_depth_min = gr.Textbox(label="Depth Min")
69
- output_depth_max = gr.Textbox(label="Depth Max")
 
70
  gr.Examples(
71
  examples=sorted(pathlib.Path("images").glob("*.jpg")),
72
  inputs=input_image,
@@ -79,7 +78,7 @@ with gr.Blocks(css="style.css") as demo:
79
  output_depth_max,
80
  ],
81
  )
82
-
83
  run_button.click(
84
  fn=run,
85
  inputs=input_image,
@@ -93,4 +92,4 @@ with gr.Blocks(css="style.css") as demo:
93
  )
94
 
95
  if __name__ == "__main__":
96
- demo.queue().launch()
 
1
  #!/usr/bin/env python
 
2
  import pathlib
 
3
  import gradio as gr
4
  import matplotlib as mpl
5
  import numpy as np
 
13
  image_processor = DepthProImageProcessorFast.from_pretrained("apple/DepthPro-hf")
14
  model = DepthProForDepthEstimation.from_pretrained("apple/DepthPro-hf").to(device)
15
 
16
+ # Changed from Spectral_r to gray colormap
17
+ cmap = mpl.colormaps.get_cmap("gray")
18
 
19
  @spaces.GPU(duration=20)
20
  @torch.inference_mode()
 
22
  inputs = image_processor(images=image, return_tensors="pt").to(device)
23
  outputs = model(**inputs)
24
  post_processed_output = image_processor.post_process_depth_estimation(
25
+ outputs, target_sizes=[(image.height, image.width)],
 
26
  )
27
+
28
  depth_raw = post_processed_output[0]["predicted_depth"]
29
  depth_min = depth_raw.min().item()
30
  depth_max = depth_raw.max().item()
31
+
32
  inverse_depth = 1 / depth_raw
 
33
  normalized_inverse_depth = (inverse_depth - inverse_depth.min()) / (inverse_depth.max() - inverse_depth.min())
34
  normalized_inverse_depth = normalized_inverse_depth * 255.0
35
  normalized_inverse_depth = normalized_inverse_depth.detach().cpu().numpy()
36
  normalized_inverse_depth = PIL.Image.fromarray(normalized_inverse_depth.astype("uint8"))
37
+
38
+ # For grayscale, we can either use the normalized_inverse_depth directly as grayscale
39
+ # or apply the gray colormap - using colormap for consistency with original code
40
  colored_inverse_depth = PIL.Image.fromarray(
41
  (cmap(np.array(normalized_inverse_depth))[:, :, :3] * 255).astype(np.uint8)
42
  )
43
+
44
  field_of_view = post_processed_output[0]["field_of_view"].item()
45
  focal_length = post_processed_output[0]["focal_length"].item()
46
+
47
  return (
48
  (image, colored_inverse_depth),
49
  f"{field_of_view:.2f}",
 
52
  f"{depth_max:.2f}",
53
  )
54
 
 
55
  with gr.Blocks(css="style.css") as demo:
56
  gr.Markdown("# DepthPro")
57
  with gr.Row():
 
60
  run_button = gr.Button()
61
  with gr.Column():
62
  output_image = ImageSlider()
63
+ with gr.Row():
64
+ output_field_of_view = gr.Textbox(label="Field of View")
65
+ output_focal_length = gr.Textbox(label="Focal Length")
66
+ output_depth_min = gr.Textbox(label="Depth Min")
67
+ output_depth_max = gr.Textbox(label="Depth Max")
68
+
69
  gr.Examples(
70
  examples=sorted(pathlib.Path("images").glob("*.jpg")),
71
  inputs=input_image,
 
78
  output_depth_max,
79
  ],
80
  )
81
+
82
  run_button.click(
83
  fn=run,
84
  inputs=input_image,
 
92
  )
93
 
94
  if __name__ == "__main__":
95
+ demo.queue().launch()