Spaces:
Running
Running
SauravMaheshkar
commited on
Commit
•
1fbc150
1
Parent(s):
6bcae54
feat: init app
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import depth_pro
|
2 |
+
import gradio as gr
|
3 |
+
import matplotlib.cm as cm
|
4 |
+
import numpy as np
|
5 |
+
from depth_pro.depth_pro import DepthProConfig
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
|
9 |
+
def run(input_image_path):
|
10 |
+
config = DepthProConfig(
|
11 |
+
patch_encoder_preset="dinov2l16_384",
|
12 |
+
image_encoder_preset="dinov2l16_384",
|
13 |
+
checkpoint_uri="./depth_pro.pt",
|
14 |
+
decoder_features=256,
|
15 |
+
use_fov_head=True,
|
16 |
+
fov_encoder_preset="dinov2l16_384",
|
17 |
+
)
|
18 |
+
|
19 |
+
# Load model and preprocessing transform
|
20 |
+
model, transform = depth_pro.create_model_and_transforms(config=config)
|
21 |
+
model.eval()
|
22 |
+
|
23 |
+
# Load and preprocess an image
|
24 |
+
image, _, f_px = depth_pro.load_rgb(input_image_path)
|
25 |
+
image = transform(image)
|
26 |
+
|
27 |
+
# Run inference
|
28 |
+
prediction = model.infer(image, f_px=f_px)
|
29 |
+
|
30 |
+
depth_map = prediction["depth"].squeeze().cpu().numpy()
|
31 |
+
focallength_px = prediction["focallength_px"]
|
32 |
+
|
33 |
+
depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
|
34 |
+
colormap = cm.get_cmap("viridis")
|
35 |
+
depth_map = colormap(depth_map)
|
36 |
+
depth_map = (depth_map[:, :, :3] * 255).astype(np.uint8)
|
37 |
+
|
38 |
+
depth_map = Image.fromarray(depth_map)
|
39 |
+
|
40 |
+
return depth_map, focallength_px.item()
|
41 |
+
|
42 |
+
|
43 |
+
demo = gr.Interface(
|
44 |
+
fn=run,
|
45 |
+
inputs=gr.Image(label="Input Image", type="filepath"),
|
46 |
+
outputs=[
|
47 |
+
gr.Image(label="Depth Map"),
|
48 |
+
gr.Number(label="Focal Length"),
|
49 |
+
],
|
50 |
+
description="""
|
51 |
+
<div align="center">
|
52 |
+
<h2><a href="https://arxiv.org/abs/2410.02073">Depth Pro: Sharp Monocular Metric Depth in Less Than a Second</a></h2>
|
53 |
+
</div>
|
54 |
+
""",
|
55 |
+
)
|
56 |
+
demo.launch()
|