Martin Tomov commited on
Commit
6e9745a
Β·
verified Β·
1 Parent(s): 67307b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -3
app.py CHANGED
@@ -178,23 +178,58 @@ def draw_classification_boxes(image_with_insects, detections):
178
  f"{label}: {score:.2f}",
179
  (box.xmin, box.ymin - baseline),
180
  cv2.FONT_HERSHEY_SIMPLEX,
181
- 0.5,
182
  (255, 255, 255),
183
  2
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()
 
178
  f"{label}: {score:.2f}",
179
  (box.xmin, box.ymin - baseline),
180
  cv2.FONT_HERSHEY_SIMPLEX,
181
+ 0.5,
182
  (255, 255, 255),
183
  2
184
  )
185
  return image_with_insects
186
 
187
+ def plot_detections_plotly(image: np.ndarray, detections: List[DetectionResult]) -> str:
188
+ from plotly import graph_objects as go
189
+ import plotly.express as px
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
+ plotly_image_path = plot_detections_plotly(original_image, detections)
228
+ return annotated_image, yellow_background_with_boxes, plotly_image_path
229
 
230
  gr.Interface(
231
  fn=process_image,
232
  inputs=gr.Image(type="pil"),
233
+ outputs=[gr.Image(type="numpy"), gr.Image(type="numpy"), gr.HTML()],
234
  title="🐞 InsectSAM + GroundingDINO Inference"
235
  ).launch()