multimodalart HF staff commited on
Commit
fc54c05
·
verified ·
1 Parent(s): a8fba25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -29
app.py CHANGED
@@ -170,31 +170,41 @@ def infer(image, width=1024, height=1024, overlap_width=18, num_inference_steps=
170
 
171
  yield background, cnet_image
172
 
173
- def create_zoom_in_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
 
199
  def create_video_from_images(image_list, fps=4):
200
  if not image_list:
@@ -216,12 +226,17 @@ def create_video_from_images(image_list, fps=4):
216
 
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
224
-
 
 
 
 
 
 
225
  for _ in progress.tqdm(range(num_iterations), desc="Generating frames"):
226
  # Generate new image
227
  for step_result in infer(current_image, width, height, overlap_width, num_inference_steps,
@@ -242,7 +257,7 @@ def loop_outpainting(image, width=1024, height=1024, overlap_width=18, num_infer
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
@@ -368,10 +383,10 @@ with gr.Blocks(css=css) as demo:
368
  visible=False
369
  )
370
  with gr.Row():
371
- num_iterations = gr.Slider(label="Number of iterations", minimum=2, maximum=24, step=1, value=18)
372
  fps = gr.Slider(label="fps", minimum=1, maximum=24, value=8)
373
  with gr.Row():
374
- num_interpolation_frames = gr.Slider(label="Interpolation frames", minimum=0, maximum=10, step=1, value=5)
375
 
376
  with gr.Column():
377
  result = ImageSlider(
 
170
 
171
  yield background, cnet_image
172
 
173
+ def create_zoom_animation(previous_frame, next_frame, steps):
174
+
175
+ # List to store all frames
176
+ interpolated_frames = []
177
+
178
+ for i in range(steps + 1):
179
+ t = i / steps # Normalized time between 0 and 1
180
+
181
+ # Compute zoom factor (from 1 to 2)
182
+ z = 1 + t # Zoom factor increases from 1 to 2
183
+
184
+ # Compute crop size
185
+ crop_size = int(1024 / z)
 
186
 
187
+ # Compute crop coordinates to center the crop
188
+ x0 = (1024 - crop_size) // 2
189
+ y0 = (1024 - crop_size) // 2
190
+ x1 = x0 + crop_size
191
+ y1 = y0 + crop_size
192
 
193
+ # Crop the previous_frame
194
+ cropped_prev = previous_frame.crop((x0, y0, x1, y1))
195
+ # Resize to 512x512
196
+ resized_prev = cropped_prev.resize((512, 512), Image.LANCZOS)
197
+
198
+ # For the last frame, use the next_frame resized to 512x512
199
+ if i == steps:
200
+ resized_frame = next_frame.resize((512, 512), Image.LANCZOS)
201
+ else:
202
+ resized_frame = resized_prev
203
+
204
+ # Append the resized frame to the list
205
+ interpolated_frames.append(resized_frame)
206
 
207
+ return interpolated_frames
208
 
209
  def create_video_from_images(image_list, fps=4):
210
  if not image_list:
 
226
 
227
  @spaces.GPU(duration=70)
228
  def loop_outpainting(image, width=1024, height=1024, overlap_width=18, num_inference_steps=8,
229
+ resize_option="custom", custom_resize_size=512, prompt_input=None,
230
+ alignment="Middle", num_iterations=18, fps=6, num_interpolation_frames=10,
231
  progress=gr.Progress()):
 
232
  current_image = image
233
+ if(current_image.width != 1024 or current_image.height != 1024):
234
+ for first_result in infer(current_image, 1024, 1024, overlap_width, num_inference_steps,
235
+ resize_option, 1024, prompt_input, alignment):
236
+ pass
237
+ current_image = first_result[1]
238
+
239
+ image_list = [current_image]
240
  for _ in progress.tqdm(range(num_iterations), desc="Generating frames"):
241
  # Generate new image
242
  for step_result in infer(current_image, width, height, overlap_width, num_inference_steps,
 
257
  for i in range(len(reverse_image_list) - 1):
258
  larger_frame = reverse_image_list[i]
259
  smaller_frame = reverse_image_list[i + 1]
260
+ interpolated_frames = create_zoom_animation(larger_frame, smaller_frame, num_interpolation_frames)
261
  final_frame_list.extend(interpolated_frames)
262
 
263
  # Add the last frame
 
383
  visible=False
384
  )
385
  with gr.Row():
386
+ num_iterations = gr.Slider(label="Number of iterations", minimum=2, maximum=24, step=1, value=6)
387
  fps = gr.Slider(label="fps", minimum=1, maximum=24, value=8)
388
  with gr.Row():
389
+ num_interpolation_frames = gr.Slider(label="Interpolation frames", minimum=0, maximum=10, step=1, value=10)
390
 
391
  with gr.Column():
392
  result = ImageSlider(