liuyizhang commited on
Commit
c7d2050
1 Parent(s): 63e6e86

update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -44
app.py CHANGED
@@ -477,6 +477,7 @@ def relate_anything(input_image, k):
477
  concate_pil_image = concatenate_images_vertical(current_pil_image, title_image)
478
  pil_image_list.append(concate_pil_image)
479
 
 
480
  return pil_image_list
481
 
482
  mask_source_draw = "draw a mask on input image"
@@ -504,9 +505,6 @@ def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_t
504
  input_mask = np.array(input_mask_pil.convert("L"))
505
 
506
  image_pil, image = load_image(input_image['image'].convert("RGB"))
507
-
508
- # visualize raw image
509
- # image_pil.save(os.path.join(output_dir, f"raw_image_{file_temp}.jpg"))
510
 
511
  size = image_pil.size
512
 
@@ -539,11 +537,6 @@ def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_t
539
  "labels": pred_phrases,
540
  }
541
  image_with_box = plot_boxes_to_image(copy.deepcopy(image_pil), pred_dict)[0]
542
- # image_path = os.path.join(output_dir, f"grounding_dino_output_{file_temp}.jpg")
543
- # image_with_box.save(image_path)
544
- # detection_image_result = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
545
- # os.remove(image_path)
546
- # output_images.append(detection_image_result)
547
  output_images.append(image_with_box)
548
 
549
  logger.info(f'run_anything_task_[{file_temp}]_{task_type}_2_')
@@ -601,12 +594,6 @@ def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_t
601
  masks = torch.where(masks > 0, True, False)
602
  mask = masks[0][0].cpu().numpy()
603
  mask_pil = Image.fromarray(mask)
604
-
605
- # image_path = os.path.join(output_dir, f"image_mask_{file_temp}.jpg")
606
- # mask_pil.convert("RGB").save(image_path)
607
- # image_result = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
608
- # os.remove(image_path)
609
- # output_images.append(image_result)
610
  output_images.append(mask_pil.convert("RGB"))
611
 
612
  if task_type == 'inpainting':
@@ -645,23 +632,10 @@ def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_t
645
  extend_pixels=remove_mask_extend, useRectangle=useRectangle)
646
  mask_imgs.append(mask_pil_exp)
647
  mask_pil = mix_masks(mask_imgs)
648
-
649
- # image_path = os.path.join(output_dir, f"image_mask_{file_temp}.jpg")
650
- # mask_pil.convert("RGB").save(image_path)
651
- # image_result = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
652
- # os.remove(image_path)
653
- # output_images.append(image_result)
654
  output_images.append(mask_pil.convert("RGB"))
655
  image_inpainting = lama_cleaner_process(np.array(image_pil), np.array(mask_pil.convert("L")))
656
 
657
  image_inpainting = image_inpainting.resize((image_pil.size[0], image_pil.size[1]))
658
-
659
- # image_path = os.path.join(output_dir, f"grounded_sam_inpainting_output_{file_temp}.jpg")
660
- # image_inpainting.save(image_path)
661
- # image_result = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
662
- # os.remove(image_path)
663
- # logger.info(f'run_anything_task_[{file_temp}]_{task_type}_9_')
664
- # output_images.append(image_result)
665
  output_images.append(image_inpainting)
666
  return output_images, gr.Gallery.update(label='result images')
667
  else:
@@ -674,10 +648,6 @@ def change_radio_display(task_type, mask_source_radio):
674
  inpaint_prompt_visible = False
675
  mask_source_radio_visible = False
676
  num_relation_visible = False
677
- # run_button_visible = True
678
- # relate_all_button_visible = False
679
- # gsa_gallery_visible = True
680
- # ram_gallery_visible = False
681
  if task_type == "inpainting":
682
  inpaint_prompt_visible = True
683
  if task_type == "inpainting" or task_type == "remove":
@@ -687,12 +657,7 @@ def change_radio_display(task_type, mask_source_radio):
687
  if task_type == "relate anything":
688
  text_prompt_visible = False
689
  num_relation_visible = True
690
- # run_button_visible = False
691
- # relate_all_button_visible = True
692
- # gsa_gallery_visible = False
693
- # ram_gallery_visible = True
694
  return gr.Textbox.update(visible=text_prompt_visible), gr.Textbox.update(visible=inpaint_prompt_visible), gr.Radio.update(visible=mask_source_radio_visible), gr.Slider.update(visible=num_relation_visible)
695
- #, gr.Button.update(visible=run_button_visible), gr.Button.update(visible=relate_all_button_visible), gr.Gallery.update(visible=gsa_gallery_visible), gr.Gallery.update(visible=ram_gallery_visible)
696
 
697
  if __name__ == "__main__":
698
  parser = argparse.ArgumentParser("Grounded SAM demo", add_help=True)
@@ -716,7 +681,6 @@ if __name__ == "__main__":
716
  inpaint_prompt = gr.Textbox(label="Inpaint Prompt (if this is empty, then remove)", visible=False)
717
  num_relation = gr.Slider(label="How many relations do you want to see", minimum=1, maximum=20, value=5, step=1, visible=False)
718
  run_button = gr.Button(label="Run", visible=True)
719
- # relate_all_button = gr.Button(label="Run", visible=False)
720
  with gr.Accordion("Advanced options", open=False) as advanced_options:
721
  box_threshold = gr.Slider(
722
  label="Box Threshold", minimum=0.0, maximum=1.0, value=0.3, step=0.001
@@ -735,16 +699,11 @@ if __name__ == "__main__":
735
  remove_mask_extend = gr.Textbox(label="remove_mask_extend", value='10')
736
 
737
  with gr.Column():
738
- image_gallery = gr.Gallery(label="result images", show_label=True, elem_id="gsa_allery", visible=True
739
- ).style(preview=True, columns=[5], object_fit="scale-down", height="auto")
740
- # gsa_gallery = gr.Gallery(label="result images", show_label=True, elem_id="gsa_allery", visible=True
741
- # ).style(preview=True, grid=[2], full_width=True, full_height=True)
742
- # ram_gallery = gr.Gallery(label="Your Result", show_label=True, elem_id="ram_gallery", visible=False
743
- # ).style(preview=True, columns=5, object_fit="scale-down")
744
 
745
  run_button.click(fn=run_anything_task, inputs=[
746
  input_image, text_prompt, task_type, inpaint_prompt, box_threshold, text_threshold, iou_threshold, inpaint_mode, mask_source_radio, remove_mode, remove_mask_extend, num_relation], outputs=[image_gallery, image_gallery], show_progress=True, queue=True)
747
- # relate_all_button.click(fn=relate_anything, inputs=[input_image, num_relation], outputs=[ram_gallery], show_progress=True, queue=True)
748
 
749
  task_type.change(fn=change_radio_display, inputs=[task_type, mask_source_radio], outputs=[text_prompt, inpaint_prompt, mask_source_radio, num_relation])
750
  mask_source_radio.change(fn=change_radio_display, inputs=[task_type, mask_source_radio], outputs=[text_prompt, inpaint_prompt, mask_source_radio, num_relation])
 
477
  concate_pil_image = concatenate_images_vertical(current_pil_image, title_image)
478
  pil_image_list.append(concate_pil_image)
479
 
480
+ logger.info(f'relate_anything_5_{len(pil_image_list)}')
481
  return pil_image_list
482
 
483
  mask_source_draw = "draw a mask on input image"
 
505
  input_mask = np.array(input_mask_pil.convert("L"))
506
 
507
  image_pil, image = load_image(input_image['image'].convert("RGB"))
 
 
 
508
 
509
  size = image_pil.size
510
 
 
537
  "labels": pred_phrases,
538
  }
539
  image_with_box = plot_boxes_to_image(copy.deepcopy(image_pil), pred_dict)[0]
 
 
 
 
 
540
  output_images.append(image_with_box)
541
 
542
  logger.info(f'run_anything_task_[{file_temp}]_{task_type}_2_')
 
594
  masks = torch.where(masks > 0, True, False)
595
  mask = masks[0][0].cpu().numpy()
596
  mask_pil = Image.fromarray(mask)
 
 
 
 
 
 
597
  output_images.append(mask_pil.convert("RGB"))
598
 
599
  if task_type == 'inpainting':
 
632
  extend_pixels=remove_mask_extend, useRectangle=useRectangle)
633
  mask_imgs.append(mask_pil_exp)
634
  mask_pil = mix_masks(mask_imgs)
 
 
 
 
 
 
635
  output_images.append(mask_pil.convert("RGB"))
636
  image_inpainting = lama_cleaner_process(np.array(image_pil), np.array(mask_pil.convert("L")))
637
 
638
  image_inpainting = image_inpainting.resize((image_pil.size[0], image_pil.size[1]))
 
 
 
 
 
 
 
639
  output_images.append(image_inpainting)
640
  return output_images, gr.Gallery.update(label='result images')
641
  else:
 
648
  inpaint_prompt_visible = False
649
  mask_source_radio_visible = False
650
  num_relation_visible = False
 
 
 
 
651
  if task_type == "inpainting":
652
  inpaint_prompt_visible = True
653
  if task_type == "inpainting" or task_type == "remove":
 
657
  if task_type == "relate anything":
658
  text_prompt_visible = False
659
  num_relation_visible = True
 
 
 
 
660
  return gr.Textbox.update(visible=text_prompt_visible), gr.Textbox.update(visible=inpaint_prompt_visible), gr.Radio.update(visible=mask_source_radio_visible), gr.Slider.update(visible=num_relation_visible)
 
661
 
662
  if __name__ == "__main__":
663
  parser = argparse.ArgumentParser("Grounded SAM demo", add_help=True)
 
681
  inpaint_prompt = gr.Textbox(label="Inpaint Prompt (if this is empty, then remove)", visible=False)
682
  num_relation = gr.Slider(label="How many relations do you want to see", minimum=1, maximum=20, value=5, step=1, visible=False)
683
  run_button = gr.Button(label="Run", visible=True)
 
684
  with gr.Accordion("Advanced options", open=False) as advanced_options:
685
  box_threshold = gr.Slider(
686
  label="Box Threshold", minimum=0.0, maximum=1.0, value=0.3, step=0.001
 
699
  remove_mask_extend = gr.Textbox(label="remove_mask_extend", value='10')
700
 
701
  with gr.Column():
702
+ image_gallery = gr.Gallery(label="result images", show_label=True, elem_id="gallery", visible=True
703
+ ).style(preview=True, columns=[5], object_fit="scale-down", height="auto")
 
 
 
 
704
 
705
  run_button.click(fn=run_anything_task, inputs=[
706
  input_image, text_prompt, task_type, inpaint_prompt, box_threshold, text_threshold, iou_threshold, inpaint_mode, mask_source_radio, remove_mode, remove_mask_extend, num_relation], outputs=[image_gallery, image_gallery], show_progress=True, queue=True)
 
707
 
708
  task_type.change(fn=change_radio_display, inputs=[task_type, mask_source_radio], outputs=[text_prompt, inpaint_prompt, mask_source_radio, num_relation])
709
  mask_source_radio.change(fn=change_radio_display, inputs=[task_type, mask_source_radio], outputs=[text_prompt, inpaint_prompt, mask_source_radio, num_relation])