Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -60,48 +60,53 @@ title = "# Depth Anything V2"
|
|
60 |
description = """Official demo for **Depth Anything V2**.
|
61 |
Please refer to our [paper](https://arxiv.org/abs/2406.09414), [project page](https://depth-anything-v2.github.io), and [github](https://github.com/DepthAnything/Depth-Anything-V2) for more details."""
|
62 |
|
63 |
-
@
|
64 |
def predict_depth(image):
|
65 |
-
return
|
66 |
|
67 |
-
with gr.Blocks(css=
|
68 |
gr.Markdown(title)
|
69 |
gr.Markdown(description)
|
70 |
gr.Markdown("### Depth Prediction demo")
|
71 |
|
72 |
with gr.Row():
|
73 |
input_image = gr.Image(label="Input Image", type='numpy', elem_id='img-display-input')
|
74 |
-
depth_image_slider =
|
75 |
submit = gr.Button(value="Compute Depth")
|
76 |
-
gray_depth_file = gr.File(label="Grayscale depth map", elem_id="download")
|
77 |
-
raw_file = gr.File(label="16-bit raw output (can be considered as disparity)", elem_id="download")
|
78 |
|
79 |
cmap = matplotlib.colormaps.get_cmap('Spectral_r')
|
80 |
|
81 |
def on_submit(image):
|
82 |
-
|
|
|
|
|
|
|
|
|
83 |
|
84 |
-
# Convert depth to images and save
|
85 |
raw_depth = Image.fromarray(depth.astype('uint16'))
|
86 |
tmp_raw_depth = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
|
87 |
raw_depth.save(tmp_raw_depth.name)
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
colored_depth = (cmap(normalized_depth)[:, :, :3] * 255).astype(np.uint8)
|
93 |
|
94 |
-
gray_depth = Image.fromarray(
|
95 |
tmp_gray_depth = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
|
96 |
gray_depth.save(tmp_gray_depth.name)
|
97 |
|
98 |
-
return [(
|
99 |
|
100 |
submit.click(on_submit, inputs=[input_image], outputs=[depth_image_slider, gray_depth_file, raw_file])
|
101 |
|
102 |
-
example_files =
|
|
|
103 |
example_files = [os.path.join('assets/examples', filename) for filename in example_files]
|
104 |
examples = gr.Examples(examples=example_files, inputs=[input_image], outputs=[depth_image_slider, gray_depth_file, raw_file], fn=on_submit)
|
105 |
|
|
|
106 |
if __name__ == '__main__':
|
107 |
demo.queue().launch(share=True)
|
|
|
|
60 |
description = """Official demo for **Depth Anything V2**.
|
61 |
Please refer to our [paper](https://arxiv.org/abs/2406.09414), [project page](https://depth-anything-v2.github.io), and [github](https://github.com/DepthAnything/Depth-Anything-V2) for more details."""
|
62 |
|
63 |
+
@spaces.GPU
|
64 |
def predict_depth(image):
|
65 |
+
return model.infer_image(image)
|
66 |
|
67 |
+
with gr.Blocks(css=css) as demo:
|
68 |
gr.Markdown(title)
|
69 |
gr.Markdown(description)
|
70 |
gr.Markdown("### Depth Prediction demo")
|
71 |
|
72 |
with gr.Row():
|
73 |
input_image = gr.Image(label="Input Image", type='numpy', elem_id='img-display-input')
|
74 |
+
depth_image_slider = ImageSlider(label="Depth Map with Slider View", elem_id='img-display-output', position=0.5)
|
75 |
submit = gr.Button(value="Compute Depth")
|
76 |
+
gray_depth_file = gr.File(label="Grayscale depth map", elem_id="download",)
|
77 |
+
raw_file = gr.File(label="16-bit raw output (can be considered as disparity)", elem_id="download",)
|
78 |
|
79 |
cmap = matplotlib.colormaps.get_cmap('Spectral_r')
|
80 |
|
81 |
def on_submit(image):
|
82 |
+
original_image = image.copy()
|
83 |
+
|
84 |
+
h, w = image.shape[:2]
|
85 |
+
|
86 |
+
depth = predict_depth(image[:, :, ::-1])
|
87 |
|
|
|
88 |
raw_depth = Image.fromarray(depth.astype('uint16'))
|
89 |
tmp_raw_depth = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
|
90 |
raw_depth.save(tmp_raw_depth.name)
|
91 |
|
92 |
+
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
|
93 |
+
depth = depth.astype(np.uint8)
|
94 |
+
colored_depth = (cmap(depth)[:, :, :3] * 255).astype(np.uint8)
|
|
|
95 |
|
96 |
+
gray_depth = Image.fromarray(depth)
|
97 |
tmp_gray_depth = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
|
98 |
gray_depth.save(tmp_gray_depth.name)
|
99 |
|
100 |
+
return [(original_image, colored_depth), tmp_gray_depth.name, tmp_raw_depth.name]
|
101 |
|
102 |
submit.click(on_submit, inputs=[input_image], outputs=[depth_image_slider, gray_depth_file, raw_file])
|
103 |
|
104 |
+
example_files = os.listdir('assets/examples')
|
105 |
+
example_files.sort()
|
106 |
example_files = [os.path.join('assets/examples', filename) for filename in example_files]
|
107 |
examples = gr.Examples(examples=example_files, inputs=[input_image], outputs=[depth_image_slider, gray_depth_file, raw_file], fn=on_submit)
|
108 |
|
109 |
+
|
110 |
if __name__ == '__main__':
|
111 |
demo.queue().launch(share=True)
|
112 |
+
|