Spaces:
Sleeping
Sleeping
Update space
Browse files
app.py
CHANGED
|
@@ -1,18 +1,70 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import os
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import spaces
|
| 7 |
+
import torch
|
| 8 |
+
from gradio_imageslider import ImageSlider
|
| 9 |
|
| 10 |
+
css = """
|
| 11 |
+
#img-display-container {
|
| 12 |
+
max-height: 100vh;
|
| 13 |
+
}
|
| 14 |
+
#img-display-input {
|
| 15 |
+
max-height: 80vh;
|
| 16 |
+
}
|
| 17 |
+
#img-display-output {
|
| 18 |
+
max-height: 80vh;
|
| 19 |
+
}
|
| 20 |
+
"""
|
| 21 |
+
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 22 |
+
title = "# Stereo Anything"
|
| 23 |
+
description = """Official demo for **Stereo Anything: Unifying Stereo Matching with Large-Scale Mixed Data**.
|
| 24 |
+
Please refer to our [paper](https://arxiv.org/abs/2411.14053), [github](https://github.com/XiandaGuo/OpenStereo/) for more details."""
|
| 25 |
+
|
| 26 |
+
@spaces.GPU
|
| 27 |
+
@torch.no_grad()
|
| 28 |
+
def predict_depth(model, image):
|
| 29 |
+
return model(image)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
with gr.Blocks(css=css) as demo:
|
| 33 |
+
gr.Markdown(title)
|
| 34 |
+
gr.Markdown(description)
|
| 35 |
+
gr.Markdown("### Depth Prediction demo")
|
| 36 |
+
gr.Markdown("You can slide the output to compare the depth prediction with input image")
|
| 37 |
+
|
| 38 |
+
with gr.Row():
|
| 39 |
+
left_image = gr.Image(label="Left Image", type='numpy', elem_id='img-display-input')
|
| 40 |
+
right_image = gr.Image(label="Right Image", type='numpy', elem_id='img-display-input')
|
| 41 |
+
depth_image_slider = ImageSlider(label="Depth Map with Slider View", elem_id='img-display-output', position=0.5,)
|
| 42 |
+
# raw_file = gr.File(label="16-bit raw depth (can be considered as disparity)")
|
| 43 |
+
submit = gr.Button("Submit")
|
| 44 |
+
|
| 45 |
+
def on_submit(left_image,right_image):
|
| 46 |
+
sample = {
|
| 47 |
+
'left': left_image,
|
| 48 |
+
'right': right_image,
|
| 49 |
+
}
|
| 50 |
+
sample['left'] = sample['left'].unsqueeze(0)
|
| 51 |
+
sample['right'] = sample['right'].unsqueeze(0)
|
| 52 |
+
|
| 53 |
+
model.eval()
|
| 54 |
+
for k, v in sample.items():
|
| 55 |
+
sample[k] = v.to(0) if torch.is_tensor(v) else v
|
| 56 |
+
|
| 57 |
+
model_pred = model(sample)
|
| 58 |
+
|
| 59 |
+
return [model_pred]
|
| 60 |
+
|
| 61 |
+
submit.click(on_submit, inputs=[left_image,right_image], outputs=[depth_image_slider])
|
| 62 |
+
|
| 63 |
+
example_files = os.listdir('examples')
|
| 64 |
+
example_files.sort()
|
| 65 |
+
example_files = [os.path.join('examples', filename) for filename in example_files]
|
| 66 |
+
examples = gr.Examples(examples=example_files, inputs=[left_image,right_image], outputs=[depth_image_slider], fn=on_submit, cache_examples=True)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
if __name__ == '__main__':
|
| 70 |
+
demo.queue().launch()
|