yizhangliu commited on
Commit
9ba66a8
1 Parent(s): 904637c

update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -21
app.py CHANGED
@@ -615,11 +615,22 @@ def load_kolors_inpainting(inpaint_prompt, input_image, mask_image):
615
  from gradio_client import Client, handle_file
616
  import tempfile
617
  MAX_IMAGE_SIZE = 1024
 
 
 
 
 
 
 
 
 
 
618
  try:
619
  # logger.info(f'load_kolors_inpainting_input_image={inpaint_prompt} // {input_image}')
620
  # logger.info(f'load_kolors_inpainting_mask_image={mask_image}')
621
 
622
  job_image = {}
 
623
  if 'background' in input_image.keys():
624
  width, height = input_image['background'].size
625
  if max(width, height) > MAX_IMAGE_SIZE:
@@ -638,31 +649,22 @@ def load_kolors_inpainting(inpaint_prompt, input_image, mask_image):
638
  img.save(temp_file_path)
639
  # logger.info(f'load_kolors_inpainting_temp_file_background_={temp_file_path}')
640
  job_image["background"] = handle_file(temp_file_path)
641
- if 'layers' in input_image.keys() and len(input_image['layers']) > 0:
642
- _, temp_file_path = tempfile.mkstemp(suffix='.png')
643
- img = input_image['layers'][0].convert("RGB").resize((resize_width, resize_height))
644
- img.save(temp_file_path)
645
- # logger.info(f'load_kolors_inpainting_temp_file_layers_={temp_file_path}')
646
- job_image["layers"] = [handle_file(temp_file_path)]
647
 
648
- if 'composite' in input_image.keys():
649
  _, temp_file_path = tempfile.mkstemp(suffix='.png')
650
- img = input_image['composite'].convert("RGB").resize((resize_width, resize_height))
 
 
 
 
651
  img.save(temp_file_path)
652
- # logger.info(f'load_kolors_inpainting_temp_file_composite_={temp_file_path}')
653
- job_image["composite"] = handle_file(temp_file_path)
654
-
655
- _, temp_file_path = tempfile.mkstemp(suffix='.png')
656
- img = mask_image.convert("RGB").resize((resize_width, resize_height))
657
- img.save(temp_file_path)
658
- # logger.info(f'load_kolors_inpainting_temp_file_mask_={temp_file_path}')
659
- job_mask_image = handle_file(temp_file_path)
660
 
661
  # logger.info(f'load_kolors_inpainting_job_image={job_image}')
662
  # logger.info(f'load_kolors_inpainting_job_mask_image={job_mask_image}')
663
 
664
- client = Client("Kwai-Kolors/Kolors-Inpainting")
665
-
666
  job = client.submit(
667
  prompt=inpaint_prompt,
668
  image=job_image,
@@ -670,7 +672,7 @@ def load_kolors_inpainting(inpaint_prompt, input_image, mask_image):
670
  negative_prompt="broken fingers, deformed fingers, deformed hands, stumps, blurriness, low quality",
671
  seed=0,
672
  randomize_seed=True,
673
- guidance_scale=6,
674
  num_inference_steps=25,
675
  api_name="/infer"
676
  )
@@ -879,7 +881,8 @@ def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_t
879
  image_inpainting = load_kolors_inpainting(inpaint_prompt, input_image, image_mask_for_inpaint)
880
  if image_inpainting is None:
881
  logger.info(f'load_kolors_inpainting_failed_')
882
- return None, None, None, None, None, None, None
 
883
  else:
884
  # remove from mask
885
  if mask_source_radio == mask_source_segment:
@@ -916,7 +919,8 @@ def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_t
916
  image_inpainting = lama_cleaner_process(np.array(image_pil), np.array(mask_pil.convert("L")), cleaner_size_limit)
917
  if image_inpainting is None:
918
  logger.info(f'run_anything_task_failed_')
919
- return None, None, None, None, None, None, None
 
920
 
921
  # output_images.append(image_inpainting)
922
  # run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
 
615
  from gradio_client import Client, handle_file
616
  import tempfile
617
  MAX_IMAGE_SIZE = 1024
618
+
619
+ def change_RGB_value(image, r0, g0, b0, r1, g1, b1):
620
+ pixels = image.load()
621
+ for i in range(image.size[0]):
622
+ for j in range(image.size[1]):
623
+ r, g, b = pixels[i, j]
624
+ if r == r0 and g == g0 and b == b0:
625
+ pixels[i, j] = (r1, g1, b1)
626
+ return image
627
+
628
  try:
629
  # logger.info(f'load_kolors_inpainting_input_image={inpaint_prompt} // {input_image}')
630
  # logger.info(f'load_kolors_inpainting_mask_image={mask_image}')
631
 
632
  job_image = {}
633
+ job_mask_image = None
634
  if 'background' in input_image.keys():
635
  width, height = input_image['background'].size
636
  if max(width, height) > MAX_IMAGE_SIZE:
 
649
  img.save(temp_file_path)
650
  # logger.info(f'load_kolors_inpainting_temp_file_background_={temp_file_path}')
651
  job_image["background"] = handle_file(temp_file_path)
 
 
 
 
 
 
652
 
653
+ if mask_image is not None:
654
  _, temp_file_path = tempfile.mkstemp(suffix='.png')
655
+ img = mask_image.convert("RGB").resize((resize_width, resize_height))
656
+ # RGB(0,0,0) --> RGB(230,230,230)
657
+ img = change_RGB_value(img, 0, 0, 0, 230, 230, 230)
658
+ # RGB(255,255,255) --> RGB(170,170,170)
659
+ img = change_RGB_value(img, 255, 255, 255, 170, 170, 170)
660
  img.save(temp_file_path)
661
+ # logger.info(f'load_kolors_inpainting_temp_file___mask_={temp_file_path}')
662
+ job_image["layers"] = [handle_file(temp_file_path)]
 
 
 
 
 
 
663
 
664
  # logger.info(f'load_kolors_inpainting_job_image={job_image}')
665
  # logger.info(f'load_kolors_inpainting_job_mask_image={job_mask_image}')
666
 
667
+ client = Client("Kwai-Kolors/Kolors-Inpainting")
 
668
  job = client.submit(
669
  prompt=inpaint_prompt,
670
  image=job_image,
 
672
  negative_prompt="broken fingers, deformed fingers, deformed hands, stumps, blurriness, low quality",
673
  seed=0,
674
  randomize_seed=True,
675
+ guidance_scale=6.0,
676
  num_inference_steps=25,
677
  api_name="/infer"
678
  )
 
881
  image_inpainting = load_kolors_inpainting(inpaint_prompt, input_image, image_mask_for_inpaint)
882
  if image_inpainting is None:
883
  logger.info(f'load_kolors_inpainting_failed_')
884
+ time_cost_str = f"load_kolors_inpainting_task__failed!"
885
+ return None, None, time_cost_str, gr.update(visible=(time_cost_str !='')), None, None, None
886
  else:
887
  # remove from mask
888
  if mask_source_radio == mask_source_segment:
 
919
  image_inpainting = lama_cleaner_process(np.array(image_pil), np.array(mask_pil.convert("L")), cleaner_size_limit)
920
  if image_inpainting is None:
921
  logger.info(f'run_anything_task_failed_')
922
+ time_cost_str = f"run_anything_task[{task_type}]__failed!"
923
+ return None, None, time_cost_str, gr.update(visible=(time_cost_str !='')), None, None, None
924
 
925
  # output_images.append(image_inpainting)
926
  # run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)