Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -12,19 +12,19 @@ from utils.dc_utils import read_video_frames, save_video
|
|
12 |
from huggingface_hub import hf_hub_download
|
13 |
|
14 |
# Examples for the Gradio Demo.
|
15 |
-
# Each example now contains 8 parameters:
|
16 |
-
# [video_path, max_len, target_fps, max_res, stitch, grayscale,
|
17 |
examples = [
|
18 |
-
['assets/example_videos/davis_rollercoaster.mp4', -1, -1, 1280, True, True,
|
19 |
-
['assets/example_videos/Tokyo-Walk_rgb.mp4', -1, -1, 1280, True, True,
|
20 |
-
['assets/example_videos/4158877-uhd_3840_2160_30fps_rgb.mp4', -1, -1, 1280, True, True,
|
21 |
-
['assets/example_videos/4511004-uhd_3840_2160_24fps_rgb.mp4', -1, -1, 1280, True, True,
|
22 |
-
['assets/example_videos/1753029-hd_1920_1080_30fps.mp4', -1, -1, 1280, True, True,
|
23 |
-
['assets/example_videos/davis_burnout.mp4', -1, -1, 1280, True, True,
|
24 |
-
['assets/example_videos/example_5473765-l.mp4', -1, -1, 1280, True, True,
|
25 |
-
['assets/example_videos/Istanbul-26920.mp4', -1, -1, 1280, True, True,
|
26 |
-
['assets/example_videos/obj_1.mp4', -1, -1, 1280, True, True,
|
27 |
-
['assets/example_videos/sheep_cut1.mp4', -1, -1, 1280, True, True,
|
28 |
]
|
29 |
|
30 |
# Use GPU if available; otherwise, use CPU.
|
@@ -63,11 +63,10 @@ def infer_video_depth(
|
|
63 |
max_res: int = 1280,
|
64 |
stitch: bool = True,
|
65 |
grayscale: bool = True,
|
66 |
-
|
67 |
-
|
68 |
output_dir: str = './outputs',
|
69 |
input_size: int = 518,
|
70 |
-
convert_from_color: bool = True,
|
71 |
):
|
72 |
# 1. Read input video frames for inference (downscaled to max_res).
|
73 |
frames, target_fps = read_video_frames(input_video, max_len, target_fps, max_res)
|
@@ -94,7 +93,7 @@ def infer_video_depth(
|
|
94 |
for i in range(min(len(full_frames), len(depths))):
|
95 |
rgb_full = full_frames[i] # Full-resolution RGB frame.
|
96 |
depth_frame = depths[i]
|
97 |
-
# Normalize the depth frame to [0, 255].
|
98 |
depth_norm = ((depth_frame - d_min) / (d_max - d_min) * 255).astype(np.uint8)
|
99 |
# Generate depth visualization:
|
100 |
if grayscale:
|
@@ -113,16 +112,16 @@ def infer_video_depth(
|
|
113 |
depth_vis = (cmap(depth_norm / 255.0)[..., :3] * 255).astype(np.uint8)
|
114 |
# Apply Gaussian blur if requested.
|
115 |
if blur > 0:
|
116 |
-
kernel_size = int(blur * 20) * 2 + 1 #
|
117 |
depth_vis = cv2.GaussianBlur(depth_vis, (kernel_size, kernel_size), 0)
|
118 |
# Resize the depth visualization to match the full-resolution RGB frame.
|
119 |
H_full, W_full = rgb_full.shape[:2]
|
120 |
depth_vis_resized = cv2.resize(depth_vis, (W_full, H_full))
|
121 |
-
# Concatenate full-resolution RGB (left) and resized depth visualization (right).
|
122 |
stitched = cv2.hconcat([rgb_full, depth_vis_resized])
|
123 |
stitched_frames.append(stitched)
|
124 |
stitched_frames = np.array(stitched_frames)
|
125 |
-
#
|
126 |
base_name = os.path.splitext(video_name)[0]
|
127 |
short_name = base_name[:20]
|
128 |
stitched_video_path = os.path.join(output_dir, short_name + '_RGBD.mp4')
|
@@ -175,15 +174,15 @@ def construct_demo():
|
|
175 |
max_res = gr.Slider(label="Max side resolution", minimum=480, maximum=1920, value=1280, step=1)
|
176 |
stitch_option = gr.Checkbox(label="Stitch RGB & Depth Videos", value=True)
|
177 |
grayscale_option = gr.Checkbox(label="Output Depth as Grayscale", value=True)
|
178 |
-
blur_slider = gr.Slider(minimum=0, maximum=1, step=0.01, label="Depth Blur Factor", value=0)
|
179 |
convert_from_color_option = gr.Checkbox(label="Convert Grayscale from Color", value=True)
|
|
|
180 |
generate_btn = gr.Button("Generate")
|
181 |
with gr.Column(scale=2):
|
182 |
pass
|
183 |
|
184 |
gr.Examples(
|
185 |
examples=examples,
|
186 |
-
inputs=[input_video, max_len, target_fps, max_res, stitch_option, grayscale_option,
|
187 |
outputs=[processed_video, depth_vis_video, stitched_video],
|
188 |
fn=infer_video_depth,
|
189 |
cache_examples=True,
|
@@ -192,7 +191,7 @@ def construct_demo():
|
|
192 |
|
193 |
generate_btn.click(
|
194 |
fn=infer_video_depth,
|
195 |
-
inputs=[input_video, max_len, target_fps, max_res, stitch_option, grayscale_option,
|
196 |
outputs=[processed_video, depth_vis_video, stitched_video],
|
197 |
)
|
198 |
|
|
|
12 |
from huggingface_hub import hf_hub_download
|
13 |
|
14 |
# Examples for the Gradio Demo.
|
15 |
+
# Each example now contains 8 parameters in the following order:
|
16 |
+
# [video_path, max_len, target_fps, max_res, stitch, grayscale, convert_from_color, blur]
|
17 |
examples = [
|
18 |
+
['assets/example_videos/davis_rollercoaster.mp4', -1, -1, 1280, True, True, True, 0.5],
|
19 |
+
['assets/example_videos/Tokyo-Walk_rgb.mp4', -1, -1, 1280, True, True, True, 0.5],
|
20 |
+
['assets/example_videos/4158877-uhd_3840_2160_30fps_rgb.mp4', -1, -1, 1280, True, True, True, 0.5],
|
21 |
+
['assets/example_videos/4511004-uhd_3840_2160_24fps_rgb.mp4', -1, -1, 1280, True, True, True, 0.5],
|
22 |
+
['assets/example_videos/1753029-hd_1920_1080_30fps.mp4', -1, -1, 1280, True, True, True, 0.5],
|
23 |
+
['assets/example_videos/davis_burnout.mp4', -1, -1, 1280, True, True, True, 0.5],
|
24 |
+
['assets/example_videos/example_5473765-l.mp4', -1, -1, 1280, True, True, True, 0.5],
|
25 |
+
['assets/example_videos/Istanbul-26920.mp4', -1, -1, 1280, True, True, True, 0.5],
|
26 |
+
['assets/example_videos/obj_1.mp4', -1, -1, 1280, True, True, True, 0.5],
|
27 |
+
['assets/example_videos/sheep_cut1.mp4', -1, -1, 1280, True, True, True, 0.5],
|
28 |
]
|
29 |
|
30 |
# Use GPU if available; otherwise, use CPU.
|
|
|
63 |
max_res: int = 1280,
|
64 |
stitch: bool = True,
|
65 |
grayscale: bool = True,
|
66 |
+
convert_from_color: bool = True,
|
67 |
+
blur: float = 0.5,
|
68 |
output_dir: str = './outputs',
|
69 |
input_size: int = 518,
|
|
|
70 |
):
|
71 |
# 1. Read input video frames for inference (downscaled to max_res).
|
72 |
frames, target_fps = read_video_frames(input_video, max_len, target_fps, max_res)
|
|
|
93 |
for i in range(min(len(full_frames), len(depths))):
|
94 |
rgb_full = full_frames[i] # Full-resolution RGB frame.
|
95 |
depth_frame = depths[i]
|
96 |
+
# Normalize the depth frame to the range [0, 255].
|
97 |
depth_norm = ((depth_frame - d_min) / (d_max - d_min) * 255).astype(np.uint8)
|
98 |
# Generate depth visualization:
|
99 |
if grayscale:
|
|
|
112 |
depth_vis = (cmap(depth_norm / 255.0)[..., :3] * 255).astype(np.uint8)
|
113 |
# Apply Gaussian blur if requested.
|
114 |
if blur > 0:
|
115 |
+
kernel_size = int(blur * 20) * 2 + 1 # Ensures an odd kernel size.
|
116 |
depth_vis = cv2.GaussianBlur(depth_vis, (kernel_size, kernel_size), 0)
|
117 |
# Resize the depth visualization to match the full-resolution RGB frame.
|
118 |
H_full, W_full = rgb_full.shape[:2]
|
119 |
depth_vis_resized = cv2.resize(depth_vis, (W_full, H_full))
|
120 |
+
# Concatenate the full-resolution RGB frame (left) and the resized depth visualization (right).
|
121 |
stitched = cv2.hconcat([rgb_full, depth_vis_resized])
|
122 |
stitched_frames.append(stitched)
|
123 |
stitched_frames = np.array(stitched_frames)
|
124 |
+
# Use only the first 20 characters of the base name for the output filename and append '_RGBD.mp4'
|
125 |
base_name = os.path.splitext(video_name)[0]
|
126 |
short_name = base_name[:20]
|
127 |
stitched_video_path = os.path.join(output_dir, short_name + '_RGBD.mp4')
|
|
|
174 |
max_res = gr.Slider(label="Max side resolution", minimum=480, maximum=1920, value=1280, step=1)
|
175 |
stitch_option = gr.Checkbox(label="Stitch RGB & Depth Videos", value=True)
|
176 |
grayscale_option = gr.Checkbox(label="Output Depth as Grayscale", value=True)
|
|
|
177 |
convert_from_color_option = gr.Checkbox(label="Convert Grayscale from Color", value=True)
|
178 |
+
blur_slider = gr.Slider(minimum=0, maximum=1, step=0.01, label="Depth Blur (can reduce edge artifacts)", value=0.5)
|
179 |
generate_btn = gr.Button("Generate")
|
180 |
with gr.Column(scale=2):
|
181 |
pass
|
182 |
|
183 |
gr.Examples(
|
184 |
examples=examples,
|
185 |
+
inputs=[input_video, max_len, target_fps, max_res, stitch_option, grayscale_option, convert_from_color_option, blur_slider],
|
186 |
outputs=[processed_video, depth_vis_video, stitched_video],
|
187 |
fn=infer_video_depth,
|
188 |
cache_examples=True,
|
|
|
191 |
|
192 |
generate_btn.click(
|
193 |
fn=infer_video_depth,
|
194 |
+
inputs=[input_video, max_len, target_fps, max_res, stitch_option, grayscale_option, convert_from_color_option, blur_slider],
|
195 |
outputs=[processed_video, depth_vis_video, stitched_video],
|
196 |
)
|
197 |
|