Update app.py

#3
by linoyts HF Staff - opened
Files changed (1) hide show
  1. app.py +46 -1
app.py CHANGED
@@ -118,6 +118,49 @@ def prepare_video_and_mask_FLF2V(first_img: Image.Image, last_img: Image.Image,
118
  mask = [mask_black, *[mask_white] * (num_frames - 2), mask_black]
119
  return frames, mask
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  def prepare_video_and_mask_Random2V(images: List[Image.Image], frame_indices: List[int], height: int, width: int, num_frames: int):
122
  images = [img.resize((width, height)) for img in images]
123
  # Ideally, this should be 127.5 to match original code, but they perform computation on numpy arrays
@@ -205,10 +248,12 @@ def generate_video(gallery_images, mode, prompt, height, width,
205
  frames, mask = prepare_video_and_mask_Ref2V(height=target_h, width=target_w, num_frames=num_frames)
206
  reference_images = gallery_images
207
  else: # mode == "Random2V"
 
 
208
 
209
  frames, mask = prepare_video_and_mask_Random2V(
210
  images=gallery_images,
211
- frame_indices=[0,20,40], # todo - generalize
212
  height=target_h,
213
  width=target_w,
214
  num_frames=num_frames
 
118
  mask = [mask_black, *[mask_white] * (num_frames - 2), mask_black]
119
  return frames, mask
120
 
121
+ def calculate_random2v_frame_indices(num_images: int, num_frames: int) -> List[int]:
122
+ """
123
+ Calculate evenly spaced frame indices for Random2V mode.
124
+
125
+ Args:
126
+ num_images (int): Number of input images
127
+ num_frames (int): Total number of frames in the video
128
+
129
+ Returns:
130
+ List[int]: Frame indices where images should be placed
131
+ """
132
+ if num_images <= 0:
133
+ return []
134
+
135
+ if num_images == 1:
136
+ # Single image goes in the middle
137
+ return [num_frames // 2]
138
+
139
+ if num_images >= num_frames:
140
+ # More images than frames, use every frame
141
+ return list(range(num_frames))
142
+
143
+ # Calculate evenly spaced indices
144
+ # We want to distribute images across the full duration
145
+ indices = []
146
+ step = (num_frames - 1) / (num_images - 1)
147
+
148
+ for i in range(num_images):
149
+ frame_idx = int(round(i * step))
150
+ # Ensure we don't exceed num_frames - 1
151
+ frame_idx = min(frame_idx, num_frames - 1)
152
+ indices.append(frame_idx)
153
+
154
+ # Remove duplicates while preserving order
155
+ seen = set()
156
+ unique_indices = []
157
+ for idx in indices:
158
+ if idx not in seen:
159
+ seen.add(idx)
160
+ unique_indices.append(idx)
161
+
162
+ return unique_indices
163
+
164
  def prepare_video_and_mask_Random2V(images: List[Image.Image], frame_indices: List[int], height: int, width: int, num_frames: int):
165
  images = [img.resize((width, height)) for img in images]
166
  # Ideally, this should be 127.5 to match original code, but they perform computation on numpy arrays
 
248
  frames, mask = prepare_video_and_mask_Ref2V(height=target_h, width=target_w, num_frames=num_frames)
249
  reference_images = gallery_images
250
  else: # mode == "Random2V"
251
+ # Calculate dynamic frame indices based on number of images and frames
252
+ frame_indices = calculate_random2v_frame_indices(len(gallery_images), num_frames)
253
 
254
  frames, mask = prepare_video_and_mask_Random2V(
255
  images=gallery_images,
256
+ frame_indices=frame_indices,
257
  height=target_h,
258
  width=target_w,
259
  num_frames=num_frames