update demo to 4.18.0 gradio version

pull/295/head
Skwarson96 2024-02-13 22:29:21 +01:00
parent 2b62f419c2
commit 66be972730
1 changed files with 13 additions and 22 deletions

View File

@ -1,22 +1,16 @@
import argparse import argparse
from functools import partial
import cv2 import cv2
import requests
import os
from io import BytesIO
from PIL import Image from PIL import Image
import numpy as np import numpy as np
from pathlib import Path
import warnings import warnings
import torch import torch
# prepare the environment # prepare the environment
os.system("python setup.py build develop --user") # os.system("python setup.py build develop --user")
os.system("pip install packaging==21.3") # os.system("pip install packaging==21.3")
os.system("pip install gradio") # os.system("pip install gradio")
warnings.filterwarnings("ignore") warnings.filterwarnings("ignore")
@ -26,13 +20,12 @@ import gradio as gr
from groundingdino.models import build_model from groundingdino.models import build_model
from groundingdino.util.slconfig import SLConfig from groundingdino.util.slconfig import SLConfig
from groundingdino.util.utils import clean_state_dict from groundingdino.util.utils import clean_state_dict
from groundingdino.util.inference import annotate, load_image, predict from groundingdino.util.inference import annotate, load_image, predict, load_model
import groundingdino.datasets.transforms as T import groundingdino.datasets.transforms as T
from huggingface_hub import hf_hub_download from huggingface_hub import hf_hub_download
# Use this command for evaluate the Grounding DINO model # Use this command for evaluate the Grounding DINO model
config_file = "groundingdino/config/GroundingDINO_SwinT_OGC.py" config_file = "groundingdino/config/GroundingDINO_SwinT_OGC.py"
ckpt_repo_id = "ShilongLiu/GroundingDINO" ckpt_repo_id = "ShilongLiu/GroundingDINO"
@ -71,13 +64,12 @@ model = load_model_hf(config_file, ckpt_repo_id, ckpt_filenmae)
def run_grounding(input_image, grounding_caption, box_threshold, text_threshold): def run_grounding(input_image, grounding_caption, box_threshold, text_threshold):
init_image = input_image.convert("RGB") init_image = input_image.convert("RGB")
original_size = init_image.size
_, image_tensor = image_transform_grounding(init_image) _, image_tensor = image_transform_grounding(init_image)
image_pil: Image = image_transform_grounding_for_vis(init_image) image_pil: Image = image_transform_grounding_for_vis(init_image)
# run grounidng # run grounidng
boxes, logits, phrases = predict(model, image_tensor, grounding_caption, box_threshold, text_threshold, device='cpu') boxes, logits, phrases = predict(model, image_tensor, grounding_caption, box_threshold, text_threshold, device='cuda')
annotated_frame = annotate(image_source=np.asarray(image_pil), boxes=boxes, logits=logits, phrases=phrases) annotated_frame = annotate(image_source=np.asarray(image_pil), boxes=boxes, logits=logits, phrases=phrases)
image_with_box = Image.fromarray(cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)) image_with_box = Image.fromarray(cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB))
@ -98,9 +90,9 @@ if __name__ == "__main__":
with gr.Row(): with gr.Row():
with gr.Column(): with gr.Column():
input_image = gr.Image(source='upload', type="pil") input_image = gr.Image(label='upload', type="pil")
grounding_caption = gr.Textbox(label="Detection Prompt") grounding_caption = gr.Textbox(label="Detection Prompt")
run_button = gr.Button(label="Run") run_button = gr.Button(value="Run")
with gr.Accordion("Advanced options", open=False): with gr.Accordion("Advanced options", open=False):
box_threshold = gr.Slider( box_threshold = gr.Slider(
label="Box Threshold", minimum=0.0, maximum=1.0, value=0.25, step=0.001 label="Box Threshold", minimum=0.0, maximum=1.0, value=0.25, step=0.001
@ -110,10 +102,10 @@ if __name__ == "__main__":
) )
with gr.Column(): with gr.Column():
gallery = gr.outputs.Image( gallery = gr.components.Image(
type="pil", label="grounding results",
# label="grounding results" type="pil"
).style(full_width=True, full_height=True) )
# gallery = gr.Gallery(label="Generated images", show_label=False).style( # gallery = gr.Gallery(label="Generated images", show_label=False).style(
# grid=[1], height="auto", container=True, full_width=True, full_height=True) # grid=[1], height="auto", container=True, full_width=True, full_height=True)
@ -122,4 +114,3 @@ if __name__ == "__main__":
block.launch(server_name='0.0.0.0', server_port=7579, debug=args.debug, share=args.share) block.launch(server_name='0.0.0.0', server_port=7579, debug=args.debug, share=args.share)