EduardoPacheco commited on
Commit
c73bc62
1 Parent(s): f3f82df

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +63 -0
  2. input_image.jpeg +0 -0
  3. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from grounded_sam.inference import grounded_segmentation
4
+ from grounded_sam.plot import plot_detections, plot_detections_plotly
5
+
6
+ def app_fn(
7
+ image: gr.Image,
8
+ labels: str,
9
+ threshold: float,
10
+ bounding_box_selection: bool
11
+ ) -> str:
12
+ labels = labels.split("\n")
13
+ labels = [label if label.endswith(".") else label + "." for label in labels]
14
+ image_array, detections = grounded_segmentation(image, labels, threshold, True)
15
+ fig_detection = plot_detections_plotly(image_array, detections, bounding_box_selection)
16
+
17
+ return fig_detection
18
+
19
+ if __name__=="__main__":
20
+ title = "Grounding SAM - Text-to-Segmentation Model"
21
+ with gr.Blocks(title=title) as demo:
22
+ gr.Markdown(f"# {title}")
23
+ gr.Markdown(
24
+ """
25
+ Grounded SAM is a text-to-segmentation model that generates segmentation masks from natural language descriptions.
26
+ This demo uses Grounding DINO in tandem with SAM to generate segmentation masks from text.
27
+ The workflow is as follows:
28
+ 1. Select text labels to generate bounding boxes with Grounding DINO.
29
+ 2. Prompt the SAM model to generate segmentation masks from the bounding boxes.
30
+ 3. Refine the masks if needed.
31
+ 4. Visualize the segmentation masks.
32
+
33
+
34
+ ### Notes
35
+ - To pass multiple labels, separate them by a new line.
36
+ - The model may take a few seconds to generate the segmentation masks as we need to run through two models.
37
+ - The refinement is done by default by converting the mask to a polygon and back to a mask with openCV.
38
+ - I use in here a concise implementation, but you can find the full code at [GitHub](https://github.com/EduardoPach/grounded-sam)
39
+ """
40
+ )
41
+ with gr.Row():
42
+ threshold = gr.Slider(minimum=0, maximum=1, value=0.3, step=0.05, label="Box Threshold")
43
+ labels = gr.Textbox(lines=2, max_lines=5, label="Labels")
44
+ bounding_box_selection = gr.Checkbox(label="Allow Box Selection")
45
+ btn = gr.Button()
46
+ with gr.Row():
47
+ img = gr.Image(type="pil")
48
+ fig = gr.Plot(label="Segmentation Mask")
49
+
50
+ btn.click(fn=app_fn, inputs=[img, labels, threshold, bounding_box_selection], outputs=[fig])
51
+
52
+ gr.Examples(
53
+ [
54
+ ["input_image.jpeg", "a person.\na mountain.", 0.25, 0.25],
55
+ ],
56
+ inputs = [img, labels, threshold, bounding_box_selection],
57
+ outputs = [fig],
58
+ fn=app_fn,
59
+ cache_examples=True,
60
+ label='Try this example input!'
61
+ )
62
+
63
+ demo.launch()
input_image.jpeg ADDED
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ git+https://github.com/EduardoPach/grounded-sam.git