Martin Tomov commited on
Commit
5977b5c
Β·
verified Β·
1 Parent(s): e886b5d

experimenting with extraction of bbx

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -59,13 +59,14 @@ def annotate(image: Union[Image.Image, np.ndarray], detection_results: List[Dete
59
 
60
  if mask is not None:
61
  mask_uint8 = (mask * 255).astype(np.uint8)
62
- contours, _ = cv2.findContours(mask_uint8, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
63
- cv2.drawContours(yellow_background, contours, -1, (0, 0, 0), cv2.FILLED)
64
-
65
- # Extract insect region using mask
66
- insect_region = cv2.bitwise_and(image_cv2, image_cv2, mask=mask)
67
-
68
- yellow_background[mask > 0] = insect_region[mask > 0]
 
69
 
70
  return cv2.cvtColor(yellow_background, cv2.COLOR_BGR2RGB)
71
 
@@ -217,4 +218,4 @@ gr.Interface(
217
  outputs=[gr.Image(type="numpy"), gr.Textbox()],
218
  title="InsectSAM 🐞",
219
  examples=examples
220
- ).launch()
 
59
 
60
  if mask is not None:
61
  mask_uint8 = (mask * 255).astype(np.uint8)
62
+ mask_inv = cv2.bitwise_not(mask_uint8)
63
+
64
+ # Extract insect using mask
65
+ insect_region = cv2.bitwise_and(image_cv2, image_cv2, mask=mask_uint8)
66
+ yellow_background_masked = cv2.bitwise_and(yellow_background, yellow_background, mask=mask_inv)
67
+ insect_combined = cv2.add(insect_region, yellow_background_masked)
68
+
69
+ yellow_background = cv2.add(yellow_background, insect_combined)
70
 
71
  return cv2.cvtColor(yellow_background, cv2.COLOR_BGR2RGB)
72
 
 
218
  outputs=[gr.Image(type="numpy"), gr.Textbox()],
219
  title="InsectSAM 🐞",
220
  examples=examples
221
+ ).launch()