ghostsInTheMachine commited on
Commit
3e8f9ec
1 Parent(s): 7d32b39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -16
app.py CHANGED
@@ -6,7 +6,7 @@ import shutil
6
  import time
7
  import ffmpeg
8
  import numpy as np
9
- from PIL import Image
10
  import moviepy.editor as mp
11
  from infer import lotus, load_models
12
  import logging
@@ -23,33 +23,53 @@ task_name = 'depth'
23
  pipe_g, pipe_d = load_models(task_name, device)
24
 
25
  # Preprocess the video to adjust resolution and frame rate
26
- def preprocess_video(video_path, target_fps=24, max_resolution=(512, 512)):
27
- """Preprocess the video to resize and adjust its frame rate."""
28
  video = mp.VideoFileClip(video_path)
29
 
30
- # Resize video if it's larger than the target resolution
31
- if video.size[0] > max_resolution[0] or video.size[1] > max_resolution[1]:
32
- video = video.resize(height=max_resolution[1])
33
-
34
  # Adjust FPS if target_fps is specified
35
  if target_fps > 0:
36
  video = video.set_fps(target_fps)
37
 
38
  return video
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  # Process a single frame through the depth model
41
  def process_frame(frame, seed=0, target_size=(512, 512)):
42
  """Process a single frame and return depth map."""
43
  try:
44
  torch.cuda.empty_cache() # Clear GPU cache
45
 
46
- # Resize frame to the target size
47
- image = Image.fromarray(frame).convert('RGB').resize(target_size, Image.BILINEAR)
 
 
 
48
 
49
  # Run inference
50
- depth_map = lotus(image, 'depth', seed, device, pipe_g, pipe_d)
 
 
 
 
 
 
 
 
51
 
52
- return depth_map
53
 
54
  except Exception as e:
55
  logger.error(f"Error processing frame: {e}")
@@ -64,13 +84,13 @@ def process_video(video_path, fps=0, seed=0):
64
  start_time = time.time()
65
 
66
  # Preprocess the video
67
- video = preprocess_video(video_path, target_fps=fps, max_resolution=(512, 512))
68
 
69
  # Use original video FPS if not specified
70
  if fps == 0:
71
  fps = video.fps
72
 
73
- frames = list(video.iter_frames(fps=video.fps))
74
  total_frames = len(frames)
75
 
76
  logger.info(f"Processing {total_frames} frames at {fps} FPS...")
@@ -86,7 +106,7 @@ def process_video(video_path, fps=0, seed=0):
86
  if depth_map is not None:
87
  # Save frame
88
  frame_path = os.path.join(frames_dir, f"frame_{i:06d}.png")
89
- depth_map.save(frame_path)
90
 
91
  # Update live preview every 10% progress
92
  if i % max(1, total_frames // 10) == 0:
@@ -109,10 +129,11 @@ def process_video(video_path, fps=0, seed=0):
109
 
110
  try:
111
  # FFmpeg settings for high-quality MP4
 
112
  (
113
  ffmpeg
114
- .input(os.path.join(frames_dir, 'frame_%06d.png'), pattern_type='sequence', framerate=fps)
115
- .output(output_video_path, vcodec='libx264', pix_fmt='yuv420p', crf=17)
116
  .run(overwrite_output=True, quiet=True)
117
  )
118
  logger.info("MP4 video created successfully!")
 
6
  import time
7
  import ffmpeg
8
  import numpy as np
9
+ from PIL import Image, ImageOps
10
  import moviepy.editor as mp
11
  from infer import lotus, load_models
12
  import logging
 
23
  pipe_g, pipe_d = load_models(task_name, device)
24
 
25
  # Preprocess the video to adjust resolution and frame rate
26
+ def preprocess_video(video_path, target_fps=24, max_resolution=None):
27
+ """Preprocess the video to adjust its frame rate."""
28
  video = mp.VideoFileClip(video_path)
29
 
 
 
 
 
30
  # Adjust FPS if target_fps is specified
31
  if target_fps > 0:
32
  video = video.set_fps(target_fps)
33
 
34
  return video
35
 
36
+ # Resize image while preserving aspect ratio and adding padding
37
+ def resize_and_pad(image, target_size):
38
+ """Resize and pad an image to the target size while preserving aspect ratio."""
39
+ # Calculate the new size preserving aspect ratio
40
+ image.thumbnail(target_size, Image.ANTIALIAS)
41
+
42
+ # Create a new image with the target size and black background
43
+ new_image = Image.new("RGB", target_size)
44
+ new_image.paste(
45
+ image, ((target_size[0] - image.width) // 2, (target_size[1] - image.height) // 2)
46
+ )
47
+ return new_image
48
+
49
  # Process a single frame through the depth model
50
  def process_frame(frame, seed=0, target_size=(512, 512)):
51
  """Process a single frame and return depth map."""
52
  try:
53
  torch.cuda.empty_cache() # Clear GPU cache
54
 
55
+ # Convert frame to PIL Image
56
+ image = Image.fromarray(frame).convert('RGB')
57
+
58
+ # Resize and pad image
59
+ input_image = resize_and_pad(image, target_size)
60
 
61
  # Run inference
62
+ depth_map = lotus(input_image, 'depth', seed, device, pipe_g, pipe_d)
63
+
64
+ # Crop the output depth map back to original image size
65
+ width, height = image.size
66
+ left = (target_size[0] - width) // 2
67
+ top = (target_size[1] - height) // 2
68
+ right = left + width
69
+ bottom = top + height
70
+ depth_map_cropped = depth_map.crop((left, top, right, bottom))
71
 
72
+ return depth_map_cropped
73
 
74
  except Exception as e:
75
  logger.error(f"Error processing frame: {e}")
 
84
  start_time = time.time()
85
 
86
  # Preprocess the video
87
+ video = preprocess_video(video_path, target_fps=fps)
88
 
89
  # Use original video FPS if not specified
90
  if fps == 0:
91
  fps = video.fps
92
 
93
+ frames = list(video.iter_frames())
94
  total_frames = len(frames)
95
 
96
  logger.info(f"Processing {total_frames} frames at {fps} FPS...")
 
106
  if depth_map is not None:
107
  # Save frame
108
  frame_path = os.path.join(frames_dir, f"frame_{i:06d}.png")
109
+ depth_map.save(frame_path, format='PNG', compress_level=0)
110
 
111
  # Update live preview every 10% progress
112
  if i % max(1, total_frames // 10) == 0:
 
129
 
130
  try:
131
  # FFmpeg settings for high-quality MP4
132
+ input_pattern = os.path.join(frames_dir, 'frame_%06d.png')
133
  (
134
  ffmpeg
135
+ .input(input_pattern, pattern_type='sequence', framerate=fps)
136
+ .output(output_video_path, vcodec='libx264', pix_fmt='yuv420p', crf=17, preset='slow')
137
  .run(overwrite_output=True, quiet=True)
138
  )
139
  logger.info("MP4 video created successfully!")