rinong commited on
Commit
9cdf7e4
1 Parent(s): abcc9dc

Videos now loop.

Browse files
Files changed (2) hide show
  1. app.py +28 -12
  2. generate_videos.py +1 -1
app.py CHANGED
@@ -144,22 +144,37 @@ class ImageEditor(object):
144
 
145
  return inverted_latent
146
 
147
- def get_generators_for_styles(self, output_styles):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  return [self.generators[style] for style in output_styles]
149
 
150
  def edit_image(self, input, output_styles):
151
- return self.predict(input, output_styles, False, False, None)
152
 
153
- def edit_video(self, input, output_styles, with_editing, video_format):
154
- return self.predict(input, output_styles, True, with_editing, video_format)
155
 
156
  def predict(
157
  self,
158
- input, # Input image path
159
- output_styles, # Which output style do you want to use?
160
- generate_video, # Generate a video instead of an output image
161
- with_editing, # Apply latent space editing to the generated video
162
- video_format # Choose gif to display in browser, mp4 for higher-quality downloadable video
 
163
  ):
164
 
165
  # @title Align image
@@ -167,7 +182,7 @@ class ImageEditor(object):
167
  out_path = out_dir / "out.jpg"
168
 
169
  inverted_latent = self.invert_image(input)
170
- generators = self.get_generators_for_styles(output_styles)
171
 
172
  if not generate_video:
173
  with torch.no_grad():
@@ -290,14 +305,15 @@ with blocks:
290
  with gr.Column():
291
  with gr.Row():
292
  vid_button = gr.Button("Generate Video")
293
- edit_choice = gr.inputs.Checkbox(default=False, label="With latent space editing?", optional=False)
 
294
  vid_format_choice = gr.inputs.Radio(choices=["gif", "mp4"], type="value", default='mp4', label="Video Format")
295
 
296
  with gr.Column():
297
  vid_output = gr.outputs.Video(label="Output Video")
298
 
299
  img_button.click(fn=editor.edit_image, inputs=[input_img, style_choice], outputs=img_output)
300
- vid_button.click(fn=editor.edit_video, inputs=[input_img, style_choice, edit_choice, vid_format_choice], outputs=vid_output)
301
 
302
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2108.00946' target='_blank'>StyleGAN-NADA: CLIP-Guided Domain Adaptation of Image Generators</a> | <a href='https://stylegan-nada.github.io/' target='_blank'>Project Page</a> | <a href='https://github.com/rinongal/StyleGAN-nada' target='_blank'>Code</a></p> <center><img src='https://visitor-badge.glitch.me/badge?page_id=rinong_sgnada' alt='visitor badge'></center>"
303
  gr.Markdown(article)
 
144
 
145
  return inverted_latent
146
 
147
+ def get_generators_for_styles(self, output_styles, loop_styles=False):
148
+
149
+ # if style_string:
150
+ # styles = style_string.split(",")
151
+ # for style in styles:
152
+ # if style not in self.model_list:
153
+ # raise ValueError(f"Encountered style '{style}' in the input style list which is not an available option.")
154
+ # else:
155
+ # styles = style_checkbox_list
156
+
157
+ if "base" in output_styles: # always start with base if chosen
158
+ output_styles.insert(0, output_styles.pop(output_styles.index("base")))
159
+ if loop_styles:
160
+ output_styles.append(output_styles[0])
161
+
162
  return [self.generators[style] for style in output_styles]
163
 
164
  def edit_image(self, input, output_styles):
165
+ return self.predict(input, output_styles)
166
 
167
+ def edit_video(self, input, output_styles, with_editing, video_format, loop_styles):
168
+ return self.predict(input, output_styles, True, with_editing, video_format, loop_styles)
169
 
170
  def predict(
171
  self,
172
+ input, # Input image path
173
+ output_styles, # Style checkbox options.
174
+ generate_video = False, # Generate a video instead of an output image
175
+ with_editing = False, # Apply latent space editing to the generated video
176
+ video_format = "mp4", # Choose gif to display in browser, mp4 for higher-quality downloadable video
177
+ loop_styles = False, # Loop back to the initial style
178
  ):
179
 
180
  # @title Align image
 
182
  out_path = out_dir / "out.jpg"
183
 
184
  inverted_latent = self.invert_image(input)
185
+ generators = self.get_generators_for_styles(output_styles, loop_styles)
186
 
187
  if not generate_video:
188
  with torch.no_grad():
 
305
  with gr.Column():
306
  with gr.Row():
307
  vid_button = gr.Button("Generate Video")
308
+ loop_styles = gr.inputs.Checkbox(default=True, label="Loop video back to the initial style?")
309
+ edit_choice = gr.inputs.Checkbox(default=False, label="With latent space editing?")
310
  vid_format_choice = gr.inputs.Radio(choices=["gif", "mp4"], type="value", default='mp4', label="Video Format")
311
 
312
  with gr.Column():
313
  vid_output = gr.outputs.Video(label="Output Video")
314
 
315
  img_button.click(fn=editor.edit_image, inputs=[input_img, style_choice], outputs=img_output)
316
+ vid_button.click(fn=editor.edit_video, inputs=[input_img, style_choice, edit_choice, vid_format_choice, loop_styles], outputs=vid_output)
317
 
318
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2108.00946' target='_blank'>StyleGAN-NADA: CLIP-Guided Domain Adaptation of Image Generators</a> | <a href='https://stylegan-nada.github.io/' target='_blank'>Project Page</a> | <a href='https://github.com/rinongal/StyleGAN-nada' target='_blank'>Code</a></p> <center><img src='https://visitor-badge.glitch.me/badge?page_id=rinong_sgnada' alt='visitor badge'></center>"
319
  gr.Markdown(article)
generate_videos.py CHANGED
@@ -151,7 +151,7 @@ def video_from_interpolations(fps, output_dir):
151
  "-r", f"{fps}",
152
  "-i", f"{output_dir}/%03d.jpg",
153
  "-c:v", "libx264",
154
- "-vf", f"fps={fps}",
155
  "-pix_fmt", "yuv420p",
156
  f"{output_dir}/out.mp4"]
157
 
 
151
  "-r", f"{fps}",
152
  "-i", f"{output_dir}/%03d.jpg",
153
  "-c:v", "libx264",
154
+ "-vf", f'"fps={fps}, loop=loop=-1"',
155
  "-pix_fmt", "yuv420p",
156
  f"{output_dir}/out.mp4"]
157