Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,7 @@
|
|
| 14 |
|
| 15 |
import gc
|
| 16 |
import os
|
|
|
|
| 17 |
import uuid
|
| 18 |
import zipfile
|
| 19 |
|
|
@@ -21,6 +22,7 @@ import gradio as gr
|
|
| 21 |
import Imath
|
| 22 |
import numpy as np
|
| 23 |
import OpenEXR
|
|
|
|
| 24 |
import spaces
|
| 25 |
import torch
|
| 26 |
import torch.nn.functional as F
|
|
@@ -179,12 +181,16 @@ title = "# Video Depth Anything"
|
|
| 179 |
|
| 180 |
description = """
|
| 181 |
Official demo for **Video Depth Anything**, modified to export the model's
|
| 182 |
-
|
| 183 |
|
| 184 |
- **MP4 depth preview:** normalized/compressed visualization for viewing.
|
|
|
|
|
|
|
| 185 |
- **EXR ZIP:** raw floating-point depth values for Blender displacement.
|
| 186 |
|
| 187 |
-
For Blender,
|
|
|
|
|
|
|
| 188 |
"""
|
| 189 |
|
| 190 |
|
|
@@ -261,6 +267,144 @@ def zip_directory(source_dir: str, zip_path: str) -> str:
|
|
| 261 |
return zip_path
|
| 262 |
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
# =============================================================================
|
| 265 |
# INFERENCE
|
| 266 |
# =============================================================================
|
|
@@ -349,6 +493,11 @@ def infer_video_depth(
|
|
| 349 |
f"{base_name}_vis.mp4",
|
| 350 |
)
|
| 351 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
depth_exr_dir = os.path.join(
|
| 353 |
run_dir,
|
| 354 |
f"{base_name}_depth_EXR",
|
|
@@ -381,6 +530,20 @@ def infer_video_depth(
|
|
| 381 |
grayscale=bool(grayscale),
|
| 382 |
)
|
| 383 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
# -------------------------------------------------------------------------
|
| 385 |
# Save RAW floating-point EXR sequence
|
| 386 |
# -------------------------------------------------------------------------
|
|
@@ -417,6 +580,7 @@ def infer_video_depth(
|
|
| 417 |
return [
|
| 418 |
processed_video_path,
|
| 419 |
depth_vis_path,
|
|
|
|
| 420 |
zip_path,
|
| 421 |
]
|
| 422 |
|
|
@@ -467,6 +631,14 @@ def construct_demo():
|
|
| 467 |
interactive=False,
|
| 468 |
)
|
| 469 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 470 |
with gr.Row(equal_height=True):
|
| 471 |
|
| 472 |
with gr.Column(scale=1):
|
|
@@ -526,7 +698,7 @@ def construct_demo():
|
|
| 526 |
)
|
| 527 |
|
| 528 |
generate_btn = gr.Button(
|
| 529 |
-
"Generate Depth + EXR",
|
| 530 |
variant="primary",
|
| 531 |
)
|
| 532 |
|
|
@@ -545,6 +717,13 @@ def construct_demo():
|
|
| 545 |
|
| 546 |
The EXRs may look very dark in a normal image viewer. That is normal: they
|
| 547 |
contain raw floating-point model values instead of a normalized 8-bit preview.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
"""
|
| 549 |
)
|
| 550 |
|
|
@@ -561,6 +740,7 @@ contain raw floating-point model values instead of a normalized 8-bit preview.
|
|
| 561 |
outputs=[
|
| 562 |
processed_video,
|
| 563 |
depth_vis_video,
|
|
|
|
| 564 |
depth_exr_file,
|
| 565 |
],
|
| 566 |
fn=infer_video_depth,
|
|
@@ -580,6 +760,7 @@ contain raw floating-point model values instead of a normalized 8-bit preview.
|
|
| 580 |
outputs=[
|
| 581 |
processed_video,
|
| 582 |
depth_vis_video,
|
|
|
|
| 583 |
depth_exr_file,
|
| 584 |
],
|
| 585 |
)
|
|
|
|
| 14 |
|
| 15 |
import gc
|
| 16 |
import os
|
| 17 |
+
import subprocess
|
| 18 |
import uuid
|
| 19 |
import zipfile
|
| 20 |
|
|
|
|
| 22 |
import Imath
|
| 23 |
import numpy as np
|
| 24 |
import OpenEXR
|
| 25 |
+
import imageio_ffmpeg
|
| 26 |
import spaces
|
| 27 |
import torch
|
| 28 |
import torch.nn.functional as F
|
|
|
|
| 181 |
|
| 182 |
description = """
|
| 183 |
Official demo for **Video Depth Anything**, modified to export the model's
|
| 184 |
+
depth prediction in three useful formats.
|
| 185 |
|
| 186 |
- **MP4 depth preview:** normalized/compressed visualization for viewing.
|
| 187 |
+
- **16-bit grayscale depth video:** lossless FFV1 master with 65,536 gray
|
| 188 |
+
levels, full processed resolution, and no chroma subsampling.
|
| 189 |
- **EXR ZIP:** raw floating-point depth values for Blender displacement.
|
| 190 |
|
| 191 |
+
For Blender displacement, the EXR sequence remains the best choice. The
|
| 192 |
+
16-bit MKV is an additional high-quality grayscale video for editing,
|
| 193 |
+
archiving, and workflows that do not need the raw floating-point values.
|
| 194 |
"""
|
| 195 |
|
| 196 |
|
|
|
|
| 267 |
return zip_path
|
| 268 |
|
| 269 |
|
| 270 |
+
# =============================================================================
|
| 271 |
+
# LOSSLESS 16-BIT GRAYSCALE VIDEO EXPORT
|
| 272 |
+
# =============================================================================
|
| 273 |
+
|
| 274 |
+
def save_depths_as_lossless_gray16_video(
|
| 275 |
+
depths: np.ndarray,
|
| 276 |
+
output_path: str,
|
| 277 |
+
fps: float,
|
| 278 |
+
) -> str:
|
| 279 |
+
"""Save a full-resolution, lossless 16-bit grayscale FFV1 video.
|
| 280 |
+
|
| 281 |
+
Raw VDA values are relative depth and may extend outside 0-1. They are
|
| 282 |
+
normalized once using the minimum and maximum of the entire clip, then
|
| 283 |
+
quantized to 16 bits. Clip-wide normalization avoids the brightness
|
| 284 |
+
pumping that per-frame normalization would introduce.
|
| 285 |
+
|
| 286 |
+
FFV1 in Matroska is used because normal H.264 MP4 output is generally
|
| 287 |
+
8-bit and lossy. FFV1 preserves all 65,536 grayscale code values without
|
| 288 |
+
chroma subsampling or inter-frame loss. The raw 32-bit values are still
|
| 289 |
+
preserved separately in the EXR sequence.
|
| 290 |
+
"""
|
| 291 |
+
|
| 292 |
+
depths = np.asarray(depths, dtype=np.float32)
|
| 293 |
+
|
| 294 |
+
if depths.ndim != 3 or depths.shape[0] == 0:
|
| 295 |
+
raise ValueError(
|
| 296 |
+
f"Expected depth video with shape TxHxW, got {depths.shape}"
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
if not np.isfinite(fps) or fps <= 0:
|
| 300 |
+
raise ValueError(f"FPS must be positive, got {fps}")
|
| 301 |
+
|
| 302 |
+
depth_min = float(np.nanmin(depths))
|
| 303 |
+
depth_max = float(np.nanmax(depths))
|
| 304 |
+
|
| 305 |
+
if not np.isfinite(depth_min) or not np.isfinite(depth_max):
|
| 306 |
+
raise ValueError("Depth output contains no usable finite range")
|
| 307 |
+
|
| 308 |
+
depth_range = depth_max - depth_min
|
| 309 |
+
|
| 310 |
+
if depth_range <= np.finfo(np.float32).eps:
|
| 311 |
+
raise ValueError(
|
| 312 |
+
"Depth output is effectively constant, so a grayscale video "
|
| 313 |
+
"cannot be normalized"
|
| 314 |
+
)
|
| 315 |
+
|
| 316 |
+
os.makedirs(os.path.dirname(output_path) or ".", exist_ok=True)
|
| 317 |
+
|
| 318 |
+
height, width = depths.shape[1:]
|
| 319 |
+
ffmpeg_exe = imageio_ffmpeg.get_ffmpeg_exe()
|
| 320 |
+
|
| 321 |
+
command = [
|
| 322 |
+
ffmpeg_exe,
|
| 323 |
+
"-y",
|
| 324 |
+
"-hide_banner",
|
| 325 |
+
"-loglevel",
|
| 326 |
+
"error",
|
| 327 |
+
"-f",
|
| 328 |
+
"rawvideo",
|
| 329 |
+
"-pixel_format",
|
| 330 |
+
"gray16le",
|
| 331 |
+
"-video_size",
|
| 332 |
+
f"{width}x{height}",
|
| 333 |
+
"-framerate",
|
| 334 |
+
f"{float(fps):.8f}",
|
| 335 |
+
"-i",
|
| 336 |
+
"-",
|
| 337 |
+
"-an",
|
| 338 |
+
"-c:v",
|
| 339 |
+
"ffv1",
|
| 340 |
+
"-level",
|
| 341 |
+
"3",
|
| 342 |
+
"-coder",
|
| 343 |
+
"1",
|
| 344 |
+
"-context",
|
| 345 |
+
"1",
|
| 346 |
+
"-g",
|
| 347 |
+
"1",
|
| 348 |
+
"-slicecrc",
|
| 349 |
+
"1",
|
| 350 |
+
"-pix_fmt",
|
| 351 |
+
"gray16le",
|
| 352 |
+
output_path,
|
| 353 |
+
]
|
| 354 |
+
|
| 355 |
+
process = subprocess.Popen(
|
| 356 |
+
command,
|
| 357 |
+
stdin=subprocess.PIPE,
|
| 358 |
+
stdout=subprocess.DEVNULL,
|
| 359 |
+
stderr=subprocess.PIPE,
|
| 360 |
+
)
|
| 361 |
+
|
| 362 |
+
try:
|
| 363 |
+
for depth in depths:
|
| 364 |
+
# Replace exceptional values only in the downloadable video. The
|
| 365 |
+
# raw EXR sequence remains untouched.
|
| 366 |
+
clean_depth = np.nan_to_num(
|
| 367 |
+
depth,
|
| 368 |
+
nan=depth_min,
|
| 369 |
+
posinf=depth_max,
|
| 370 |
+
neginf=depth_min,
|
| 371 |
+
)
|
| 372 |
+
normalized = np.clip(
|
| 373 |
+
(clean_depth - depth_min) / depth_range,
|
| 374 |
+
0.0,
|
| 375 |
+
1.0,
|
| 376 |
+
)
|
| 377 |
+
gray16 = np.rint(normalized * 65535.0).astype("<u2")
|
| 378 |
+
process.stdin.write(np.ascontiguousarray(gray16).tobytes())
|
| 379 |
+
|
| 380 |
+
process.stdin.close()
|
| 381 |
+
process.stdin = None
|
| 382 |
+
stderr = process.stderr.read().decode("utf-8", errors="replace")
|
| 383 |
+
return_code = process.wait()
|
| 384 |
+
|
| 385 |
+
if return_code != 0:
|
| 386 |
+
raise RuntimeError(
|
| 387 |
+
"FFmpeg could not create the lossless 16-bit depth video: "
|
| 388 |
+
+ (stderr.strip() or f"exit code {return_code}")
|
| 389 |
+
)
|
| 390 |
+
|
| 391 |
+
except Exception:
|
| 392 |
+
if process.poll() is None:
|
| 393 |
+
process.kill()
|
| 394 |
+
process.wait()
|
| 395 |
+
|
| 396 |
+
if os.path.exists(output_path):
|
| 397 |
+
os.remove(output_path)
|
| 398 |
+
|
| 399 |
+
raise
|
| 400 |
+
|
| 401 |
+
finally:
|
| 402 |
+
if process.stderr is not None:
|
| 403 |
+
process.stderr.close()
|
| 404 |
+
|
| 405 |
+
return output_path
|
| 406 |
+
|
| 407 |
+
|
| 408 |
# =============================================================================
|
| 409 |
# INFERENCE
|
| 410 |
# =============================================================================
|
|
|
|
| 493 |
f"{base_name}_vis.mp4",
|
| 494 |
)
|
| 495 |
|
| 496 |
+
depth_gray16_path = os.path.join(
|
| 497 |
+
run_dir,
|
| 498 |
+
f"{base_name}_depth_gray16_lossless.mkv",
|
| 499 |
+
)
|
| 500 |
+
|
| 501 |
depth_exr_dir = os.path.join(
|
| 502 |
run_dir,
|
| 503 |
f"{base_name}_depth_EXR",
|
|
|
|
| 530 |
grayscale=bool(grayscale),
|
| 531 |
)
|
| 532 |
|
| 533 |
+
# -------------------------------------------------------------------------
|
| 534 |
+
# Save lossless 16-bit grayscale depth video
|
| 535 |
+
# -------------------------------------------------------------------------
|
| 536 |
+
|
| 537 |
+
print("Saving lossless 16-bit grayscale FFV1 video...")
|
| 538 |
+
|
| 539 |
+
save_depths_as_lossless_gray16_video(
|
| 540 |
+
depths,
|
| 541 |
+
depth_gray16_path,
|
| 542 |
+
fps=fps,
|
| 543 |
+
)
|
| 544 |
+
|
| 545 |
+
print("16-bit grayscale video:", depth_gray16_path)
|
| 546 |
+
|
| 547 |
# -------------------------------------------------------------------------
|
| 548 |
# Save RAW floating-point EXR sequence
|
| 549 |
# -------------------------------------------------------------------------
|
|
|
|
| 580 |
return [
|
| 581 |
processed_video_path,
|
| 582 |
depth_vis_path,
|
| 583 |
+
depth_gray16_path,
|
| 584 |
zip_path,
|
| 585 |
]
|
| 586 |
|
|
|
|
| 631 |
interactive=False,
|
| 632 |
)
|
| 633 |
|
| 634 |
+
depth_gray16_file = gr.File(
|
| 635 |
+
label=(
|
| 636 |
+
"Lossless 16-bit Grayscale Depth Video "
|
| 637 |
+
"(FFV1 .mkv)"
|
| 638 |
+
),
|
| 639 |
+
interactive=False,
|
| 640 |
+
)
|
| 641 |
+
|
| 642 |
with gr.Row(equal_height=True):
|
| 643 |
|
| 644 |
with gr.Column(scale=1):
|
|
|
|
| 698 |
)
|
| 699 |
|
| 700 |
generate_btn = gr.Button(
|
| 701 |
+
"Generate Depth + 16-bit Video + EXR",
|
| 702 |
variant="primary",
|
| 703 |
)
|
| 704 |
|
|
|
|
| 717 |
|
| 718 |
The EXRs may look very dark in a normal image viewer. That is normal: they
|
| 719 |
contain raw floating-point model values instead of a normalized 8-bit preview.
|
| 720 |
+
|
| 721 |
+
### 16-bit grayscale video
|
| 722 |
+
|
| 723 |
+
The MKV download uses lossless FFV1 at the full processed frame resolution.
|
| 724 |
+
It stores 65,536 grayscale levels instead of the 256 levels in a normal 8-bit
|
| 725 |
+
MP4. VLC and FFmpeg-based tools can read FFV1/MKV. Some web browsers will not
|
| 726 |
+
preview it, so it is provided as a download rather than an embedded player.
|
| 727 |
"""
|
| 728 |
)
|
| 729 |
|
|
|
|
| 740 |
outputs=[
|
| 741 |
processed_video,
|
| 742 |
depth_vis_video,
|
| 743 |
+
depth_gray16_file,
|
| 744 |
depth_exr_file,
|
| 745 |
],
|
| 746 |
fn=infer_video_depth,
|
|
|
|
| 760 |
outputs=[
|
| 761 |
processed_video,
|
| 762 |
depth_vis_video,
|
| 763 |
+
depth_gray16_file,
|
| 764 |
depth_exr_file,
|
| 765 |
],
|
| 766 |
)
|