SingleZombie commited on
Commit
ef5f302
1 Parent(s): e8fbf78
Files changed (2) hide show
  1. app.py +11 -73
  2. requirements.txt +1 -1
app.py CHANGED
@@ -38,6 +38,7 @@ from annotator.midas import MidasDetector
38
  import huggingface_hub
39
  import shutil
40
  import os
 
41
 
42
  huggingface_hub.hf_hub_download('SingleZombie/FRESCO',
43
  'boxer-punching-towards-camera.mp4',
@@ -170,7 +171,7 @@ def cfg_to_input(filename):
170
  1.0, cfg['controlnet_type'], 50, 100,
171
  cfg['num_inference_steps'], 7.5, a_prompt, n_prompt,
172
  frame_count, cfg['batch_size'], cfg['mininterv'], cfg['maxinterv'],
173
- use_constraints, True, True, 4,
174
  1, 1, 1, 1
175
  ]
176
  return args
@@ -232,21 +233,17 @@ class GlobalState:
232
  param.requires_grad = False
233
 
234
 
235
- @torch.no_grad()
236
- def process(*args):
237
- keypath = process1(*args)
238
- fullpath = process2(*args)
239
- return keypath, fullpath
240
 
241
 
242
  @torch.no_grad()
 
243
  def process1(input_path, prompt, sd_model, seed, image_resolution, control_strength,
244
  x0_strength, control_type, low_threshold, high_threshold,
245
  ddpm_steps, scale, a_prompt, n_prompt,
246
  frame_count, batch_size, mininterv, maxinterv,
247
  use_constraints, bg_smooth, use_poisson, max_process,
248
  b1, b2, s1, s2):
249
- global global_state
250
  global_state.update_controlnet_model(control_type)
251
  global_state.update_sd_model(sd_model)
252
  apply_freeu(global_state.pipe, b1=b1, b2=b2, s1=s1, s2=s2)
@@ -392,7 +389,7 @@ def process1(input_path, prompt, sd_model, seed, image_resolution, control_stren
392
  gc.collect()
393
  if device == 'cuda':
394
  torch.cuda.empty_cache()
395
- break
396
 
397
  writer = imageio.get_writer(os.path.join(save_path, 'key.mp4'), fps=fps)
398
  file_list = sorted(os.listdir(os.path.join(save_path, 'keys')))
@@ -404,43 +401,9 @@ def process1(input_path, prompt, sd_model, seed, image_resolution, control_stren
404
  writer.append_data(curImg)
405
  writer.close()
406
 
407
- global_state.processing_state = ProcessingState.KEY_IMGS
408
  return os.path.join(save_path, 'key.mp4')
409
 
410
 
411
- @torch.no_grad()
412
- def process2(input_path, prompt, sd_model, seed, image_resolution, control_strength,
413
- x0_strength, control_type, low_threshold, high_threshold,
414
- ddpm_steps, scale, a_prompt, n_prompt,
415
- frame_count, batch_size, mininterv, maxinterv,
416
- use_constraints, bg_smooth, use_poisson, max_process,
417
- b1, b2, s1, s2):
418
-
419
- global global_state
420
- if global_state.processing_state != ProcessingState.KEY_IMGS:
421
- raise gr.Error('Please generate key images before propagation')
422
-
423
- # reset blend dir
424
- filename = os.path.splitext(os.path.basename(input_path))[0]
425
- blend_dir = os.path.join('output', filename)
426
- os.makedirs(blend_dir, exist_ok=True)
427
-
428
- video_cap = cv2.VideoCapture(input_path)
429
- fps = int(video_cap.get(cv2.CAP_PROP_FPS))
430
- o_video = os.path.join(blend_dir, 'blend.mp4')
431
- key_ind = io.StringIO()
432
- for k in global_state.keys:
433
- print('%d' % (k), end=' ', file=key_ind)
434
- ps = '-ps' if use_poisson else ''
435
- cmd = (
436
- f'python video_blend.py {blend_dir} --key keys '
437
- f'--key_ind {key_ind.getvalue()} --output {o_video} --fps {fps} '
438
- f'--n_proc {max_process} {ps}')
439
- print(cmd)
440
- os.system(cmd)
441
- return o_video
442
-
443
-
444
  config_dir = 'config'
445
  filenames = os.listdir(config_dir)
446
  config_list = []
@@ -448,7 +411,6 @@ for filename in filenames:
448
  if filename.endswith('yaml'):
449
  config_list.append(f'{config_dir}/{filename}')
450
 
451
- global_state = GlobalState()
452
  block = gr.Blocks().queue()
453
  with block:
454
  with gr.Row():
@@ -456,7 +418,7 @@ with block:
456
  with gr.Row():
457
  with gr.Column():
458
  input_path = gr.Video(label='Input Video',
459
- source='upload',
460
  format='mp4',
461
  visible=True)
462
  prompt = gr.Textbox(label='Prompt')
@@ -472,10 +434,7 @@ with block:
472
  step=1,
473
  value=0,
474
  randomize=True)
475
- run_button = gr.Button(value='Run All')
476
- with gr.Row():
477
- run_button1 = gr.Button(value='Run Key Frames')
478
- run_button2 = gr.Button(value='Run Propagation (Ebsynth)')
479
  with gr.Accordion('Advanced options for single frame processing',
480
  open=False):
481
  image_resolution = gr.Slider(label='Frame resolution',
@@ -595,20 +554,6 @@ with block:
595
  value=True,
596
  info='Select to smooth background')
597
 
598
- with gr.Accordion(
599
- 'Advanced options for the full video translation',
600
- open=False):
601
- use_poisson = gr.Checkbox(
602
- label='Gradient blending',
603
- value=True,
604
- info=('Blend the output video in gradient, to reduce'
605
- ' ghosting artifacts (but may increase flickers)'))
606
- max_process = gr.Slider(label='Number of parallel processes',
607
- minimum=1,
608
- maximum=16,
609
- value=4,
610
- step=1)
611
-
612
  with gr.Accordion('Example configs', open=True):
613
 
614
  example_list = [cfg_to_input(x) for x in config_list]
@@ -618,7 +563,7 @@ with block:
618
  x0_strength, control_type, low_threshold, high_threshold,
619
  ddpm_steps, scale, a_prompt, n_prompt,
620
  frame_count, batch_size, mininterv, maxinterv,
621
- use_constraints[0], bg_smooth, use_poisson, max_process,
622
  b1, b2, s1, s2
623
  ]
624
 
@@ -631,9 +576,6 @@ with block:
631
  result_keyframe = gr.Video(label='Output key frame video',
632
  format='mp4',
633
  interactive=False)
634
- result_video = gr.Video(label='Output full video',
635
- format='mp4',
636
- interactive=False)
637
 
638
  def input_changed(path):
639
  if path is None:
@@ -650,13 +592,13 @@ with block:
650
  max_interv_c = min(20, frame_count)
651
  min_interv_r = frame_count
652
  max_interv_r = frame_count
653
- return (gr.Slider.update(minimum=min_interv_l,
654
  value=min_interv_c,
655
  maximum=min_interv_r),
656
- gr.Slider.update(minimum=max_interv_l,
657
  value=max_interv_c,
658
  maximum=max_interv_r),
659
- gr.Slider.update(minimum=8,
660
  value=frame_count,
661
  maximum=frame_count),
662
  )
@@ -666,10 +608,6 @@ with block:
666
  input_path.upload(input_changed, input_path, [
667
  mininterv, maxinterv, frame_count])
668
 
669
- run_button.click(fn=process,
670
- inputs=ips,
671
- outputs=[result_keyframe, result_video])
672
  run_button1.click(fn=process1, inputs=ips, outputs=[result_keyframe])
673
- run_button2.click(fn=process2, inputs=ips, outputs=[result_video])
674
 
675
  block.launch()
 
38
  import huggingface_hub
39
  import shutil
40
  import os
41
+ import spaces
42
 
43
  huggingface_hub.hf_hub_download('SingleZombie/FRESCO',
44
  'boxer-punching-towards-camera.mp4',
 
171
  1.0, cfg['controlnet_type'], 50, 100,
172
  cfg['num_inference_steps'], 7.5, a_prompt, n_prompt,
173
  frame_count, cfg['batch_size'], cfg['mininterv'], cfg['maxinterv'],
174
+ use_constraints, True,
175
  1, 1, 1, 1
176
  ]
177
  return args
 
233
  param.requires_grad = False
234
 
235
 
 
 
 
 
 
236
 
237
 
238
  @torch.no_grad()
239
+ @spaces.GPU(duration=240)
240
  def process1(input_path, prompt, sd_model, seed, image_resolution, control_strength,
241
  x0_strength, control_type, low_threshold, high_threshold,
242
  ddpm_steps, scale, a_prompt, n_prompt,
243
  frame_count, batch_size, mininterv, maxinterv,
244
  use_constraints, bg_smooth, use_poisson, max_process,
245
  b1, b2, s1, s2):
246
+ global_state = GlobalState()
247
  global_state.update_controlnet_model(control_type)
248
  global_state.update_sd_model(sd_model)
249
  apply_freeu(global_state.pipe, b1=b1, b2=b2, s1=s1, s2=s2)
 
389
  gc.collect()
390
  if device == 'cuda':
391
  torch.cuda.empty_cache()
392
+ break
393
 
394
  writer = imageio.get_writer(os.path.join(save_path, 'key.mp4'), fps=fps)
395
  file_list = sorted(os.listdir(os.path.join(save_path, 'keys')))
 
401
  writer.append_data(curImg)
402
  writer.close()
403
 
 
404
  return os.path.join(save_path, 'key.mp4')
405
 
406
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
407
  config_dir = 'config'
408
  filenames = os.listdir(config_dir)
409
  config_list = []
 
411
  if filename.endswith('yaml'):
412
  config_list.append(f'{config_dir}/{filename}')
413
 
 
414
  block = gr.Blocks().queue()
415
  with block:
416
  with gr.Row():
 
418
  with gr.Row():
419
  with gr.Column():
420
  input_path = gr.Video(label='Input Video',
421
+ sources='upload',
422
  format='mp4',
423
  visible=True)
424
  prompt = gr.Textbox(label='Prompt')
 
434
  step=1,
435
  value=0,
436
  randomize=True)
437
+ run_button1 = gr.Button(value='Run Key Frames')
 
 
 
438
  with gr.Accordion('Advanced options for single frame processing',
439
  open=False):
440
  image_resolution = gr.Slider(label='Frame resolution',
 
554
  value=True,
555
  info='Select to smooth background')
556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557
  with gr.Accordion('Example configs', open=True):
558
 
559
  example_list = [cfg_to_input(x) for x in config_list]
 
563
  x0_strength, control_type, low_threshold, high_threshold,
564
  ddpm_steps, scale, a_prompt, n_prompt,
565
  frame_count, batch_size, mininterv, maxinterv,
566
+ use_constraints[0], bg_smooth,
567
  b1, b2, s1, s2
568
  ]
569
 
 
576
  result_keyframe = gr.Video(label='Output key frame video',
577
  format='mp4',
578
  interactive=False)
 
 
 
579
 
580
  def input_changed(path):
581
  if path is None:
 
592
  max_interv_c = min(20, frame_count)
593
  min_interv_r = frame_count
594
  max_interv_r = frame_count
595
+ return (gr.Slider.change(minimum=min_interv_l,
596
  value=min_interv_c,
597
  maximum=min_interv_r),
598
+ gr.Slider.change(minimum=max_interv_l,
599
  value=max_interv_c,
600
  maximum=max_interv_r),
601
+ gr.Slider.change(minimum=8,
602
  value=frame_count,
603
  maximum=frame_count),
604
  )
 
608
  input_path.upload(input_changed, input_path, [
609
  mininterv, maxinterv, frame_count])
610
 
 
 
 
611
  run_button1.click(fn=process1, inputs=ips, outputs=[result_keyframe])
 
612
 
613
  block.launch()
requirements.txt CHANGED
@@ -8,4 +8,4 @@ av
8
  basicsr==1.4.2
9
  numba==0.57.0
10
  imageio-ffmpeg
11
- gradio==3.44.4
 
8
  basicsr==1.4.2
9
  numba==0.57.0
10
  imageio-ffmpeg
11
+ gradio==4.27.0