niye4 commited on
Commit
abea5f8
·
verified ·
1 Parent(s): 2d5b4e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -14
app.py CHANGED
@@ -3,21 +3,41 @@ import subprocess
3
  import gradio as gr
4
  from gradio_imageslider import ImageSlider
5
  from PIL import Image
6
- import glob
7
- import tempfile
8
 
9
  # Output folder
10
  OUTPUT_DIR = "output"
11
  os.makedirs(OUTPUT_DIR, exist_ok=True)
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  def process_video(video_file):
14
  """
15
- Run DepthAnythingV2 run_video.py CLI to process the uploaded MP4 video
16
- and return both a slider view (per-frame depth map) and the output video.
17
  """
18
  video_path = video_file.name
19
 
20
- # Call run_video.py using subprocess
21
  cmd = [
22
  "python", "run_video.py",
23
  "--encoder", "vitb",
@@ -28,26 +48,22 @@ def process_video(video_file):
28
  ]
29
  subprocess.run(cmd, check=True)
30
 
31
- # run_video.py output video filename: <input>_depth.mp4
32
  base_name = os.path.basename(video_path)
33
  output_video = os.path.join(OUTPUT_DIR, base_name.replace(".mp4", "_depth.mp4"))
34
 
35
- # Prepare slider images for per-frame preview
36
- # Assume run_video.py saves frames as PNG in output folder
37
- slider_images = []
38
- frame_files = sorted(glob.glob(os.path.join(OUTPUT_DIR, "*.png")))
39
- for f in frame_files:
40
- slider_images.append(Image.open(f))
41
 
42
  return slider_images, output_video
43
 
44
  # Gradio UI
45
  with gr.Blocks() as demo:
46
  gr.Markdown("# Depth Anything V2 - Video Demo")
47
- gr.Markdown("Upload an MP4 video, and the system will automatically generate a DepthMap video.")
48
 
49
  video_input = gr.File(label="Upload MP4", file_types=['.mp4'])
50
- depth_slider = ImageSlider(label="Depth Map Slider") # Slider view of frames
51
  video_output = gr.Video(label="DepthMap Video")
52
  submit = gr.Button("Render DepthMap")
53
 
 
3
  import gradio as gr
4
  from gradio_imageslider import ImageSlider
5
  from PIL import Image
6
+ import cv2
 
7
 
8
  # Output folder
9
  OUTPUT_DIR = "output"
10
  os.makedirs(OUTPUT_DIR, exist_ok=True)
11
 
12
+ def generate_slider_from_video(video_path, max_frames=30):
13
+ """
14
+ Extract up to max_frames evenly spaced frames from the video
15
+ to display in the ImageSlider.
16
+ """
17
+ cap = cv2.VideoCapture(video_path)
18
+ frames = []
19
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
20
+ step = max(1, total_frames // max_frames)
21
+ idx = 0
22
+ while True:
23
+ ret, frame = cap.read()
24
+ if not ret:
25
+ break
26
+ if idx % step == 0:
27
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
28
+ frames.append(Image.fromarray(frame_rgb))
29
+ idx += 1
30
+ cap.release()
31
+ return frames
32
+
33
  def process_video(video_file):
34
  """
35
+ Call run_video.py to process the uploaded video and return
36
+ both a slider view and the output depth video.
37
  """
38
  video_path = video_file.name
39
 
40
+ # Run DepthAnythingV2 CLI
41
  cmd = [
42
  "python", "run_video.py",
43
  "--encoder", "vitb",
 
48
  ]
49
  subprocess.run(cmd, check=True)
50
 
51
+ # Construct output video path (run_video.py convention)
52
  base_name = os.path.basename(video_path)
53
  output_video = os.path.join(OUTPUT_DIR, base_name.replace(".mp4", "_depth.mp4"))
54
 
55
+ # Generate slider images from output video
56
+ slider_images = generate_slider_from_video(output_video, max_frames=30)
 
 
 
 
57
 
58
  return slider_images, output_video
59
 
60
  # Gradio UI
61
  with gr.Blocks() as demo:
62
  gr.Markdown("# Depth Anything V2 - Video Demo")
63
+ gr.Markdown("Upload an MP4 video and get a DepthMap video automatically.")
64
 
65
  video_input = gr.File(label="Upload MP4", file_types=['.mp4'])
66
+ depth_slider = ImageSlider(label="Depth Map Slider") # per-frame preview
67
  video_output = gr.Video(label="DepthMap Video")
68
  submit = gr.Button("Render DepthMap")
69