Upload app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import warnings
|
|
| 4 |
import copy
|
| 5 |
os.system("python -m pip install -e asam")
|
| 6 |
os.system("python -m pip install -e GroundingDINO")
|
| 7 |
-
os.system("python -m pip uninstall gradio")
|
| 8 |
os.system("python -m pip install gradio==3.38.0")
|
| 9 |
os.system("pip install opencv-python pycocotools matplotlib onnxruntime onnx ipykernel")
|
| 10 |
sys.path.append(os.path.join(os.getcwd(), "GroundingDINO"))
|
|
@@ -162,6 +162,7 @@ sam_predictor = None
|
|
| 162 |
|
| 163 |
|
| 164 |
def run_grounded_sam(input_image, text_prompt, task_type, box_threshold, text_threshold, iou_threshold):
|
|
|
|
| 165 |
global blip_processor, blip_model, groundingdino_model, sam_predictor
|
| 166 |
|
| 167 |
# make dir
|
|
@@ -170,7 +171,8 @@ def run_grounded_sam(input_image, text_prompt, task_type, box_threshold, text_th
|
|
| 170 |
scribble = np.array(input_image["mask"])
|
| 171 |
image_pil = input_image["image"].convert("RGB")
|
| 172 |
transformed_image = transform_image(image_pil)
|
| 173 |
-
|
|
|
|
| 174 |
if groundingdino_model is None:
|
| 175 |
groundingdino_model = load_model(
|
| 176 |
config_file, ckpt_filenmae, device=device)
|
|
@@ -225,8 +227,34 @@ def run_grounded_sam(input_image, text_prompt, task_type, box_threshold, text_th
|
|
| 225 |
# use NMS to handle overlapped boxes
|
| 226 |
print(f"Revise caption with number: {text_prompt}")
|
| 227 |
|
| 228 |
-
if task_type == '
|
| 229 |
-
if task_type == '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
scribble = scribble.transpose(2, 1, 0)[0]
|
| 231 |
labeled_array, num_features = ndimage.label(scribble >= 255)
|
| 232 |
centers = ndimage.center_of_mass(scribble, labeled_array, range(1, num_features+1))
|
|
@@ -261,7 +289,7 @@ def run_grounded_sam(input_image, text_prompt, task_type, box_threshold, text_th
|
|
| 261 |
draw_mask(mask[0].cpu().numpy(), mask_draw, random_color=True)
|
| 262 |
image_draw = ImageDraw.Draw(image_pil)
|
| 263 |
|
| 264 |
-
if task_type == 'scribble_box':
|
| 265 |
for box in bbox:
|
| 266 |
draw_box(box, image_draw, None)
|
| 267 |
else:
|
|
@@ -305,7 +333,7 @@ def run_grounded_sam(input_image, text_prompt, task_type, box_threshold, text_th
|
|
| 305 |
draw_mask(a_mask[0].cpu().numpy(), a_mask_draw, random_color=True)
|
| 306 |
a_image_draw = ImageDraw.Draw(a_image_pil)
|
| 307 |
|
| 308 |
-
if task_type == 'scribble_box':
|
| 309 |
for box in bbox:
|
| 310 |
draw_box(box, a_image_draw, None)
|
| 311 |
else:
|
|
@@ -373,8 +401,6 @@ def run_grounded_sam(input_image, text_prompt, task_type, box_threshold, text_th
|
|
| 373 |
a_image_pil.alpha_composite(a_mask_image)
|
| 374 |
|
| 375 |
return [[image_pil, mask_image],[a_image_pil, a_mask_image]]
|
| 376 |
-
|
| 377 |
-
|
| 378 |
|
| 379 |
else:
|
| 380 |
print("task_type:{} error!".format(task_type))
|
|
@@ -406,14 +432,14 @@ if __name__ == "__main__":
|
|
| 406 |
You may check the instruction below, or check our github page about more details.
|
| 407 |
<details>
|
| 408 |
You may select an example image or upload your image to start, we support 4 prompt types:
|
| 409 |
-
|
|
|
|
|
|
|
| 410 |
**automatic**: Automaticly generate text prompt and the corresponding box input with BLIP and Grounding-DINO.
|
| 411 |
|
| 412 |
**scribble_point**: Click an point on the target instance.
|
| 413 |
|
| 414 |
**scribble_box**: Click on two points, the top-left point and the bottom-right point to represent a bounding box of the target instance.
|
| 415 |
-
|
| 416 |
-
**text**: Send text prompt to identify the target instance in the `Text prompt` box.
|
| 417 |
|
| 418 |
</details>
|
| 419 |
""")
|
|
@@ -421,14 +447,14 @@ if __name__ == "__main__":
|
|
| 421 |
with gr.Row():
|
| 422 |
with gr.Column():
|
| 423 |
input_image = gr.Image(
|
| 424 |
-
source='upload', type="pil", value="
|
| 425 |
task_type = gr.Dropdown(
|
| 426 |
-
["automatic", "scribble_point", "scribble_box", "text"], value="
|
| 427 |
-
text_prompt = gr.Textbox(label="Text Prompt", placeholder="bench .")
|
| 428 |
run_button = gr.Button(label="Run")
|
| 429 |
with gr.Accordion("Advanced options", open=False):
|
| 430 |
box_threshold = gr.Slider(
|
| 431 |
-
label="Box Threshold", minimum=0.0, maximum=1.0, value=0.
|
| 432 |
)
|
| 433 |
text_threshold = gr.Slider(
|
| 434 |
label="Text Threshold", minimum=0.0, maximum=1.0, value=0.25, step=0.001
|
|
@@ -461,8 +487,19 @@ if __name__ == "__main__":
|
|
| 461 |
with gr.Column():
|
| 462 |
gr.Examples(["example2.jpg"], inputs=input_image)
|
| 463 |
with gr.Column():
|
| 464 |
-
|
| 465 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
run_button.click(fn=run_grounded_sam, inputs=[
|
| 467 |
input_image, text_prompt, task_type, box_threshold, text_threshold, iou_threshold], outputs=[gallery1,gallery2])
|
| 468 |
|
|
|
|
| 4 |
import copy
|
| 5 |
os.system("python -m pip install -e asam")
|
| 6 |
os.system("python -m pip install -e GroundingDINO")
|
| 7 |
+
# os.system("python -m pip uninstall gradio")
|
| 8 |
os.system("python -m pip install gradio==3.38.0")
|
| 9 |
os.system("pip install opencv-python pycocotools matplotlib onnxruntime onnx ipykernel")
|
| 10 |
sys.path.append(os.path.join(os.getcwd(), "GroundingDINO"))
|
|
|
|
| 162 |
|
| 163 |
|
| 164 |
def run_grounded_sam(input_image, text_prompt, task_type, box_threshold, text_threshold, iou_threshold):
|
| 165 |
+
print(text_prompt, type(text_prompt))
|
| 166 |
global blip_processor, blip_model, groundingdino_model, sam_predictor
|
| 167 |
|
| 168 |
# make dir
|
|
|
|
| 171 |
scribble = np.array(input_image["mask"])
|
| 172 |
image_pil = input_image["image"].convert("RGB")
|
| 173 |
transformed_image = transform_image(image_pil)
|
| 174 |
+
print('img sum:' ,torch.sum(transformed_image).to(torch.int).item())
|
| 175 |
+
|
| 176 |
if groundingdino_model is None:
|
| 177 |
groundingdino_model = load_model(
|
| 178 |
config_file, ckpt_filenmae, device=device)
|
|
|
|
| 227 |
# use NMS to handle overlapped boxes
|
| 228 |
print(f"Revise caption with number: {text_prompt}")
|
| 229 |
|
| 230 |
+
if task_type == 'default_box' or task_type == 'automatic' or task_type == 'scribble_box':
|
| 231 |
+
if task_type == 'default_box':
|
| 232 |
+
id = torch.sum(transformed_image).to(torch.int).item()
|
| 233 |
+
if id == -1683627: #example 1 *
|
| 234 |
+
x_min, y_min, x_max, y_max = 204, 213, 813, 1023
|
| 235 |
+
elif id == 1137390: #example 2 *
|
| 236 |
+
x_min, y_min, x_max, y_max = 125, 168, 842, 904
|
| 237 |
+
elif id == 1145309: #example 3 *
|
| 238 |
+
x_min, y_min, x_max, y_max = 0, 486, 992, 899
|
| 239 |
+
elif id == 1091779: #example 4 *
|
| 240 |
+
x_min, y_min, x_max, y_max = 2, 73, 981, 968
|
| 241 |
+
elif id == -1335352: #example 5 *
|
| 242 |
+
x_min, y_min, x_max, y_max = 201, 195, 811, 1023
|
| 243 |
+
elif id == -1479645: #example 6
|
| 244 |
+
x_min, y_min, x_max, y_max = 428, 0, 992, 799
|
| 245 |
+
elif id == -544197: #example 7
|
| 246 |
+
x_min, y_min, x_max, y_max = 106, 419, 312, 783
|
| 247 |
+
elif id == -23873: #example 8
|
| 248 |
+
x_min, y_min, x_max, y_max = 250, 25, 774, 803
|
| 249 |
+
elif id == -1572157: #example 9 *
|
| 250 |
+
x_min, y_min, x_max, y_max = 15, 88, 1006, 977
|
| 251 |
+
else:
|
| 252 |
+
print("not defined")
|
| 253 |
+
raise NotImplementedError
|
| 254 |
+
bbox = np.array([x_min, y_min, x_max, y_max])
|
| 255 |
+
bbox = torch.tensor(bbox).unsqueeze(0)
|
| 256 |
+
transformed_boxes = sam_predictor.transform.apply_boxes_torch(bbox, image.shape[:2]).to(device)
|
| 257 |
+
elif task_type == 'scribble_box':
|
| 258 |
scribble = scribble.transpose(2, 1, 0)[0]
|
| 259 |
labeled_array, num_features = ndimage.label(scribble >= 255)
|
| 260 |
centers = ndimage.center_of_mass(scribble, labeled_array, range(1, num_features+1))
|
|
|
|
| 289 |
draw_mask(mask[0].cpu().numpy(), mask_draw, random_color=True)
|
| 290 |
image_draw = ImageDraw.Draw(image_pil)
|
| 291 |
|
| 292 |
+
if task_type == 'scribble_box' or task_type == 'default_box':
|
| 293 |
for box in bbox:
|
| 294 |
draw_box(box, image_draw, None)
|
| 295 |
else:
|
|
|
|
| 333 |
draw_mask(a_mask[0].cpu().numpy(), a_mask_draw, random_color=True)
|
| 334 |
a_image_draw = ImageDraw.Draw(a_image_pil)
|
| 335 |
|
| 336 |
+
if task_type == 'scribble_box' or task_type == 'default_box':
|
| 337 |
for box in bbox:
|
| 338 |
draw_box(box, a_image_draw, None)
|
| 339 |
else:
|
|
|
|
| 401 |
a_image_pil.alpha_composite(a_mask_image)
|
| 402 |
|
| 403 |
return [[image_pil, mask_image],[a_image_pil, a_mask_image]]
|
|
|
|
|
|
|
| 404 |
|
| 405 |
else:
|
| 406 |
print("task_type:{} error!".format(task_type))
|
|
|
|
| 432 |
You may check the instruction below, or check our github page about more details.
|
| 433 |
<details>
|
| 434 |
You may select an example image or upload your image to start, we support 4 prompt types:
|
| 435 |
+
|
| 436 |
+
**default_box**: According to the mask label, automaticly generate the default box prompt, only used for examples.
|
| 437 |
+
|
| 438 |
**automatic**: Automaticly generate text prompt and the corresponding box input with BLIP and Grounding-DINO.
|
| 439 |
|
| 440 |
**scribble_point**: Click an point on the target instance.
|
| 441 |
|
| 442 |
**scribble_box**: Click on two points, the top-left point and the bottom-right point to represent a bounding box of the target instance.
|
|
|
|
|
|
|
| 443 |
|
| 444 |
</details>
|
| 445 |
""")
|
|
|
|
| 447 |
with gr.Row():
|
| 448 |
with gr.Column():
|
| 449 |
input_image = gr.Image(
|
| 450 |
+
source='upload', type="pil", value="example9.jpg", tool="sketch",brush_radius=20)
|
| 451 |
task_type = gr.Dropdown(
|
| 452 |
+
["default_box","automatic", "scribble_point", "scribble_box", "text"], value="default_box", label="task_type")
|
| 453 |
+
text_prompt = gr.Textbox(label="Text Prompt", placeholder="bench .", visible=False)
|
| 454 |
run_button = gr.Button(label="Run")
|
| 455 |
with gr.Accordion("Advanced options", open=False):
|
| 456 |
box_threshold = gr.Slider(
|
| 457 |
+
label="Box Threshold", minimum=0.0, maximum=1.0, value=0.4, step=0.001
|
| 458 |
)
|
| 459 |
text_threshold = gr.Slider(
|
| 460 |
label="Text Threshold", minimum=0.0, maximum=1.0, value=0.25, step=0.001
|
|
|
|
| 487 |
with gr.Column():
|
| 488 |
gr.Examples(["example2.jpg"], inputs=input_image)
|
| 489 |
with gr.Column():
|
| 490 |
+
gr.Examples(["example3.jpg"], inputs=input_image)
|
| 491 |
+
with gr.Column():
|
| 492 |
+
gr.Examples(["example4.jpg"], inputs=input_image)
|
| 493 |
+
with gr.Column():
|
| 494 |
+
gr.Examples(["example5.jpg"], inputs=input_image)
|
| 495 |
+
with gr.Column():
|
| 496 |
+
gr.Examples(["example6.jpg"], inputs=input_image)
|
| 497 |
+
with gr.Column():
|
| 498 |
+
gr.Examples(["example7.jpg"], inputs=input_image)
|
| 499 |
+
with gr.Column():
|
| 500 |
+
gr.Examples(["example8.jpg"], inputs=input_image)
|
| 501 |
+
with gr.Column():
|
| 502 |
+
gr.Examples(["example9.jpg"], inputs=input_image)
|
| 503 |
run_button.click(fn=run_grounded_sam, inputs=[
|
| 504 |
input_image, text_prompt, task_type, box_threshold, text_threshold, iou_threshold], outputs=[gallery1,gallery2])
|
| 505 |
|