fffiloni commited on
Commit
af37bf6
β€’
1 Parent(s): 9b4ad86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -17
app.py CHANGED
@@ -50,6 +50,9 @@ def controlnet(i, prompt, control_task, seed_in, ddim_steps, scale):
50
  eta = 0.0
51
  low_threshold = 100
52
  high_threshold = 200
 
 
 
53
 
54
  if control_task == 'Canny':
55
  result = model.process_canny(np_img, prompt, a_prompt, n_prompt, num_samples,
@@ -57,14 +60,32 @@ def controlnet(i, prompt, control_task, seed_in, ddim_steps, scale):
57
  elif control_task == 'Depth':
58
  result = model.process_depth(np_img, prompt, a_prompt, n_prompt, num_samples,
59
  image_resolution, detect_resolution, ddim_steps, scale, seed_in, eta)
 
 
 
 
 
 
 
 
 
 
60
  elif control_task == 'Pose':
61
  result = model.process_pose(np_img, prompt, a_prompt, n_prompt, num_samples,
62
  image_resolution, detect_resolution, ddim_steps, scale, seed_in, eta)
 
 
 
 
 
 
63
 
64
  #print(result[0])
 
 
65
  im = Image.fromarray(result[1])
66
  im.save("your_file" + str(i) + ".jpeg")
67
- return "your_file" + str(i) + ".jpeg"
68
 
69
 
70
  def get_frames(video_in):
@@ -105,15 +126,24 @@ def get_frames(video_in):
105
  return frames, fps
106
 
107
 
108
- def create_video(frames, fps):
 
 
 
 
 
 
 
 
 
109
  print("building video result")
110
  clip = ImageSequenceClip(frames, fps=fps)
111
- clip.write_videofile("movie.mp4", fps=fps)
112
 
113
- return 'movie.mp4'
114
 
115
 
116
- def infer(prompt,video_in, control_task, seed_in, trim_value, ddim_steps, scale):
117
  print(f"""
118
  β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
119
  {prompt}
@@ -129,7 +159,8 @@ def infer(prompt,video_in, control_task, seed_in, trim_value, ddim_steps, scale)
129
  print("video is shorter than the cut value")
130
  n_frame = len(frames_list)
131
 
132
- # 2. prepare frames result array
 
133
  result_frames = []
134
  print("set stop frames to: " + str(n_frame))
135
 
@@ -140,14 +171,27 @@ def infer(prompt,video_in, control_task, seed_in, trim_value, ddim_steps, scale)
140
 
141
  # exporting the image
142
  #rgb_im.save(f"result_img-{i}.jpg")
143
- result_frames.append(controlnet_img)
 
144
  print("frame " + i + "/" + str(n_frame) + ": done;")
145
 
146
- final_vid = create_video(result_frames, fps)
 
 
 
 
 
 
 
 
 
147
  print("finished !")
148
 
149
- return final_vid, gr.Group.update(visible=True)
150
- #return controlnet_img
 
 
 
151
 
152
  title = """
153
  <div style="text-align: center; max-width: 700px; margin: 0 auto;">
@@ -199,6 +243,9 @@ with gr.Blocks(css='style.css') as demo:
199
  with gr.Column():
200
  video_inp = gr.Video(label="Video source", source="upload", type="filepath", elem_id="input-vid")
201
  video_out = gr.Video(label="ControlNet video result", elem_id="video-output")
 
 
 
202
  with gr.Group(elem_id="share-btn-container", visible=False) as share_group:
203
  community_icon = gr.HTML(community_icon_html)
204
  loading_icon = gr.HTML(loading_icon_html)
@@ -207,20 +254,27 @@ with gr.Blocks(css='style.css') as demo:
207
  #status = gr.Textbox()
208
 
209
  prompt = gr.Textbox(label="Prompt", placeholder="enter prompt", show_label=True, elem_id="prompt-in")
210
- control_task = gr.Dropdown(label="Control Task", choices=["Canny", "Depth", "Pose"], value="Pose", multiselect=False)
211
  with gr.Row():
212
- seed_inp = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, value=123456)
 
 
 
213
  trim_in = gr.Slider(label="Cut video at (s)", minimun=1, maximum=5, step=1, value=1)
214
- ddim_steps = gr.Slider(label='Steps',
 
 
215
  minimum=1,
216
  maximum=100,
217
  value=20,
218
  step=1)
219
- scale = gr.Slider(label='Guidance Scale',
220
  minimum=0.1,
221
  maximum=30.0,
222
  value=9.0,
223
  step=0.1)
 
 
 
224
 
225
  submit_btn = gr.Button("Generate ControlNet video")
226
 
@@ -229,13 +283,14 @@ with gr.Blocks(css='style.css') as demo:
229
  work with longer videos / skip the queue:
230
  """, elem_id="duplicate-container")
231
 
232
- inputs = [prompt,video_inp,control_task, seed_inp, trim_in, ddim_steps, scale]
233
- outputs = [video_out, share_group]
234
  #outputs = [status]
235
 
236
 
237
  gr.HTML(article)
238
-
 
239
  submit_btn.click(infer, inputs, outputs)
240
  share_button.click(None, [], [], _js=share_js)
241
 
 
50
  eta = 0.0
51
  low_threshold = 100
52
  high_threshold = 200
53
+ value_threshold = 0.1
54
+ distance_threshold = 0.1
55
+ bg_threshold = 0.4
56
 
57
  if control_task == 'Canny':
58
  result = model.process_canny(np_img, prompt, a_prompt, n_prompt, num_samples,
 
60
  elif control_task == 'Depth':
61
  result = model.process_depth(np_img, prompt, a_prompt, n_prompt, num_samples,
62
  image_resolution, detect_resolution, ddim_steps, scale, seed_in, eta)
63
+ elif control_task == 'Hed':
64
+ result = model.process_hed(np_img, prompt, a_prompt, n_prompt, num_samples,
65
+ image_resolution, detect_resolution, ddim_steps, scale, seed_in, eta)
66
+ elif control_task == 'Hough':
67
+ result = model.process_hough(np_img, prompt, a_prompt, n_prompt, num_samples,
68
+ image_resolution, detect_resolution, ddim_steps, scale, seed_in, eta, value_threshold,
69
+ distance_threshold)
70
+ elif control_task == 'Normal':
71
+ result = model.process_normal(np_img, prompt, a_prompt, n_prompt, num_samples,
72
+ image_resolution, detect_resolution, ddim_steps, scale, seed_in, eta, bg_threshold)
73
  elif control_task == 'Pose':
74
  result = model.process_pose(np_img, prompt, a_prompt, n_prompt, num_samples,
75
  image_resolution, detect_resolution, ddim_steps, scale, seed_in, eta)
76
+ elif control_task == 'Scribble':
77
+ result = model.process_scribble(np_img, prompt, a_prompt, n_prompt, num_samples,
78
+ image_resolution, ddim_steps, scale, seed_in, eta)
79
+ elif control_task == 'Seg':
80
+ result = model.process_seg(np_img, prompt, a_prompt, n_prompt, num_samples,
81
+ image_resolution, detect_resolution, ddim_steps, scale, seed_in, eta)
82
 
83
  #print(result[0])
84
+ processor_im = Image.fromarray(result[0])
85
+ processor_im.save("process_" + control_task + "_" + str(i) + ".jpeg")
86
  im = Image.fromarray(result[1])
87
  im.save("your_file" + str(i) + ".jpeg")
88
+ return "your_file" + str(i) + ".jpeg", "process_" + control_task + "_" + str(i) + ".jpeg"
89
 
90
 
91
  def get_frames(video_in):
 
126
  return frames, fps
127
 
128
 
129
+ def convert(gif):
130
+ if gif != None:
131
+ clip = VideoFileClip(gif.name)
132
+ clip.write_videofile("my_gif_video.mp4")
133
+ return "my_gif_video.mp4"
134
+ else:
135
+ pass
136
+
137
+
138
+ def create_video(frames, fps, type):
139
  print("building video result")
140
  clip = ImageSequenceClip(frames, fps=fps)
141
+ clip.write_videofile(type + "_result.mp4", fps=fps)
142
 
143
+ return type + "_result.mp4"
144
 
145
 
146
+ def infer(prompt,video_in, control_task, seed_in, trim_value, ddim_steps, scale, gif_import):
147
  print(f"""
148
  β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
149
  {prompt}
 
159
  print("video is shorter than the cut value")
160
  n_frame = len(frames_list)
161
 
162
+ # 2. prepare frames result arrays
163
+ processor_result_frames = []
164
  result_frames = []
165
  print("set stop frames to: " + str(n_frame))
166
 
 
171
 
172
  # exporting the image
173
  #rgb_im.save(f"result_img-{i}.jpg")
174
+ processor_result_frames.append(controlnet_img[1])
175
+ result_frames.append(controlnet_img[0])
176
  print("frame " + i + "/" + str(n_frame) + ": done;")
177
 
178
+ processor_vid = create_video(processor_result_frames, fps, "processor")
179
+ final_vid = create_video(result_frames, fps, "final")
180
+
181
+ files = [processor_vid, final_vid]
182
+ if gif_import != None:
183
+ final_gif = VideoFileClip(final_vid)
184
+ final_gif.write_gif("final_result.gif")
185
+ final_gif = "final_result.gif"
186
+
187
+ files.append(final_gif)
188
  print("finished !")
189
 
190
+ return final_vid, gr.Accordion.update(visible=True), gr.Video.update(value=processor_vid, visible=True), gr.File.update(value=files, visible=True), gr.Group.update(visible=True)
191
+
192
+
193
+ def clean():
194
+ return gr.Accordion.update(visible=False),gr.Video.update(value=None, visible=False), gr.Video.update(value=None), gr.File.update(value=None, visible=False), gr.Group.update(visible=False)
195
 
196
  title = """
197
  <div style="text-align: center; max-width: 700px; margin: 0 auto;">
 
243
  with gr.Column():
244
  video_inp = gr.Video(label="Video source", source="upload", type="filepath", elem_id="input-vid")
245
  video_out = gr.Video(label="ControlNet video result", elem_id="video-output")
246
+ with gr.Accordion("Detailed results", visible=False) as detailed_result:
247
+ prep_video_out = gr.Video(label="Preprocessor video result", visible=False, elem_id="prep-video-output")
248
+ files = gr.File(label="Files can be downloaded ;)", visible=False)
249
  with gr.Group(elem_id="share-btn-container", visible=False) as share_group:
250
  community_icon = gr.HTML(community_icon_html)
251
  loading_icon = gr.HTML(loading_icon_html)
 
254
  #status = gr.Textbox()
255
 
256
  prompt = gr.Textbox(label="Prompt", placeholder="enter prompt", show_label=True, elem_id="prompt-in")
 
257
  with gr.Row():
258
+ control_task = gr.Dropdown(label="Control Task", choices=["Canny", "Depth", "Hed", "Hough", "Normal", "Pose", "Scribble", "Seg"], value="Pose", multiselect=False, elem_id="controltask-in")
259
+ seed_inp = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, value=123456, elem_id="seed-in")
260
+ with gr.Row():
261
+
262
  trim_in = gr.Slider(label="Cut video at (s)", minimun=1, maximum=5, step=1, value=1)
263
+ with gr.Accordion("Advanced Options", open=False):
264
+
265
+ ddim_steps = gr.Slider(label='Steps',
266
  minimum=1,
267
  maximum=100,
268
  value=20,
269
  step=1)
270
+ scale = gr.Slider(label='Guidance Scale',
271
  minimum=0.1,
272
  maximum=30.0,
273
  value=9.0,
274
  step=0.1)
275
+
276
+ gif_import = gr.File(label="import a GIF instead", file_types=['.gif'])
277
+ gif_import.change(convert, gif_import, video_inp, queue=False)
278
 
279
  submit_btn = gr.Button("Generate ControlNet video")
280
 
 
283
  work with longer videos / skip the queue:
284
  """, elem_id="duplicate-container")
285
 
286
+ inputs = [prompt,video_inp,control_task, seed_inp, trim_in, ddim_steps, scale, gif_import]
287
+ outputs = [video_out, detailed_result, prep_video_out, files, share_group]
288
  #outputs = [status]
289
 
290
 
291
  gr.HTML(article)
292
+
293
+ submit_btn.click(clean, inputs=[], outputs=[detailed_result, prep_video_out, video_out, files, share_group], queue=False)
294
  submit_btn.click(infer, inputs, outputs)
295
  share_button.click(None, [], [], _js=share_js)
296