multimodalart HF staff commited on
Commit
b828439
1 Parent(s): 11f295f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -27
app.py CHANGED
@@ -170,33 +170,29 @@ def infer(image, width=1024, height=1024, overlap_width=18, num_inference_steps=
170
 
171
  yield background, cnet_image
172
 
173
- def interpolate_frames(frame1, frame2, num_intermediate_frames):
174
  """
175
- Interpolate between two frames by gradually zooming out from frame2 to frame1.
176
  """
177
- frame1 = np.array(frame1)
178
- frame2 = np.array(frame2)
179
- h, w = frame1.shape[:2]
180
  frames = []
181
 
182
  for i in range(num_intermediate_frames + 2):
183
  progress = i / (num_intermediate_frames + 1)
184
- # Calculate the size of the inner frame
185
- inner_h = int(h * (1 - progress))
186
- inner_w = int(w * (1 - progress))
187
 
188
- # Crop the center of frame2
189
- start_y = (h - inner_h) // 2
190
- start_x = (w - inner_w) // 2
191
- cropped = frame2[start_y:start_y+inner_h, start_x:start_x+inner_w]
192
 
193
- # Resize the cropped image to full size
194
- interpolated = Image.fromarray(cropped).resize((w, h), Image.LANCZOS)
195
- interpolated = np.array(interpolated)
196
-
197
- # Blend with frame1
198
- blended = (1 - progress) * frame1 + progress * interpolated
199
- frames.append(Image.fromarray(blended.astype(np.uint8)))
200
 
201
  return frames
202
 
@@ -221,7 +217,7 @@ def create_video_from_images(image_list, fps=4):
221
  @spaces.GPU(duration=70)
222
  def loop_outpainting(image, width=1024, height=1024, overlap_width=18, num_inference_steps=8,
223
  resize_option="custom", custom_resize_size=768, prompt_input=None,
224
- alignment="Middle", num_iterations=8, fps=6, num_interpolation_frames=5,
225
  progress=gr.Progress()):
226
  image_list = [image]
227
  current_image = image
@@ -233,18 +229,29 @@ def loop_outpainting(image, width=1024, height=1024, overlap_width=18, num_infer
233
  pass # Process all steps
234
 
235
  new_image = step_result[1] # Get the final image from the last step
236
-
237
- # Interpolate between current_image and new_image
238
- interpolated_frames = interpolate_frames(current_image, new_image, num_interpolation_frames)
239
- image_list.extend(interpolated_frames)
240
 
241
  # Use new image as input for next iteration
242
  current_image = new_image
243
 
244
- # Create video from image list
245
- video_path = create_video_from_images(image_list, fps)
246
- return video_path
 
 
 
 
 
 
 
 
 
 
247
 
 
 
 
 
248
  loop_outpainting.zerogpu = True
249
 
250
  def clear_result():
 
170
 
171
  yield background, cnet_image
172
 
173
+ def interpolate_frames(larger_frame, smaller_frame, num_intermediate_frames):
174
  """
175
+ Create intermediate frames for a smooth zoom-in effect from the larger frame to the smaller frame.
176
  """
177
+ larger_frame = np.array(larger_frame)
178
+ smaller_frame = np.array(smaller_frame)
179
+ h, w = larger_frame.shape[:2]
180
  frames = []
181
 
182
  for i in range(num_intermediate_frames + 2):
183
  progress = i / (num_intermediate_frames + 1)
184
+ # Calculate the size of the frame to crop from the larger image
185
+ crop_h = int(h * (1 - progress) + smaller_frame.shape[0] * progress)
186
+ crop_w = int(w * (1 - progress) + smaller_frame.shape[1] * progress)
187
 
188
+ # Crop the center of the larger frame
189
+ start_y = (h - crop_h) // 2
190
+ start_x = (w - crop_w) // 2
191
+ cropped = larger_frame[start_y:start_y+crop_h, start_x:start_x+crop_w]
192
 
193
+ # Resize the cropped image to match the smaller frame's size
194
+ interpolated = Image.fromarray(cropped).resize(smaller_frame.shape[:2][::-1], Image.LANCZOS)
195
+ frames.append(interpolated)
 
 
 
 
196
 
197
  return frames
198
 
 
217
  @spaces.GPU(duration=70)
218
  def loop_outpainting(image, width=1024, height=1024, overlap_width=18, num_inference_steps=8,
219
  resize_option="custom", custom_resize_size=768, prompt_input=None,
220
+ alignment="Middle", num_iterations=18, fps=6, num_interpolation_frames=5,
221
  progress=gr.Progress()):
222
  image_list = [image]
223
  current_image = image
 
229
  pass # Process all steps
230
 
231
  new_image = step_result[1] # Get the final image from the last step
232
+ image_list.append(new_image)
 
 
 
233
 
234
  # Use new image as input for next iteration
235
  current_image = new_image
236
 
237
+ # Reverse the image list to create a zoom-in effect
238
+ reverse_image_list = image_list[::-1]
239
+
240
+ # Create interpolated frames
241
+ final_frame_list = []
242
+ for i in range(len(reverse_image_list) - 1):
243
+ larger_frame = reverse_image_list[i]
244
+ smaller_frame = reverse_image_list[i + 1]
245
+ interpolated_frames = create_zoom_in_frames(larger_frame, smaller_frame, num_interpolation_frames)
246
+ final_frame_list.extend(interpolated_frames)
247
+
248
+ # Add the last frame
249
+ final_frame_list.append(reverse_image_list[-1])
250
 
251
+ # Create video from the final frame list
252
+ video_path = create_video_from_images(final_frame_list, fps)
253
+ return video_path
254
+
255
  loop_outpainting.zerogpu = True
256
 
257
  def clear_result():