Martin Tomov commited on
Commit
ce4fb35
β€’
1 Parent(s): e72d68c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -20
app.py CHANGED
@@ -43,8 +43,7 @@ class DetectionResult:
43
  ymin=detection_dict['box']['ymin'],
44
  xmax=detection_dict['box']['xmax'],
45
  ymax=detection_dict['box']['ymax']
46
- ),
47
- mask=detection_dict.get('mask')
48
  )
49
 
50
  def annotate(image: Union[Image.Image, np.ndarray], detection_results: List[DetectionResult]) -> np.ndarray:
@@ -52,12 +51,20 @@ def annotate(image: Union[Image.Image, np.ndarray], detection_results: List[Dete
52
  image_cv2 = cv2.cvtColor(image_cv2, cv2.COLOR_RGB2BGR)
53
 
54
  for detection in detection_results:
 
 
 
55
  mask = detection.mask
 
 
 
 
 
 
56
  if mask is not None:
57
  mask_uint8 = (mask * 255).astype(np.uint8)
58
  contours, _ = cv2.findContours(mask_uint8, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
59
- # Drawing only the mask contours without any bounding box or label
60
- cv2.drawContours(image_cv2, contours, -1, (0, 0, 0), 2) # Black color for contours
61
 
62
  return cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB)
63
 
@@ -188,25 +195,25 @@ def detections_to_json(detections):
188
 
189
  def process_image(image, include_json):
190
  labels = ["insect"]
191
- original_image, detections = grounded_segmentation(image, labels, threshold=0.3, polygon_refinement=True)
192
- yellow_background_with_insects = create_yellow_background_with_insects(np.array(original_image), detections)
193
- if include_json:
194
- detections_json = detections_to_json(detections)
195
- json_output_path = "insect_detections.json"
196
- with open(json_output_path, β€˜w’) as json_file:
197
- json.dump(detections_json, json_file, indent=4)
198
- return yellow_background_with_insects, json.dumps(detections_json, separators=(’,’, β€˜:’))
199
- else:
200
- return yellow_background_with_insects, None
201
 
202
  examples = [
203
- [β€œflower-night.jpg”]
204
  ]
205
 
206
  gr.Interface(
207
- fn=process_image,
208
- inputs=[gr.Image(type=β€œpil”), gr.Checkbox(label=β€œInclude JSON”, value=False)],
209
- outputs=[gr.Image(type=β€œnumpy”), gr.Textbox()],
210
- title=β€œInsectSAM πŸžβ€,
211
- examples=examples
212
  ).launch()
 
43
  ymin=detection_dict['box']['ymin'],
44
  xmax=detection_dict['box']['xmax'],
45
  ymax=detection_dict['box']['ymax']
46
+ )
 
47
  )
48
 
49
  def annotate(image: Union[Image.Image, np.ndarray], detection_results: List[DetectionResult]) -> np.ndarray:
 
51
  image_cv2 = cv2.cvtColor(image_cv2, cv2.COLOR_RGB2BGR)
52
 
53
  for detection in detection_results:
54
+ label = detection.label
55
+ score = detection.score
56
+ box = detection.box
57
  mask = detection.mask
58
+ color = np.random.randint(0, 256, size=3).tolist()
59
+
60
+ cv2.rectangle(image_cv2, (box.xmin, box.ymin), (box.xmax, box.ymax), color, 2)
61
+ cv2.putText(image_cv2, f'{label}: {score:.2f}', (box.xmin, box.ymin - 10),
62
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
63
+
64
  if mask is not None:
65
  mask_uint8 = (mask * 255).astype(np.uint8)
66
  contours, _ = cv2.findContours(mask_uint8, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
67
+ cv2.drawContours(image_cv2, contours, -1, color, 2)
 
68
 
69
  return cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB)
70
 
 
195
 
196
  def process_image(image, include_json):
197
  labels = ["insect"]
198
+ original_image, detections = grounded_segmentation(image, labels, threshold=0.3, polygon_refinement=True)
199
+ yellow_background_with_insects = create_yellow_background_with_insects(np.array(original_image), detections)
200
+ if include_json:
201
+ detections_json = detections_to_json(detections)
202
+ json_output_path = "insect_detections.json"
203
+ with open(json_output_path, 'w') as json_file:
204
+ json.dump(detections_json, json_file, indent=4)
205
+ return yellow_background_with_insects, json.dumps(detections_json, separators=(',', ':'))
206
+ else:
207
+ return yellow_background_with_insects, None
208
 
209
  examples = [
210
+ ["flower-night.jpg"]
211
  ]
212
 
213
  gr.Interface(
214
+ fn=process_image,
215
+ inputs=[gr.Image(type="pil"), gr.Checkbox(label="Include JSON", value=False)],
216
+ outputs=[gr.Image(type="numpy"), gr.Textbox()],
217
+ title="InsectSAM 🐞",
218
+ examples=examples
219
  ).launch()