forked from IDEA-Research/Grounded-SAM-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grounded_sam2_florence2_image_demo.py
executable file
·657 lines (562 loc) · 23.5 KB
/
grounded_sam2_florence2_image_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
import os
import cv2
import torch
import argparse
import numpy as np
import supervision as sv
from PIL import Image
from sam2.build_sam import build_sam2
from sam2.sam2_image_predictor import SAM2ImagePredictor
from transformers import AutoProcessor, AutoModelForCausalLM
from utils.supervision_utils import CUSTOM_COLOR_MAP
"""
Define Some Hyperparam
"""
TASK_PROMPT = {
"caption": "<CAPTION>",
"detailed_caption": "<DETAILED_CAPTION>",
"more_detailed_caption": "<MORE_DETAILED_CAPTION",
"object_detection": "<OD>",
"dense_region_caption": "<DENSE_REGION_CAPTION>",
"region_proposal": "<REGION_PROPOSAL>",
"phrase_grounding": "<CAPTION_TO_PHRASE_GROUNDING>",
"referring_expression_segmentation": "<REFERRING_EXPRESSION_SEGMENTATION>",
"region_to_segmentation": "<REGION_TO_SEGMENTATION>",
"open_vocabulary_detection": "<OPEN_VOCABULARY_DETECTION>",
"region_to_category": "<REGION_TO_CATEGORY>",
"region_to_description": "<REGION_TO_DESCRIPTION>",
"ocr": "<OCR>",
"ocr_with_region": "<OCR_WITH_REGION>",
}
OUTPUT_DIR = "./outputs"
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR, exist_ok=True)
"""
Init Florence-2 and SAM 2 Model
"""
FLORENCE2_MODEL_ID = "microsoft/Florence-2-large"
SAM2_CHECKPOINT = "./checkpoints/sam2.1_hiera_large.pt"
SAM2_CONFIG = "configs/sam2.1/sam2.1_hiera_l.yaml"
# environment settings
# use bfloat16
torch.autocast(device_type="cuda", dtype=torch.bfloat16).__enter__()
if torch.cuda.get_device_properties(0).major >= 8:
# turn on tfloat32 for Ampere GPUs (https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices)
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
# build florence-2
florence2_model = AutoModelForCausalLM.from_pretrained(FLORENCE2_MODEL_ID, trust_remote_code=True, torch_dtype='auto').eval().to(device)
florence2_processor = AutoProcessor.from_pretrained(FLORENCE2_MODEL_ID, trust_remote_code=True)
# build sam 2
sam2_model = build_sam2(SAM2_CONFIG, SAM2_CHECKPOINT, device=device)
sam2_predictor = SAM2ImagePredictor(sam2_model)
def run_florence2(task_prompt, text_input, model, processor, image):
assert model is not None, "You should pass the init florence-2 model here"
assert processor is not None, "You should set florence-2 processor here"
device = model.device
if text_input is None:
prompt = task_prompt
else:
prompt = task_prompt + text_input
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch.float16)
generated_ids = model.generate(
input_ids=inputs["input_ids"].to(device),
pixel_values=inputs["pixel_values"].to(device),
max_new_tokens=1024,
early_stopping=False,
do_sample=False,
num_beams=3,
)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
parsed_answer = processor.post_process_generation(
generated_text,
task=task_prompt,
image_size=(image.width, image.height)
)
return parsed_answer
"""
We support a set of pipelines built by Florence-2 + SAM 2
"""
"""
Pipeline-1: Object Detection + Segmentation
"""
def object_detection_and_segmentation(
florence2_model,
florence2_processor,
sam2_predictor,
image_path,
task_prompt="<OD>",
text_input=None,
output_dir=OUTPUT_DIR
):
assert text_input is None, "Text input should be None when calling object detection pipeline."
# run florence-2 object detection in demo
image = Image.open(image_path).convert("RGB")
results = run_florence2(task_prompt, text_input, florence2_model, florence2_processor, image)
""" Florence-2 Object Detection Output Format
{'<OD>':
{
'bboxes':
[
[33.599998474121094, 159.59999084472656, 596.7999877929688, 371.7599792480469],
[454.0799865722656, 96.23999786376953, 580.7999877929688, 261.8399963378906],
[224.95999145507812, 86.15999603271484, 333.7599792480469, 164.39999389648438],
[449.5999755859375, 276.239990234375, 554.5599975585938, 370.3199768066406],
[91.19999694824219, 280.0799865722656, 198.0800018310547, 370.3199768066406]
],
'labels': ['car', 'door', 'door', 'wheel', 'wheel']
}
}
"""
results = results[task_prompt]
# parse florence-2 detection results
input_boxes = np.array(results["bboxes"])
class_names = results["labels"]
class_ids = np.array(list(range(len(class_names))))
# predict mask with SAM 2
sam2_predictor.set_image(np.array(image))
masks, scores, logits = sam2_predictor.predict(
point_coords=None,
point_labels=None,
box=input_boxes,
multimask_output=False,
)
if masks.ndim == 4:
masks = masks.squeeze(1)
# specify labels
labels = [
f"{class_name}" for class_name in class_names
]
# visualization results
img = cv2.imread(image_path)
detections = sv.Detections(
xyxy=input_boxes,
mask=masks.astype(bool),
class_id=class_ids
)
box_annotator = sv.BoxAnnotator()
annotated_frame = box_annotator.annotate(scene=img.copy(), detections=detections)
label_annotator = sv.LabelAnnotator()
annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=detections, labels=labels)
cv2.imwrite(os.path.join(output_dir, "grounded_sam2_florence2_det_annotated_image.jpg"), annotated_frame)
mask_annotator = sv.MaskAnnotator()
annotated_frame = mask_annotator.annotate(scene=annotated_frame, detections=detections)
cv2.imwrite(os.path.join(output_dir, "grounded_sam2_florence2_det_image_with_mask.jpg"), annotated_frame)
print(f'Successfully save annotated image to "{output_dir}"')
"""
Pipeline 2: Dense Region Caption + Segmentation
"""
def dense_region_caption_and_segmentation(
florence2_model,
florence2_processor,
sam2_predictor,
image_path,
task_prompt="<DENSE_REGION_CAPTION>",
text_input=None,
output_dir=OUTPUT_DIR
):
assert text_input is None, "Text input should be None when calling dense region caption pipeline."
# run florence-2 object detection in demo
image = Image.open(image_path).convert("RGB")
results = run_florence2(task_prompt, text_input, florence2_model, florence2_processor, image)
""" Florence-2 Object Detection Output Format
{'<DENSE_REGION_CAPTION>':
{
'bboxes':
[
[33.599998474121094, 159.59999084472656, 596.7999877929688, 371.7599792480469],
[454.0799865722656, 96.23999786376953, 580.7999877929688, 261.8399963378906],
[224.95999145507812, 86.15999603271484, 333.7599792480469, 164.39999389648438],
[449.5999755859375, 276.239990234375, 554.5599975585938, 370.3199768066406],
[91.19999694824219, 280.0799865722656, 198.0800018310547, 370.3199768066406]
],
'labels': ['turquoise Volkswagen Beetle', 'wooden double doors with metal handles', 'wheel', 'wheel', 'door']
}
}
"""
results = results[task_prompt]
# parse florence-2 detection results
input_boxes = np.array(results["bboxes"])
class_names = results["labels"]
class_ids = np.array(list(range(len(class_names))))
# predict mask with SAM 2
sam2_predictor.set_image(np.array(image))
masks, scores, logits = sam2_predictor.predict(
point_coords=None,
point_labels=None,
box=input_boxes,
multimask_output=False,
)
if masks.ndim == 4:
masks = masks.squeeze(1)
# specify labels
labels = [
f"{class_name}" for class_name in class_names
]
# visualization results
img = cv2.imread(image_path)
detections = sv.Detections(
xyxy=input_boxes,
mask=masks.astype(bool),
class_id=class_ids
)
box_annotator = sv.BoxAnnotator()
annotated_frame = box_annotator.annotate(scene=img.copy(), detections=detections)
label_annotator = sv.LabelAnnotator()
annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=detections, labels=labels)
cv2.imwrite(os.path.join(output_dir, "grounded_sam2_florence2_dense_region_cap_annotated_image.jpg"), annotated_frame)
mask_annotator = sv.MaskAnnotator()
annotated_frame = mask_annotator.annotate(scene=annotated_frame, detections=detections)
cv2.imwrite(os.path.join(output_dir, "grounded_sam2_florence2_dense_region_cap_image_with_mask.jpg"), annotated_frame)
print(f'Successfully save annotated image to "{output_dir}"')
"""
Pipeline 3: Region Proposal + Segmentation
"""
def region_proposal_and_segmentation(
florence2_model,
florence2_processor,
sam2_predictor,
image_path,
task_prompt="<REGION_PROPOSAL>",
text_input=None,
output_dir=OUTPUT_DIR
):
assert text_input is None, "Text input should be None when calling region proposal pipeline."
# run florence-2 object detection in demo
image = Image.open(image_path).convert("RGB")
results = run_florence2(task_prompt, text_input, florence2_model, florence2_processor, image)
""" Florence-2 Object Detection Output Format
{'<REGION_PROPOSAL>':
{
'bboxes':
[
[33.599998474121094, 159.59999084472656, 596.7999877929688, 371.7599792480469],
[454.0799865722656, 96.23999786376953, 580.7999877929688, 261.8399963378906],
[224.95999145507812, 86.15999603271484, 333.7599792480469, 164.39999389648438],
[449.5999755859375, 276.239990234375, 554.5599975585938, 370.3199768066406],
[91.19999694824219, 280.0799865722656, 198.0800018310547, 370.3199768066406]
],
'labels': ['', '', '', '', '', '', '']
}
}
"""
results = results[task_prompt]
# parse florence-2 detection results
input_boxes = np.array(results["bboxes"])
class_names = results["labels"]
class_ids = np.array(list(range(len(class_names))))
# predict mask with SAM 2
sam2_predictor.set_image(np.array(image))
masks, scores, logits = sam2_predictor.predict(
point_coords=None,
point_labels=None,
box=input_boxes,
multimask_output=False,
)
if masks.ndim == 4:
masks = masks.squeeze(1)
# specify labels
labels = [
f"region_{idx}" for idx, class_name in enumerate(class_names)
]
# visualization results
img = cv2.imread(image_path)
detections = sv.Detections(
xyxy=input_boxes,
mask=masks.astype(bool),
class_id=class_ids
)
box_annotator = sv.BoxAnnotator()
annotated_frame = box_annotator.annotate(scene=img.copy(), detections=detections)
label_annotator = sv.LabelAnnotator()
annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=detections, labels=labels)
cv2.imwrite(os.path.join(output_dir, "grounded_sam2_florence2_region_proposal.jpg"), annotated_frame)
mask_annotator = sv.MaskAnnotator()
annotated_frame = mask_annotator.annotate(scene=annotated_frame, detections=detections)
cv2.imwrite(os.path.join(output_dir, "grounded_sam2_florence2_region_proposal_with_mask.jpg"), annotated_frame)
print(f'Successfully save annotated image to "{output_dir}"')
"""
Pipeline 4: Phrase Grounding + Segmentation
"""
def phrase_grounding_and_segmentation(
florence2_model,
florence2_processor,
sam2_predictor,
image_path,
task_prompt="<CAPTION_TO_PHRASE_GROUNDING>",
text_input=None,
output_dir=OUTPUT_DIR
):
# run florence-2 object detection in demo
image = Image.open(image_path).convert("RGB")
results = run_florence2(task_prompt, text_input, florence2_model, florence2_processor, image)
""" Florence-2 Object Detection Output Format
{'<CAPTION_TO_PHRASE_GROUNDING>':
{
'bboxes':
[
[34.23999786376953, 159.1199951171875, 582.0800170898438, 374.6399841308594],
[1.5999999046325684, 4.079999923706055, 639.0399780273438, 305.03997802734375]
],
'labels': ['A green car', 'a yellow building']
}
}
"""
assert text_input is not None, "Text input should not be None when calling phrase grounding pipeline."
results = results[task_prompt]
# parse florence-2 detection results
input_boxes = np.array(results["bboxes"])
class_names = results["labels"]
class_ids = np.array(list(range(len(class_names))))
# predict mask with SAM 2
sam2_predictor.set_image(np.array(image))
masks, scores, logits = sam2_predictor.predict(
point_coords=None,
point_labels=None,
box=input_boxes,
multimask_output=False,
)
if masks.ndim == 4:
masks = masks.squeeze(1)
# specify labels
labels = [
f"{class_name}" for class_name in class_names
]
# visualization results
img = cv2.imread(image_path)
detections = sv.Detections(
xyxy=input_boxes,
mask=masks.astype(bool),
class_id=class_ids
)
box_annotator = sv.BoxAnnotator()
annotated_frame = box_annotator.annotate(scene=img.copy(), detections=detections)
label_annotator = sv.LabelAnnotator()
annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=detections, labels=labels)
cv2.imwrite(os.path.join(output_dir, "grounded_sam2_florence2_phrase_grounding.jpg"), annotated_frame)
mask_annotator = sv.MaskAnnotator()
annotated_frame = mask_annotator.annotate(scene=annotated_frame, detections=detections)
cv2.imwrite(os.path.join(output_dir, "grounded_sam2_florence2_phrase_grounding_with_mask.jpg"), annotated_frame)
print(f'Successfully save annotated image to "{output_dir}"')
"""
Pipeline 5: Referring Expression Segmentation
Note that Florence-2 directly support referring segmentation with polygon output format, which may be not that accurate,
therefore we try to decode box from polygon and use SAM 2 for mask prediction
"""
def referring_expression_segmentation(
florence2_model,
florence2_processor,
sam2_predictor,
image_path,
task_prompt="<REFERRING_EXPRESSION_SEGMENTATION>",
text_input=None,
output_dir=OUTPUT_DIR
):
# run florence-2 object detection in demo
image = Image.open(image_path).convert("RGB")
results = run_florence2(task_prompt, text_input, florence2_model, florence2_processor, image)
""" Florence-2 Object Detection Output Format
{'<REFERRING_EXPRESSION_SEGMENTATION>':
{
'polygons': [[[...]]]
'labels': ['']
}
}
"""
assert text_input is not None, "Text input should not be None when calling referring segmentation pipeline."
results = results[task_prompt]
# parse florence-2 detection results
polygon_points = np.array(results["polygons"][0], dtype=np.int32).reshape(-1, 2)
class_names = [text_input]
class_ids = np.array(list(range(len(class_names))))
# parse polygon format to mask
img_width, img_height = image.size[0], image.size[1]
florence2_mask = np.zeros((img_height, img_width), dtype=np.uint8)
if len(polygon_points) < 3:
print("Invalid polygon:", polygon_points)
exit()
cv2.fillPoly(florence2_mask, [polygon_points], 1)
if florence2_mask.ndim == 2:
florence2_mask = florence2_mask[None]
# compute bounding box based on polygon points
x_min = np.min(polygon_points[:, 0])
y_min = np.min(polygon_points[:, 1])
x_max = np.max(polygon_points[:, 0])
y_max = np.max(polygon_points[:, 1])
input_boxes = np.array([[x_min, y_min, x_max, y_max]])
# predict mask with SAM 2
sam2_predictor.set_image(np.array(image))
sam2_masks, scores, logits = sam2_predictor.predict(
point_coords=None,
point_labels=None,
box=input_boxes,
multimask_output=False,
)
if sam2_masks.ndim == 4:
sam2_masks = sam2_masks.squeeze(1)
# specify labels
labels = [
f"{class_name}" for class_name in class_names
]
# visualization florence2 mask
img = cv2.imread(image_path)
detections = sv.Detections(
xyxy=input_boxes,
mask=florence2_mask.astype(bool),
class_id=class_ids
)
box_annotator = sv.BoxAnnotator()
annotated_frame = box_annotator.annotate(scene=img.copy(), detections=detections)
label_annotator = sv.LabelAnnotator()
annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=detections, labels=labels)
cv2.imwrite(os.path.join(output_dir, "florence2_referring_segmentation_box.jpg"), annotated_frame)
mask_annotator = sv.MaskAnnotator()
annotated_frame = mask_annotator.annotate(scene=annotated_frame, detections=detections)
cv2.imwrite(os.path.join(output_dir, "florence2_referring_segmentation_box_with_mask.jpg"), annotated_frame)
print(f'Successfully save florence-2 annotated image to "{output_dir}"')
# visualize sam2 mask
img = cv2.imread(image_path)
detections = sv.Detections(
xyxy=input_boxes,
mask=sam2_masks.astype(bool),
class_id=class_ids
)
box_annotator = sv.BoxAnnotator()
annotated_frame = box_annotator.annotate(scene=img.copy(), detections=detections)
label_annotator = sv.LabelAnnotator()
annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=detections, labels=labels)
cv2.imwrite(os.path.join(output_dir, "grounded_sam2_florence2_referring_box.jpg"), annotated_frame)
mask_annotator = sv.MaskAnnotator()
annotated_frame = mask_annotator.annotate(scene=annotated_frame, detections=detections)
cv2.imwrite(os.path.join(output_dir, "grounded_sam2_florence2_referring_box_with_sam2_mask.jpg"), annotated_frame)
print(f'Successfully save sam2 annotated image to "{output_dir}"')
"""
Pipeline 6: Open-Vocabulary Detection + Segmentation
"""
def open_vocabulary_detection_and_segmentation(
florence2_model,
florence2_processor,
sam2_predictor,
image_path,
task_prompt="<OPEN_VOCABULARY_DETECTION>",
text_input=None,
output_dir=OUTPUT_DIR
):
# run florence-2 object detection in demo
image = Image.open(image_path).convert("RGB")
results = run_florence2(task_prompt, text_input, florence2_model, florence2_processor, image)
""" Florence-2 Open-Vocabulary Detection Output Format
{'<OPEN_VOCABULARY_DETECTION>':
{
'bboxes':
[
[34.23999786376953, 159.1199951171875, 582.0800170898438, 374.6399841308594]
],
'bboxes_labels': ['A green car'],
'polygons': [],
'polygons_labels': []
}
}
"""
assert text_input is not None, "Text input should not be None when calling open-vocabulary detection pipeline."
results = results[task_prompt]
# parse florence-2 detection results
input_boxes = np.array(results["bboxes"])
print(results)
class_names = results["bboxes_labels"]
class_ids = np.array(list(range(len(class_names))))
# predict mask with SAM 2
sam2_predictor.set_image(np.array(image))
masks, scores, logits = sam2_predictor.predict(
point_coords=None,
point_labels=None,
box=input_boxes,
multimask_output=False,
)
if masks.ndim == 4:
masks = masks.squeeze(1)
# specify labels
labels = [
f"{class_name}" for class_name in class_names
]
# visualization results
img = cv2.imread(image_path)
detections = sv.Detections(
xyxy=input_boxes,
mask=masks.astype(bool),
class_id=class_ids
)
box_annotator = sv.BoxAnnotator()
annotated_frame = box_annotator.annotate(scene=img.copy(), detections=detections)
label_annotator = sv.LabelAnnotator()
annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=detections, labels=labels)
cv2.imwrite(os.path.join(output_dir, "grounded_sam2_florence2_open_vocabulary_detection.jpg"), annotated_frame)
mask_annotator = sv.MaskAnnotator()
annotated_frame = mask_annotator.annotate(scene=annotated_frame, detections=detections)
cv2.imwrite(os.path.join(output_dir, "grounded_sam2_florence2_open_vocabulary_detection_with_mask.jpg"), annotated_frame)
print(f'Successfully save annotated image to "{output_dir}"')
if __name__ == "__main__":
parser = argparse.ArgumentParser("Grounded SAM 2 Florence-2 Demos", add_help=True)
parser.add_argument("--image_path", type=str, default="./notebooks/images/cars.jpg", required=True, help="path to image file")
parser.add_argument("--pipeline", type=str, default="object_detection_segmentation", required=True, help="path to image file")
parser.add_argument("--text_input", type=str, default=None, required=False, help="path to image file")
args = parser.parse_args()
IMAGE_PATH = args.image_path
PIPELINE = args.pipeline
INPUT_TEXT = args.text_input
print(f"Running pipeline: {PIPELINE} now.")
if PIPELINE == "object_detection_segmentation":
# pipeline-1: detection + segmentation
object_detection_and_segmentation(
florence2_model=florence2_model,
florence2_processor=florence2_processor,
sam2_predictor=sam2_predictor,
image_path=IMAGE_PATH
)
elif PIPELINE == "dense_region_caption_segmentation":
# pipeline-2: dense region caption + segmentation
dense_region_caption_and_segmentation(
florence2_model=florence2_model,
florence2_processor=florence2_processor,
sam2_predictor=sam2_predictor,
image_path=IMAGE_PATH
)
elif PIPELINE == "region_proposal_segmentation":
# pipeline-3: dense region caption + segmentation
region_proposal_and_segmentation(
florence2_model=florence2_model,
florence2_processor=florence2_processor,
sam2_predictor=sam2_predictor,
image_path=IMAGE_PATH
)
elif PIPELINE == "phrase_grounding_segmentation":
# pipeline-4: phrase grounding + segmentation
phrase_grounding_and_segmentation(
florence2_model=florence2_model,
florence2_processor=florence2_processor,
sam2_predictor=sam2_predictor,
image_path=IMAGE_PATH,
text_input=INPUT_TEXT
)
elif PIPELINE == "referring_expression_segmentation":
# pipeline-5: referring segmentation + segmentation
referring_expression_segmentation(
florence2_model=florence2_model,
florence2_processor=florence2_processor,
sam2_predictor=sam2_predictor,
image_path=IMAGE_PATH,
text_input=INPUT_TEXT
)
elif PIPELINE == "open_vocabulary_detection_segmentation":
# pipeline-6: open-vocabulary detection + segmentation
open_vocabulary_detection_and_segmentation(
florence2_model=florence2_model,
florence2_processor=florence2_processor,
sam2_predictor=sam2_predictor,
image_path=IMAGE_PATH,
text_input=INPUT_TEXT
)
else:
raise NotImplementedError(f"Pipeline: {args.pipeline} is not implemented at this time")