Fabrice-TIERCELIN commited on
Commit
3ca67e5
1 Parent(s): d01b75c

Allow the user to choose the video size

Browse files
Files changed (1) hide show
  1. app.py +27 -15
app.py CHANGED
@@ -45,7 +45,12 @@ def animate(
45
  height: int = 576
46
  ):
47
  start = time.time()
 
 
 
 
48
  output_folder = "outputs"
 
49
  if image.mode == "RGBA":
50
  image = image.convert("RGB")
51
 
@@ -98,7 +103,7 @@ def animate(
98
 
99
  return [
100
  # Display for video
101
- gr.update(value = video_path, visible=video_format != "gif"),
102
  # Display for gif
103
  gr.update(value = gif_path, visible = video_format == "gif"),
104
  # Download button
@@ -137,14 +142,14 @@ def animate_on_gpu(
137
 
138
 
139
  def resize_image(image, output_size=(1024, 576)):
140
- # Calculate aspect ratios
141
- target_aspect = output_size[0] / output_size[1] # Aspect ratio of the desired size
142
- image_aspect = image.width / image.height # Aspect ratio of the original image
143
-
144
  # Do not touch the image if the size is good
145
  if image.width == output_size[0] and image.height == output_size[1]:
146
  return image
147
 
 
 
 
 
148
  # Resize if the original image is larger
149
  if image_aspect > target_aspect:
150
  # Resize the image to match the target height, maintaining aspect ratio
@@ -181,7 +186,9 @@ def reset():
181
  3,
182
  "mp4",
183
  "webp",
184
- "auto"
 
 
185
  ]
186
 
187
  with gr.Blocks() as demo:
@@ -201,6 +208,8 @@ with gr.Blocks() as demo:
201
  with gr.Column():
202
  image = gr.Image(label="Upload your image", type="pil")
203
  with gr.Accordion("Advanced options", open=False):
 
 
204
  fps_id = gr.Slider(label="Frames per second", info="The length of your video in seconds will be 25/fps", value=25, minimum=5, maximum=30)
205
  motion_bucket_id = gr.Slider(label="Motion bucket id", info="Controls how much motion to add/remove from the image", value=127, minimum=1, maximum=255)
206
  noise_aug_strength = gr.Slider(label="Noise strength", info="The noise to add", value=0.1, minimum=0, maximum=1, step=0.1)
@@ -215,13 +224,12 @@ with gr.Blocks() as demo:
215
  reset_btn = gr.Button(value="🧹 Reinit page", variant="stop", elem_id="reset_button", visible = False)
216
 
217
  with gr.Column():
218
- video_output = gr.Video(label="Generated video", format="mp4", autoplay=True)
219
- gif_output = gr.Image(label="Generated video", format="gif", visible=False)
220
  download_button = gr.DownloadButton(label="💾 Download video", visible=False)
221
  information_msg = gr.HTML(visible=False)
222
  gallery = gr.Gallery(label="Generated frames", visible=False)
223
 
224
- image.upload(fn=resize_image, inputs=image, outputs=image, queue=False)
225
  generate_btn.click(fn=animate, inputs=[
226
  image,
227
  seed,
@@ -232,7 +240,9 @@ with gr.Blocks() as demo:
232
  decoding_t,
233
  video_format,
234
  frame_format,
235
- version
 
 
236
  ], outputs=[
237
  video_output,
238
  gif_output,
@@ -253,16 +263,18 @@ with gr.Blocks() as demo:
253
  decoding_t,
254
  video_format,
255
  frame_format,
256
- version
 
 
257
  ], queue = False, show_progress = False)
258
 
259
  gr.Examples(
260
  examples=[
261
- ["Examples/Fire.webp", 42, True, 127, 25, 0.1, 3, "mp4", "png", "auto"],
262
- ["Examples/Water.png", 42, True, 127, 25, 0.1, 3, "mp4", "png", "auto"],
263
- ["Examples/Town.jpeg", 42, True, 127, 25, 0.1, 3, "mp4", "png", "auto"]
264
  ],
265
- inputs=[image, seed, randomize_seed, motion_bucket_id, fps_id, noise_aug_strength, decoding_t, video_format, frame_format, version],
266
  outputs=[video_output, gif_output, download_button, gallery, seed, information_msg, reset_btn],
267
  fn=animate,
268
  run_on_click=True,
 
45
  height: int = 576
46
  ):
47
  start = time.time()
48
+
49
+ if image is None:
50
+ raise gr.Error("Please provide an image to animate.")
51
+
52
  output_folder = "outputs"
53
+ image = resize_image(image, output_size=(width, height))
54
  if image.mode == "RGBA":
55
  image = image.convert("RGB")
56
 
 
103
 
104
  return [
105
  # Display for video
106
+ gr.update(value = video_path, visible = video_format != "gif"),
107
  # Display for gif
108
  gr.update(value = gif_path, visible = video_format == "gif"),
109
  # Download button
 
142
 
143
 
144
  def resize_image(image, output_size=(1024, 576)):
 
 
 
 
145
  # Do not touch the image if the size is good
146
  if image.width == output_size[0] and image.height == output_size[1]:
147
  return image
148
 
149
+ # Calculate aspect ratios
150
+ target_aspect = output_size[0] / output_size[1] # Aspect ratio of the desired size
151
+ image_aspect = image.width / image.height # Aspect ratio of the original image
152
+
153
  # Resize if the original image is larger
154
  if image_aspect > target_aspect:
155
  # Resize the image to match the target height, maintaining aspect ratio
 
186
  3,
187
  "mp4",
188
  "webp",
189
+ "auto",
190
+ 1024,
191
+ 576
192
  ]
193
 
194
  with gr.Blocks() as demo:
 
208
  with gr.Column():
209
  image = gr.Image(label="Upload your image", type="pil")
210
  with gr.Accordion("Advanced options", open=False):
211
+ width = gr.Slider(label="Width", info="Width of the video", value=1024, minimum=256, maximum=1024, step=8)
212
+ height = gr.Slider(label="Height", info="Height of the video", value=576, minimum=256, maximum=576, step=8)
213
  fps_id = gr.Slider(label="Frames per second", info="The length of your video in seconds will be 25/fps", value=25, minimum=5, maximum=30)
214
  motion_bucket_id = gr.Slider(label="Motion bucket id", info="Controls how much motion to add/remove from the image", value=127, minimum=1, maximum=255)
215
  noise_aug_strength = gr.Slider(label="Noise strength", info="The noise to add", value=0.1, minimum=0, maximum=1, step=0.1)
 
224
  reset_btn = gr.Button(value="🧹 Reinit page", variant="stop", elem_id="reset_button", visible = False)
225
 
226
  with gr.Column():
227
+ video_output = gr.Video(label="Generated video", format="mp4", autoplay=True, show_download_button=False)
228
+ gif_output = gr.Image(label="Generated video", format="gif", show_download_button=False, visible=False)
229
  download_button = gr.DownloadButton(label="💾 Download video", visible=False)
230
  information_msg = gr.HTML(visible=False)
231
  gallery = gr.Gallery(label="Generated frames", visible=False)
232
 
 
233
  generate_btn.click(fn=animate, inputs=[
234
  image,
235
  seed,
 
240
  decoding_t,
241
  video_format,
242
  frame_format,
243
+ version,
244
+ width,
245
+ height
246
  ], outputs=[
247
  video_output,
248
  gif_output,
 
263
  decoding_t,
264
  video_format,
265
  frame_format,
266
+ version,
267
+ width,
268
+ height
269
  ], queue = False, show_progress = False)
270
 
271
  gr.Examples(
272
  examples=[
273
+ ["Examples/Fire.webp", 42, True, 127, 25, 0.1, 3, "mp4", "png", "auto", 1024, 576],
274
+ ["Examples/Water.png", 42, True, 127, 25, 0.1, 3, "mp4", "png", "auto", 1024, 576],
275
+ ["Examples/Town.jpeg", 42, True, 127, 25, 0.1, 3, "mp4", "png", "auto", 1024, 576]
276
  ],
277
+ inputs=[image, seed, randomize_seed, motion_bucket_id, fps_id, noise_aug_strength, decoding_t, video_format, frame_format, version, width, height],
278
  outputs=[video_output, gif_output, download_button, gallery, seed, information_msg, reset_btn],
279
  fn=animate,
280
  run_on_click=True,