Spaces:
Runtime error
Runtime error
liuyizhang
commited on
Commit
·
f19c1db
1
Parent(s):
3f5ffbb
add app_cli.py
Browse files- app.py +7 -5
- app_cli.py +121 -0
- dog.png +0 -0
app.py
CHANGED
|
@@ -19,11 +19,6 @@ if os.environ.get('IS_MY_DEBUG') is None:
|
|
| 19 |
|
| 20 |
sys.path.insert(0, './GroundingDINO')
|
| 21 |
|
| 22 |
-
if not os.path.exists('./sam_vit_h_4b8939.pth'):
|
| 23 |
-
logger.info(f"get sam_vit_h_4b8939.pth...")
|
| 24 |
-
result = subprocess.run(['wget', 'https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth'], check=True)
|
| 25 |
-
print(f'wget sam_vit_h_4b8939.pth result = {result}')
|
| 26 |
-
|
| 27 |
import gradio as gr
|
| 28 |
|
| 29 |
import argparse
|
|
@@ -85,6 +80,12 @@ sd_pipe = None
|
|
| 85 |
lama_cleaner_model= None
|
| 86 |
ram_model = None
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
def load_model_hf(model_config_path, repo_id, filename, device='cpu'):
|
| 89 |
args = SLConfig.fromfile(model_config_path)
|
| 90 |
model = build_model(args)
|
|
@@ -702,6 +703,7 @@ if __name__ == "__main__":
|
|
| 702 |
print(f'args = {args}')
|
| 703 |
|
| 704 |
set_device()
|
|
|
|
| 705 |
load_groundingdino_model()
|
| 706 |
load_sam_model()
|
| 707 |
load_sd_model()
|
|
|
|
| 19 |
|
| 20 |
sys.path.insert(0, './GroundingDINO')
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
import gradio as gr
|
| 23 |
|
| 24 |
import argparse
|
|
|
|
| 80 |
lama_cleaner_model= None
|
| 81 |
ram_model = None
|
| 82 |
|
| 83 |
+
def get_sam_vit_h_4b8939():
|
| 84 |
+
if not os.path.exists('./sam_vit_h_4b8939.pth'):
|
| 85 |
+
logger.info(f"get sam_vit_h_4b8939.pth...")
|
| 86 |
+
result = subprocess.run(['wget', 'https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth'], check=True)
|
| 87 |
+
print(f'wget sam_vit_h_4b8939.pth result = {result}')
|
| 88 |
+
|
| 89 |
def load_model_hf(model_config_path, repo_id, filename, device='cpu'):
|
| 90 |
args = SLConfig.fromfile(model_config_path)
|
| 91 |
model = build_model(args)
|
|
|
|
| 703 |
print(f'args = {args}')
|
| 704 |
|
| 705 |
set_device()
|
| 706 |
+
get_sam_vit_h_4b8939()
|
| 707 |
load_groundingdino_model()
|
| 708 |
load_sam_model()
|
| 709 |
load_sd_model()
|
app_cli.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import warnings
|
| 3 |
+
warnings.filterwarnings('ignore')
|
| 4 |
+
|
| 5 |
+
import subprocess, io, os, sys, time
|
| 6 |
+
from loguru import logger
|
| 7 |
+
|
| 8 |
+
# os.system("pip install diffuser==0.6.0")
|
| 9 |
+
# os.system("pip install transformers==4.29.1")
|
| 10 |
+
|
| 11 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
| 12 |
+
|
| 13 |
+
if os.environ.get('IS_MY_DEBUG') is None:
|
| 14 |
+
result = subprocess.run(['pip', 'install', '-e', 'GroundingDINO'], check=True)
|
| 15 |
+
print(f'pip install GroundingDINO = {result}')
|
| 16 |
+
|
| 17 |
+
# result = subprocess.run(['pip', 'list'], check=True)
|
| 18 |
+
# print(f'pip list = {result}')
|
| 19 |
+
|
| 20 |
+
sys.path.insert(0, './GroundingDINO')
|
| 21 |
+
|
| 22 |
+
import gradio as gr
|
| 23 |
+
|
| 24 |
+
import argparse
|
| 25 |
+
|
| 26 |
+
import copy
|
| 27 |
+
|
| 28 |
+
import numpy as np
|
| 29 |
+
import torch
|
| 30 |
+
from PIL import Image, ImageDraw, ImageFont, ImageOps
|
| 31 |
+
|
| 32 |
+
# Grounding DINO
|
| 33 |
+
import GroundingDINO.groundingdino.datasets.transforms as T
|
| 34 |
+
from GroundingDINO.groundingdino.models import build_model
|
| 35 |
+
from GroundingDINO.groundingdino.util import box_ops
|
| 36 |
+
from GroundingDINO.groundingdino.util.slconfig import SLConfig
|
| 37 |
+
from GroundingDINO.groundingdino.util.utils import clean_state_dict, get_phrases_from_posmap
|
| 38 |
+
|
| 39 |
+
import cv2
|
| 40 |
+
import numpy as np
|
| 41 |
+
import matplotlib.pyplot as plt
|
| 42 |
+
from lama_cleaner.model_manager import ModelManager
|
| 43 |
+
from lama_cleaner.schema import Config as lama_Config
|
| 44 |
+
|
| 45 |
+
# segment anything
|
| 46 |
+
from segment_anything import build_sam, SamPredictor, SamAutomaticMaskGenerator
|
| 47 |
+
|
| 48 |
+
# diffusers
|
| 49 |
+
import PIL
|
| 50 |
+
import requests
|
| 51 |
+
import torch
|
| 52 |
+
from io import BytesIO
|
| 53 |
+
from diffusers import StableDiffusionInpaintPipeline
|
| 54 |
+
from huggingface_hub import hf_hub_download
|
| 55 |
+
|
| 56 |
+
from utils import computer_info
|
| 57 |
+
# relate anything
|
| 58 |
+
from ram_utils import iou, sort_and_deduplicate, relation_classes, MLP, show_anns, ram_show_mask
|
| 59 |
+
from ram_train_eval import RamModel,RamPredictor
|
| 60 |
+
from mmengine.config import Config as mmengine_Config
|
| 61 |
+
|
| 62 |
+
from app import *
|
| 63 |
+
|
| 64 |
+
config_file = 'GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py'
|
| 65 |
+
ckpt_repo_id = "ShilongLiu/GroundingDINO"
|
| 66 |
+
ckpt_filenmae = "groundingdino_swint_ogc.pth"
|
| 67 |
+
sam_checkpoint = './sam_vit_h_4b8939.pth'
|
| 68 |
+
output_dir = "outputs"
|
| 69 |
+
device = 'cpu'
|
| 70 |
+
|
| 71 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 72 |
+
groundingdino_model = None
|
| 73 |
+
sam_device = None
|
| 74 |
+
sam_model = None
|
| 75 |
+
sam_predictor = None
|
| 76 |
+
sam_mask_generator = None
|
| 77 |
+
sd_pipe = None
|
| 78 |
+
lama_cleaner_model= None
|
| 79 |
+
ram_model = None
|
| 80 |
+
|
| 81 |
+
def get_args():
|
| 82 |
+
argparser = argparse.ArgumentParser()
|
| 83 |
+
argparser.add_argument("--input_image", "-i", type=str, default="", help="")
|
| 84 |
+
argparser.add_argument("--text", "-t", type=str, default="", help="")
|
| 85 |
+
argparser.add_argument("--output_image", "-o", type=str, default="", help="")
|
| 86 |
+
args = argparser.parse_args()
|
| 87 |
+
return args
|
| 88 |
+
|
| 89 |
+
# usage:
|
| 90 |
+
# python app_cli.py --input_image dog.png --text dog --output_image dog_remove.png
|
| 91 |
+
|
| 92 |
+
if __name__ == '__main__':
|
| 93 |
+
args = get_args()
|
| 94 |
+
logger.info(f'\nargs={args}\n')
|
| 95 |
+
|
| 96 |
+
logger.info(f'loading models ... ')
|
| 97 |
+
# set_device() # If you have enough GPUs, you can open this comment
|
| 98 |
+
get_sam_vit_h_4b8939()
|
| 99 |
+
load_groundingdino_model()
|
| 100 |
+
load_sam_model()
|
| 101 |
+
# load_sd_model()
|
| 102 |
+
load_lama_cleaner_model()
|
| 103 |
+
# load_ram_model()
|
| 104 |
+
|
| 105 |
+
input_image = Image.open(args.input_image)
|
| 106 |
+
|
| 107 |
+
output_images, _ = run_anything_task(input_image = input_image,
|
| 108 |
+
text_prompt = args.text,
|
| 109 |
+
task_type = 'remove',
|
| 110 |
+
inpaint_prompt = '',
|
| 111 |
+
box_threshold = 0.3,
|
| 112 |
+
text_threshold = 0.25,
|
| 113 |
+
iou_threshold = 0.8,
|
| 114 |
+
inpaint_mode = "merge",
|
| 115 |
+
mask_source_radio = "type what to detect below",
|
| 116 |
+
remove_mode = "rectangle", # ["segment", "rectangle"]
|
| 117 |
+
remove_mask_extend = "10",
|
| 118 |
+
num_relation = 5)
|
| 119 |
+
if len(output_images) > 0:
|
| 120 |
+
logger.info(f'save result to {args.output_image} ... ')
|
| 121 |
+
output_images[-1].save(args.output_image)
|
dog.png
ADDED
|