Spaces:
Running
on
Zero
Running
on
Zero
Martin Tomov
commited on
reverse app.py to stable version
Browse files
app.py
CHANGED
@@ -13,8 +13,6 @@ import matplotlib.pyplot as plt
|
|
13 |
from transformers import AutoModelForMaskGeneration, AutoProcessor, pipeline
|
14 |
import gradio as gr
|
15 |
import spaces
|
16 |
-
from plotly import graph_objects as go
|
17 |
-
import plotly.express as px
|
18 |
|
19 |
@dataclass
|
20 |
class BoundingBox:
|
@@ -186,52 +184,17 @@ def draw_classification_boxes(image_with_insects, detections):
|
|
186 |
)
|
187 |
return image_with_insects
|
188 |
|
189 |
-
def plot_detections_plotly(image: np.ndarray, detections: List[DetectionResult]) -> str:
|
190 |
-
fig = px.imshow(image)
|
191 |
-
class_colors = {i: f'rgb({random.randint(0, 255)}, {random.randint(0, 255)}, {random.randint(0, 255)})' for i in range(len(detections))}
|
192 |
-
for idx, detection in enumerate(detections):
|
193 |
-
label = detection.label
|
194 |
-
box = detection.box
|
195 |
-
score = detection.score
|
196 |
-
mask = detection.mask
|
197 |
-
polygon = mask_to_polygon(mask)
|
198 |
-
fig.add_trace(go.Scatter(
|
199 |
-
x=[point[0] for point in polygon] + [polygon[0][0]],
|
200 |
-
y=[point[1] for point in polygon] + [polygon[0][1]],
|
201 |
-
mode='lines',
|
202 |
-
line=dict(color=class_colors[idx], width=2),
|
203 |
-
fill='toself',
|
204 |
-
name=f"{label}: {score:.2f}"
|
205 |
-
))
|
206 |
-
xmin, ymin, xmax, ymax = box.xyxy
|
207 |
-
fig.add_shape(
|
208 |
-
type="rect",
|
209 |
-
x0=xmin, y0=ymin, x1=xmax, y1=ymax,
|
210 |
-
line=dict(color=class_colors[idx])
|
211 |
-
)
|
212 |
-
fig.add_annotation(
|
213 |
-
x=(xmin + xmax) // 2, y=(ymin + ymax) // 2,
|
214 |
-
text=f"{label}: {score:.2f}",
|
215 |
-
)
|
216 |
-
fig.update_layout(xaxis=dict(visible=False), yaxis=dict(visible=False))
|
217 |
-
file_path = "/tmp/plotly_image.html"
|
218 |
-
fig.write_html(file_path)
|
219 |
-
return file_path
|
220 |
-
|
221 |
def process_image(image):
|
222 |
labels = ["insect"]
|
223 |
original_image, detections = grounded_segmentation(image, labels, threshold=0.3, polygon_refinement=True)
|
224 |
annotated_image = plot_detections(original_image, detections)
|
225 |
yellow_background_with_insects = create_yellow_background_with_insects(np.array(original_image), detections)
|
226 |
yellow_background_with_boxes = draw_classification_boxes(yellow_background_with_insects.copy(), detections)
|
227 |
-
|
228 |
-
with open(plotly_image_path, 'r') as file:
|
229 |
-
plotly_html = file.read()
|
230 |
-
return annotated_image, yellow_background_with_boxes, plotly_html
|
231 |
|
232 |
gr.Interface(
|
233 |
fn=process_image,
|
234 |
inputs=gr.Image(type="pil"),
|
235 |
-
outputs=[gr.Image(type="numpy"), gr.Image(type="numpy")
|
236 |
-
title="π InsectSAM + GroundingDINO Inference"
|
237 |
-
).launch()
|
|
|
13 |
from transformers import AutoModelForMaskGeneration, AutoProcessor, pipeline
|
14 |
import gradio as gr
|
15 |
import spaces
|
|
|
|
|
16 |
|
17 |
@dataclass
|
18 |
class BoundingBox:
|
|
|
184 |
)
|
185 |
return image_with_insects
|
186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
def process_image(image):
|
188 |
labels = ["insect"]
|
189 |
original_image, detections = grounded_segmentation(image, labels, threshold=0.3, polygon_refinement=True)
|
190 |
annotated_image = plot_detections(original_image, detections)
|
191 |
yellow_background_with_insects = create_yellow_background_with_insects(np.array(original_image), detections)
|
192 |
yellow_background_with_boxes = draw_classification_boxes(yellow_background_with_insects.copy(), detections)
|
193 |
+
return annotated_image, yellow_background_with_boxes
|
|
|
|
|
|
|
194 |
|
195 |
gr.Interface(
|
196 |
fn=process_image,
|
197 |
inputs=gr.Image(type="pil"),
|
198 |
+
outputs=[gr.Image(type="numpy"), gr.Image(type="numpy")],
|
199 |
+
title="π InsectSAM + GroundingDINO Inference",
|
200 |
+
).launch()
|