niye4 commited on
Commit
fa50454
·
verified ·
1 Parent(s): 9eeb5d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -21
app.py CHANGED
@@ -4,10 +4,7 @@ import numpy as np
4
  import torch
5
  from PIL import Image
6
  import gradio as gr
7
- from gradio_imageslider import ImageSlider
8
  from depth_anything_v2.dpt import DepthAnythingV2
9
- import matplotlib.pyplot as plt
10
- import matplotlib
11
 
12
  # ===============================
13
  # Device & Model
@@ -29,22 +26,19 @@ def predict_depth(frame_rgb):
29
  return depth.astype(np.float32)
30
 
31
  # ===============================
32
- # Colormap for preview
33
  # ===============================
34
- cmap = matplotlib.cm.get_cmap('magma') # nice perceptual colormap
35
-
36
- def apply_colormap(depth):
37
- """Scale depth to 0-1 and apply colormap, return uint8 RGB"""
38
  norm = (depth - depth.min()) / (depth.max() - depth.min() + 1e-8)
39
- colored = (cmap(norm)[:, :, :3] * 255).astype(np.uint8)
40
- return colored
41
 
42
  # ===============================
43
  # Process video
44
  # ===============================
45
  def process_video(video_file):
46
  """
47
- Render depthmap video with colormap.
48
  Keep original resolution & FPS.
49
  """
50
  OUTPUT_DIR = "output"
@@ -66,7 +60,7 @@ def process_video(video_file):
66
  # Video output path
67
  output_video_path = os.path.join(OUTPUT_DIR, os.path.basename(video_path).replace(".mp4","_depth.mp4"))
68
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
69
- out = cv2.VideoWriter(output_video_path, fourcc, fps, (width,height), isColor=True)
70
 
71
  # Slider preview (sample frames)
72
  slider_frames = []
@@ -82,14 +76,12 @@ def process_video(video_file):
82
 
83
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
84
  depth_map = predict_depth(frame_rgb)
85
-
86
- # Apply colormap for video output
87
- colored_frame = apply_colormap(depth_map)
88
- out.write(cv2.cvtColor(colored_frame, cv2.COLOR_RGB2BGR))
89
 
90
  # Add sampled frames for slider preview
91
  if idx % step == 0:
92
- slider_frames.append(Image.fromarray(colored_frame))
93
  idx += 1
94
 
95
  cap.release()
@@ -100,15 +92,15 @@ def process_video(video_file):
100
  # Gradio Interface
101
  # ===============================
102
  with gr.Blocks() as demo:
103
- gr.Markdown("# Depth Anything V2 – Depth Video (vitb)")
104
  gr.Markdown(
105
- "Upload an MP4 video to generate a **colored DepthMap video**.\n\n"
106
- "**Model:** vitb – fast and high quality for real-time processing.\n"
107
  "Resolution and FPS are preserved from the original video."
108
  )
109
 
110
  video_input = gr.File(label="Upload MP4", file_types=['.mp4'])
111
- depth_slider = ImageSlider(label="DepthMap Slider Preview")
112
  video_output = gr.Video(label="DepthMap Video")
113
  submit = gr.Button("Render DepthMap")
114
 
 
4
  import torch
5
  from PIL import Image
6
  import gradio as gr
 
7
  from depth_anything_v2.dpt import DepthAnythingV2
 
 
8
 
9
  # ===============================
10
  # Device & Model
 
26
  return depth.astype(np.float32)
27
 
28
  # ===============================
29
+ # Normalize to 0-255 grayscale
30
  # ===============================
31
+ def depth_to_grayscale(depth):
 
 
 
32
  norm = (depth - depth.min()) / (depth.max() - depth.min() + 1e-8)
33
+ gray = (norm * 255).astype(np.uint8)
34
+ return gray
35
 
36
  # ===============================
37
  # Process video
38
  # ===============================
39
  def process_video(video_file):
40
  """
41
+ Render grayscale depthmap video.
42
  Keep original resolution & FPS.
43
  """
44
  OUTPUT_DIR = "output"
 
60
  # Video output path
61
  output_video_path = os.path.join(OUTPUT_DIR, os.path.basename(video_path).replace(".mp4","_depth.mp4"))
62
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
63
+ out = cv2.VideoWriter(output_video_path, fourcc, fps, (width,height), isColor=False)
64
 
65
  # Slider preview (sample frames)
66
  slider_frames = []
 
76
 
77
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
78
  depth_map = predict_depth(frame_rgb)
79
+ gray_frame = depth_to_grayscale(depth_map)
80
+ out.write(gray_frame)
 
 
81
 
82
  # Add sampled frames for slider preview
83
  if idx % step == 0:
84
+ slider_frames.append(Image.fromarray(gray_frame))
85
  idx += 1
86
 
87
  cap.release()
 
92
  # Gradio Interface
93
  # ===============================
94
  with gr.Blocks() as demo:
95
+ gr.Markdown("# Depth Anything V2 – Grayscale Depth Video (vitb)")
96
  gr.Markdown(
97
+ "Upload an MP4 video to generate a **grayscale DepthMap video**.\n\n"
98
+ "**Model:** vitb – fast and high quality for video processing.\n"
99
  "Resolution and FPS are preserved from the original video."
100
  )
101
 
102
  video_input = gr.File(label="Upload MP4", file_types=['.mp4'])
103
+ depth_slider = gr.Gallery(label="DepthMap Slider Preview", elem_id="depth_slider")
104
  video_output = gr.Video(label="DepthMap Video")
105
  submit = gr.Button("Render DepthMap")
106