Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,6 @@ import cv2
|
|
3 |
import numpy as np
|
4 |
import os
|
5 |
from PIL import Image
|
6 |
-
import spaces
|
7 |
import torch
|
8 |
import torch.nn.functional as F
|
9 |
from torchvision.transforms import Compose
|
@@ -25,11 +24,11 @@ css = """
|
|
25 |
}
|
26 |
"""
|
27 |
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
28 |
-
|
29 |
-
model = DepthAnything.from_pretrained(f"LiheYoung/depth_anything_{encoder}14").to(DEVICE).eval()
|
30 |
|
31 |
title = "# Depth Anything"
|
32 |
description = """Official demo for **Depth Anything: Unleashing the Power of Large-Scale Unlabeled Data**.
|
|
|
33 |
Please refer to our [paper](https://arxiv.org/abs/2401.10891), [project page](https://depth-anything.github.io), or [github](https://github.com/LiheYoung/Depth-Anything) for more details."""
|
34 |
|
35 |
transform = Compose([
|
@@ -46,54 +45,105 @@ transform = Compose([
|
|
46 |
PrepareForNet(),
|
47 |
])
|
48 |
|
49 |
-
|
|
|
50 |
@torch.no_grad()
|
51 |
def predict_depth(model, image):
|
52 |
return model(image)
|
53 |
|
54 |
-
|
55 |
with gr.Blocks(css=css) as demo:
|
56 |
gr.Markdown(title)
|
57 |
gr.Markdown(description)
|
58 |
gr.Markdown("### Depth Prediction demo")
|
59 |
gr.Markdown("You can slide the output to compare the depth prediction with input image")
|
60 |
|
61 |
-
with gr.
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
if __name__ == '__main__':
|
98 |
demo.queue().launch()
|
99 |
-
|
|
|
3 |
import numpy as np
|
4 |
import os
|
5 |
from PIL import Image
|
|
|
6 |
import torch
|
7 |
import torch.nn.functional as F
|
8 |
from torchvision.transforms import Compose
|
|
|
24 |
}
|
25 |
"""
|
26 |
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
27 |
+
model = DepthAnything.from_pretrained('LiheYoung/depth_anything_vitl14').to(DEVICE).eval()
|
|
|
28 |
|
29 |
title = "# Depth Anything"
|
30 |
description = """Official demo for **Depth Anything: Unleashing the Power of Large-Scale Unlabeled Data**.
|
31 |
+
|
32 |
Please refer to our [paper](https://arxiv.org/abs/2401.10891), [project page](https://depth-anything.github.io), or [github](https://github.com/LiheYoung/Depth-Anything) for more details."""
|
33 |
|
34 |
transform = Compose([
|
|
|
45 |
PrepareForNet(),
|
46 |
])
|
47 |
|
48 |
+
margin_width = 50
|
49 |
+
|
50 |
@torch.no_grad()
|
51 |
def predict_depth(model, image):
|
52 |
return model(image)
|
53 |
|
|
|
54 |
with gr.Blocks(css=css) as demo:
|
55 |
gr.Markdown(title)
|
56 |
gr.Markdown(description)
|
57 |
gr.Markdown("### Depth Prediction demo")
|
58 |
gr.Markdown("You can slide the output to compare the depth prediction with input image")
|
59 |
|
60 |
+
with gr.Tab("Image Depth Prediction"):
|
61 |
+
with gr.Row():
|
62 |
+
input_image = gr.Image(label="Input Image", type='numpy', elem_id='img-display-input')
|
63 |
+
depth_image_slider = ImageSlider(label="Depth Map with Slider View", elem_id='img-display-output', position=0.5)
|
64 |
+
raw_file = gr.File(label="16-bit raw depth (can be considered as disparity)")
|
65 |
+
submit = gr.Button("Submit")
|
66 |
+
|
67 |
+
def on_submit(image):
|
68 |
+
original_image = image.copy()
|
69 |
+
|
70 |
+
h, w = image.shape[:2]
|
71 |
+
|
72 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) / 255.0
|
73 |
+
image = transform({'image': image})['image']
|
74 |
+
image = torch.from_numpy(image).unsqueeze(0).to(DEVICE)
|
75 |
+
|
76 |
+
depth = predict_depth(model, image)
|
77 |
+
depth = F.interpolate(depth[None], (h, w), mode='bilinear', align_corners=False)[0, 0]
|
78 |
+
|
79 |
+
raw_depth = Image.fromarray(depth.cpu().numpy().astype('uint16'))
|
80 |
+
tmp = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
|
81 |
+
raw_depth.save(tmp.name)
|
82 |
+
|
83 |
+
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
|
84 |
+
depth = depth.cpu().numpy().astype(np.uint8)
|
85 |
+
colored_depth = cv2.applyColorMap(depth, cv2.COLORMAP_INFERNO)[:, :, ::-1]
|
86 |
+
|
87 |
+
return [(original_image, colored_depth), tmp.name]
|
88 |
+
|
89 |
+
submit.click(on_submit, inputs=[input_image], outputs=[depth_image_slider, raw_file])
|
90 |
+
|
91 |
+
example_files = os.listdir('assets/examples')
|
92 |
+
example_files.sort()
|
93 |
+
example_files = [os.path.join('assets/examples', filename) for filename in example_files]
|
94 |
+
examples = gr.Examples(examples=example_files, inputs=[input_image], outputs=[depth_image_slider, raw_file], fn=on_submit, cache_examples=True)
|
95 |
+
|
96 |
+
with gr.Tab("Video Depth Prediction"):
|
97 |
+
with gr.Row():
|
98 |
+
input_video = gr.Video(label="Input Video")
|
99 |
+
submit = gr.Button("Submit")
|
100 |
+
processed_video = gr.Video(label="Processed Video")
|
101 |
+
|
102 |
+
def on_submit(filename):
|
103 |
+
raw_video = cv2.VideoCapture(filename)
|
104 |
+
frame_width, frame_height = int(raw_video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(raw_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
105 |
+
frame_rate = int(raw_video.get(cv2.CAP_PROP_FPS))
|
106 |
+
output_width = frame_width * 2 + margin_width
|
107 |
+
|
108 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
|
109 |
+
output_path = tmpfile.name
|
110 |
+
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), frame_rate, (output_width, frame_height))
|
111 |
+
|
112 |
+
while raw_video.isOpened():
|
113 |
+
ret, raw_frame = raw_video.read()
|
114 |
+
if not ret:
|
115 |
+
break
|
116 |
+
|
117 |
+
frame = cv2.cvtColor(raw_frame, cv2.COLOR_BGR2RGB) / 255.0
|
118 |
+
|
119 |
+
frame = transform({'image': frame})['image']
|
120 |
+
frame = torch.from_numpy(frame).unsqueeze(0).to(DEVICE)
|
121 |
+
|
122 |
+
depth = predict_depth(model, frame)
|
123 |
+
|
124 |
+
depth = F.interpolate(depth[None], (frame_height, frame_width), mode='bilinear', align_corners=False)[0, 0]
|
125 |
+
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
|
126 |
+
|
127 |
+
depth = depth.cpu().numpy().astype(np.uint8)
|
128 |
+
depth_color = cv2.applyColorMap(depth, cv2.COLORMAP_INFERNO)
|
129 |
+
|
130 |
+
split_region = np.ones((frame_height, margin_width, 3), dtype=np.uint8) * 255
|
131 |
+
combined_frame = cv2.hconcat([raw_frame, split_region, depth_color])
|
132 |
+
|
133 |
+
out.write(combined_frame)
|
134 |
+
|
135 |
+
raw_video.release()
|
136 |
+
out.release()
|
137 |
+
|
138 |
+
return output_path
|
139 |
+
|
140 |
+
submit.click(on_submit, inputs=[input_video], outputs=processed_video)
|
141 |
+
|
142 |
+
example_files = os.listdir('assets/examples_video')
|
143 |
+
example_files.sort()
|
144 |
+
example_files = [os.path.join('assets/examples_video', filename) for filename in example_files]
|
145 |
+
examples = gr.Examples(examples=example_files, inputs=[input_video], outputs=processed_video, fn=on_submit, cache_examples=True)
|
146 |
+
|
147 |
|
148 |
if __name__ == '__main__':
|
149 |
demo.queue().launch()
|
|