PrarthanaTS commited on
Commit
abe675c
·
1 Parent(s): 098a2de

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +220 -0
  2. fast_sam_pretrained_try.ipynb +0 -0
  3. requirements.txt.txt +5 -0
app.py ADDED
@@ -0,0 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Fri Oct 6 17:53:27 2023
4
+
5
+ @author: prarthana.ts
6
+ """
7
+
8
+ from ultralytics import YOLO
9
+ import gradio as gr
10
+ import torch
11
+ from utils.tools_gradio import fast_process
12
+ from utils.tools import format_results, box_prompt, point_prompt, text_prompt
13
+ from PIL import ImageDraw
14
+ import numpy as np
15
+
16
+ # Load the pre-trained model
17
+ model = YOLO('./weights/FastSAM.pt')
18
+
19
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
20
+
21
+ # Description
22
+ title = "<center><strong><font size='10'> Fast Segment Anything </font></strong></center>"
23
+
24
+ css = "h1 { text-align: center } .about { text-align: justify; padding-left: 10%; padding-right: 10%; }"
25
+
26
+
27
+ def segment_everything(
28
+ input,
29
+ input_size=1024,
30
+ iou_threshold=0.7,
31
+ conf_threshold=0.25,
32
+ better_quality=False,
33
+ withContours=True,
34
+ use_retina=True,
35
+ text="",
36
+ wider=False,
37
+ mask_random_color=True,
38
+ ):
39
+ input_size = int(input_size)
40
+ w, h = input.size
41
+ scale = input_size / max(w, h)
42
+ new_w = int(w * scale)
43
+ new_h = int(h * scale)
44
+ input = input.resize((new_w, new_h))
45
+
46
+ results = model(input,
47
+ device=device,
48
+ retina_masks=True,
49
+ iou=iou_threshold,
50
+ conf=conf_threshold,
51
+ imgsz=input_size,)
52
+
53
+ if len(text) > 0:
54
+ results = format_results(results[0], 0)
55
+ annotations, _ = text_prompt(results, text, input, device=device, wider=wider)
56
+ annotations = np.array([annotations])
57
+ else:
58
+ annotations = results[0].masks.data
59
+
60
+ fig = fast_process(annotations=annotations,
61
+ image=input,
62
+ device=device,
63
+ scale=(1024 // input_size),
64
+ better_quality=better_quality,
65
+ mask_random_color=mask_random_color,
66
+ bbox=None,
67
+ use_retina=use_retina,
68
+ withContours=withContours,)
69
+ return fig
70
+
71
+
72
+ def segment_with_points(
73
+ input,
74
+ input_size=1024,
75
+ iou_threshold=0.7,
76
+ conf_threshold=0.25,
77
+ better_quality=False,
78
+ withContours=True,
79
+ use_retina=True,
80
+ mask_random_color=True,
81
+ ):
82
+ global global_points
83
+ global global_point_label
84
+
85
+ input_size = int(input_size)
86
+ w, h = input.size
87
+ scale = input_size / max(w, h)
88
+ new_w = int(w * scale)
89
+ new_h = int(h * scale)
90
+ input = input.resize((new_w, new_h))
91
+
92
+ scaled_points = [[int(x * scale) for x in point] for point in global_points]
93
+
94
+ results = model(input,
95
+ device=device,
96
+ retina_masks=True,
97
+ iou=iou_threshold,
98
+ conf=conf_threshold,
99
+ imgsz=input_size,)
100
+
101
+ results = format_results(results[0], 0)
102
+ annotations, _ = point_prompt(results, scaled_points, global_point_label, new_h, new_w)
103
+ annotations = np.array([annotations])
104
+
105
+ fig = fast_process(annotations=annotations,
106
+ image=input,
107
+ device=device,
108
+ scale=(1024 // input_size),
109
+ better_quality=better_quality,
110
+ mask_random_color=mask_random_color,
111
+ bbox=None,
112
+ use_retina=use_retina,
113
+ withContours=withContours,)
114
+
115
+ global_points = []
116
+ global_point_label = []
117
+ return fig, None
118
+
119
+
120
+ def get_points_with_draw(image, label, evt: gr.SelectData):
121
+ global global_points
122
+ global global_point_label
123
+
124
+ x, y = evt.index[0], evt.index[1]
125
+ point_radius, point_color = 15, (255, 255, 0) if label == 'Add Mask' else (255, 0, 255)
126
+ global_points.append([x, y])
127
+ global_point_label.append(1 if label == 'Add Mask' else 0)
128
+
129
+ print(x, y, label == 'Add Mask')
130
+
131
+ draw = ImageDraw.Draw(image)
132
+ draw.ellipse([(x - point_radius, y - point_radius), (x + point_radius, y + point_radius)], fill=point_color)
133
+ return image
134
+
135
+
136
+ cond_img_t = gr.Image(label="Input with text", value="examples/dogs.jpg", type='pil')
137
+ segm_img_t = gr.Image(label="Segmented Image with text", interactive=False, type='pil')
138
+
139
+ global_points = []
140
+ global_point_label = []
141
+
142
+ input_size_slider = gr.components.Slider(minimum=512,
143
+ maximum=1024,
144
+ value=1024,
145
+ step=64,
146
+ label='Input_size',
147
+ info='The model was trained on a size of 1024')
148
+
149
+ with gr.Blocks(css=css, title='Fast Segment Anything') as demo:
150
+ with gr.Row():
151
+ with gr.Column(scale=1):
152
+ # Title
153
+ gr.Markdown(title)
154
+
155
+ with gr.Tab("Text mode"):
156
+ # Images
157
+ with gr.Row(variant="panel"):
158
+ with gr.Column(scale=1):
159
+ cond_img_t.render()
160
+
161
+ with gr.Column(scale=1):
162
+ segm_img_t.render()
163
+
164
+ # Submit & Clear
165
+ with gr.Row():
166
+ with gr.Column():
167
+ input_size_slider_t = gr.components.Slider(minimum=512,
168
+ maximum=1024,
169
+ value=1024,
170
+ step=64,
171
+ label='Input_size',
172
+ info='Our model was trained on a size of 1024')
173
+ with gr.Row():
174
+ with gr.Column():
175
+ contour_check = gr.Checkbox(value=True, label='withContours', info='draw the edges of the masks')
176
+ text_box = gr.Textbox(label="text prompt", value="a black dog")
177
+
178
+ with gr.Column():
179
+ segment_btn_t = gr.Button("Segment with text", variant='primary')
180
+ clear_btn_t = gr.Button("Clear", variant="secondary")
181
+
182
+ gr.Markdown("Try some of the examples below ⬇️")
183
+ gr.Examples(examples=[["examples/dogs.jpg"], ["examples/fruits.jpg"], ["examples/flowers.jpg"], ["examples/dog2.jpg"], ["examples/cat.jpg"], ["examples/person.jpg"]],
184
+ inputs=[cond_img_t],
185
+ examples_per_page=6)
186
+
187
+ with gr.Column():
188
+ with gr.Accordion("Advanced options", open=False):
189
+ iou_threshold = gr.Slider(0.1, 0.9, 0.7, step=0.1, label='iou', info='iou threshold for filtering the annotations')
190
+ conf_threshold = gr.Slider(0.1, 0.9, 0.25, step=0.05, label='conf', info='object confidence threshold')
191
+ with gr.Row():
192
+ mor_check = gr.Checkbox(value=False, label='better_visual_quality', info='better quality using morphologyEx')
193
+ retina_check = gr.Checkbox(value=True, label='use_retina', info='draw high-resolution segmentation masks')
194
+ wider_check = gr.Checkbox(value=False, label='wider', info='wider result')
195
+
196
+ segment_btn_t.click(segment_everything,
197
+ inputs=[
198
+ cond_img_t,
199
+ input_size_slider_t,
200
+ iou_threshold,
201
+ conf_threshold,
202
+ mor_check,
203
+ contour_check,
204
+ retina_check,
205
+ text_box,
206
+ wider_check,
207
+ ],
208
+ outputs=segm_img_t)
209
+
210
+ def clear():
211
+ return None, None
212
+
213
+ def clear_text():
214
+ return None, None, None
215
+
216
+ # clear_btn_p.click(clear, outputs=[cond_img_p, segm_img_p])
217
+ clear_btn_t.click(clear_text, outputs=[text_box])
218
+
219
+ demo.queue()
220
+ demo.launch()
fast_sam_pretrained_try.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ matplotlib==3.2.2
2
+ numpy
3
+ opencv-python
4
+ git+https://github.com/openai/CLIP.git
5
+ ultralytics==8.0.121