Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,9 @@
|
|
| 14 |
import spaces
|
| 15 |
import gradio as gr
|
| 16 |
import gc
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
import numpy as np
|
| 19 |
import os
|
|
@@ -100,26 +103,119 @@ def infer_video_depth(
|
|
| 100 |
max_len: int = -1,
|
| 101 |
target_fps: int = -1,
|
| 102 |
max_res: int = 1280,
|
| 103 |
-
grayscale: bool = False,
|
| 104 |
output_dir: str = './outputs',
|
| 105 |
input_size: int = 518,
|
| 106 |
):
|
| 107 |
-
frames, target_fps = read_video_frames(
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
video_name = os.path.basename(input_video)
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
processed_video_path = os.path.join(output_dir, os.path.splitext(video_name)[0]+'_src.mp4')
|
| 115 |
-
depth_vis_path = os.path.join(output_dir, os.path.splitext(video_name)[0]+'_vis.mp4')
|
| 116 |
save_video(frames, processed_video_path, fps=fps)
|
| 117 |
-
save_video(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
-
|
| 120 |
-
torch.cuda.empty_cache()
|
| 121 |
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
|
| 125 |
def construct_demo():
|
|
@@ -151,6 +247,10 @@ def construct_demo():
|
|
| 151 |
show_share_button=True,
|
| 152 |
scale=5,
|
| 153 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
with gr.Row(equal_height=True):
|
| 156 |
with gr.Column(scale=1):
|
|
@@ -193,7 +293,11 @@ def construct_demo():
|
|
| 193 |
target_fps,
|
| 194 |
max_res
|
| 195 |
],
|
| 196 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
fn=infer_video_depth,
|
| 198 |
cache_examples="lazy",
|
| 199 |
)
|
|
@@ -207,7 +311,11 @@ def construct_demo():
|
|
| 207 |
max_res,
|
| 208 |
grayscale
|
| 209 |
],
|
| 210 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
)
|
| 212 |
|
| 213 |
return demo
|
|
|
|
| 14 |
import spaces
|
| 15 |
import gradio as gr
|
| 16 |
import gc
|
| 17 |
+
import zipfile
|
| 18 |
+
import OpenEXR
|
| 19 |
+
import Imath
|
| 20 |
|
| 21 |
import numpy as np
|
| 22 |
import os
|
|
|
|
| 103 |
max_len: int = -1,
|
| 104 |
target_fps: int = -1,
|
| 105 |
max_res: int = 1280,
|
|
|
|
| 106 |
output_dir: str = './outputs',
|
| 107 |
input_size: int = 518,
|
| 108 |
):
|
| 109 |
+
frames, target_fps = read_video_frames(
|
| 110 |
+
input_video,
|
| 111 |
+
max_len,
|
| 112 |
+
target_fps,
|
| 113 |
+
max_res
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
depth_list, fps = video_depth_anything.infer_video_depth(
|
| 117 |
+
frames,
|
| 118 |
+
target_fps,
|
| 119 |
+
input_size=input_size,
|
| 120 |
+
device=DEVICE
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# Raw model output
|
| 124 |
+
depth_list = np.stack(depth_list, axis=0).astype(np.float32)
|
| 125 |
+
|
| 126 |
+
# MP4 visualization
|
| 127 |
+
vis = vis_sequence_depth(depth_list)
|
| 128 |
|
| 129 |
video_name = os.path.basename(input_video)
|
| 130 |
+
base_name = os.path.splitext(video_name)[0]
|
| 131 |
+
|
| 132 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 133 |
+
|
| 134 |
+
processed_video_path = os.path.join(
|
| 135 |
+
output_dir,
|
| 136 |
+
base_name + '_src.mp4'
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
depth_vis_path = os.path.join(
|
| 140 |
+
output_dir,
|
| 141 |
+
base_name + '_vis.mp4'
|
| 142 |
+
)
|
| 143 |
|
|
|
|
|
|
|
| 144 |
save_video(frames, processed_video_path, fps=fps)
|
| 145 |
+
save_video(vis, depth_vis_path, fps=fps)
|
| 146 |
+
|
| 147 |
+
# ----------------------------------------------------
|
| 148 |
+
# RAW 32-BIT FLOAT EXR EXPORT
|
| 149 |
+
# ----------------------------------------------------
|
| 150 |
+
|
| 151 |
+
depth_exr_dir = os.path.join(
|
| 152 |
+
output_dir,
|
| 153 |
+
base_name + '_depth_EXR'
|
| 154 |
+
)
|
| 155 |
|
| 156 |
+
os.makedirs(depth_exr_dir, exist_ok=True)
|
|
|
|
| 157 |
|
| 158 |
+
FLOAT = Imath.PixelType(Imath.PixelType.FLOAT)
|
| 159 |
+
|
| 160 |
+
for i, depth in enumerate(depth_list):
|
| 161 |
+
|
| 162 |
+
depth = np.ascontiguousarray(depth.astype(np.float32))
|
| 163 |
+
|
| 164 |
+
height, width = depth.shape
|
| 165 |
+
|
| 166 |
+
exr_path = os.path.join(
|
| 167 |
+
depth_exr_dir,
|
| 168 |
+
f"frame_{i:05d}.exr"
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
header = OpenEXR.Header(width, height)
|
| 172 |
+
|
| 173 |
+
# Store depth in RGB so Blender can easily read it
|
| 174 |
+
# through an Image Texture node.
|
| 175 |
+
header["channels"] = {
|
| 176 |
+
"R": Imath.Channel(FLOAT),
|
| 177 |
+
"G": Imath.Channel(FLOAT),
|
| 178 |
+
"B": Imath.Channel(FLOAT),
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
depth_bytes = depth.tobytes()
|
| 182 |
+
|
| 183 |
+
exr = OpenEXR.OutputFile(exr_path, header)
|
| 184 |
+
|
| 185 |
+
exr.writePixels({
|
| 186 |
+
"R": depth_bytes,
|
| 187 |
+
"G": depth_bytes,
|
| 188 |
+
"B": depth_bytes,
|
| 189 |
+
})
|
| 190 |
+
|
| 191 |
+
exr.close()
|
| 192 |
+
|
| 193 |
+
# ZIP the EXRs because Gradio cannot conveniently
|
| 194 |
+
# return a directory as one downloadable result.
|
| 195 |
+
zip_path = os.path.join(
|
| 196 |
+
output_dir,
|
| 197 |
+
base_name + '_depth_EXR.zip'
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
with zipfile.ZipFile(
|
| 201 |
+
zip_path,
|
| 202 |
+
'w',
|
| 203 |
+
compression=zipfile.ZIP_DEFLATED
|
| 204 |
+
) as zipf:
|
| 205 |
+
|
| 206 |
+
for filename in sorted(os.listdir(depth_exr_dir)):
|
| 207 |
+
file_path = os.path.join(depth_exr_dir, filename)
|
| 208 |
+
|
| 209 |
+
zipf.write(
|
| 210 |
+
file_path,
|
| 211 |
+
arcname=filename
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
return [
|
| 215 |
+
processed_video_path,
|
| 216 |
+
depth_vis_path,
|
| 217 |
+
zip_path
|
| 218 |
+
]
|
| 219 |
|
| 220 |
|
| 221 |
def construct_demo():
|
|
|
|
| 247 |
show_share_button=True,
|
| 248 |
scale=5,
|
| 249 |
)
|
| 250 |
+
depth_exr_file = gr.File(
|
| 251 |
+
label="Raw 32-bit EXR Depth Sequence",
|
| 252 |
+
interactive=False
|
| 253 |
+
)
|
| 254 |
|
| 255 |
with gr.Row(equal_height=True):
|
| 256 |
with gr.Column(scale=1):
|
|
|
|
| 293 |
target_fps,
|
| 294 |
max_res
|
| 295 |
],
|
| 296 |
+
outputs=[
|
| 297 |
+
processed_video,
|
| 298 |
+
depth_vis_video,
|
| 299 |
+
depth_exr_file
|
| 300 |
+
],
|
| 301 |
fn=infer_video_depth,
|
| 302 |
cache_examples="lazy",
|
| 303 |
)
|
|
|
|
| 311 |
max_res,
|
| 312 |
grayscale
|
| 313 |
],
|
| 314 |
+
outputs=[
|
| 315 |
+
processed_video,
|
| 316 |
+
depth_vis_video,
|
| 317 |
+
depth_exr_file
|
| 318 |
+
],
|
| 319 |
)
|
| 320 |
|
| 321 |
return demo
|