Martin Tomov commited on
Commit
3562db4
Β·
verified Β·
1 Parent(s): 8e91dd4

runtime error fix

Browse files
Files changed (1) hide show
  1. app.py +10 -11
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import os
2
- os.system('pip install gradio==4.29.0') # as gradio==4.29.0 doesn't work in requirements.txt
3
 
4
  import random
5
  from dataclasses import dataclass
@@ -141,15 +141,14 @@ def extract_and_paste_insect(original_image: np.ndarray, detection: DetectionRes
141
  xmin, ymin, xmax, ymax = mask_to_min_max(mask)
142
  insect_crop = original_image[ymin:ymax, xmin:xmax]
143
  mask_crop = mask[ymin:ymax, xmin:xmax]
144
-
145
- # Ensure that we keep the original colors of the insect
146
  insect = cv2.bitwise_and(insect_crop, insect_crop, mask=mask_crop)
147
-
148
- x_offset, y_offset = xmin, ymin
149
  x_end, y_end = x_offset + insect.shape[1], y_offset + insect.shape[0]
150
-
151
- # Place the insect onto the yellow background
152
- background[y_offset:y_end, x_offset:x_end] = insect
 
 
153
 
154
  def create_yellow_background_with_insects(image: np.ndarray, detections: List[DetectionResult]) -> np.ndarray:
155
  yellow_background = np.full((image.shape[0], image.shape[1], 3), (0, 255, 255), dtype=np.uint8)
@@ -163,7 +162,7 @@ def draw_classification_boxes(image_with_insects, detections):
163
  label = detection.label
164
  score = detection.score
165
  box = detection.box
166
- color = (0, 255, 255) # Yellow color for bounding box
167
 
168
  cv2.rectangle(image_with_insects, (box.xmin, box.ymin), (box.xmax, box.ymax), color, 2)
169
  (text_width, text_height), baseline = cv2.getTextSize(f"{label}: {score:.2f}", cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)
@@ -181,7 +180,7 @@ def draw_classification_boxes(image_with_insects, detections):
181
  cv2.FONT_HERSHEY_SIMPLEX,
182
  0.5,
183
  (255, 255, 255),
184
- 2
185
  )
186
  return image_with_insects
187
 
@@ -198,4 +197,4 @@ gr.Interface(
198
  inputs=gr.Image(type="pil"),
199
  outputs=[gr.Image(type="numpy"), gr.Image(type="numpy")],
200
  title="🐞 InsectSAM + GroundingDINO Inference",
201
- ).launch()
 
1
  import os
2
+ os.system('pip install gradio==4.29.0') # as gradio==4.29.0 doesn't work in requirements.txt
3
 
4
  import random
5
  from dataclasses import dataclass
 
141
  xmin, ymin, xmax, ymax = mask_to_min_max(mask)
142
  insect_crop = original_image[ymin:ymax, xmin:xmax]
143
  mask_crop = mask[ymin:ymax, xmin:xmax]
 
 
144
  insect = cv2.bitwise_and(insect_crop, insect_crop, mask=mask_crop)
145
+ x_offset, y_offset = detection.box.xmin, detection.box.ymin
 
146
  x_end, y_end = x_offset + insect.shape[1], y_offset + insect.shape[0]
147
+ inverse_mask = cv2.bitwise_not(mask_crop)
148
+ bg_region = background[y_offset:y_end, x_offset:x_end]
149
+ bg_ready = cv2.bitwise_and(bg_region, bg_region, mask=inverse_mask)
150
+ combined = cv2.add(insect, bg_ready)
151
+ background[y_offset:y_end, x_offset:x_end] = combined
152
 
153
  def create_yellow_background_with_insects(image: np.ndarray, detections: List[DetectionResult]) -> np.ndarray:
154
  yellow_background = np.full((image.shape[0], image.shape[1], 3), (0, 255, 255), dtype=np.uint8)
 
162
  label = detection.label
163
  score = detection.score
164
  box = detection.box
165
+ color = np.random.randint(0, 256, size=3).tolist()
166
 
167
  cv2.rectangle(image_with_insects, (box.xmin, box.ymin), (box.xmax, box.ymax), color, 2)
168
  (text_width, text_height), baseline = cv2.getTextSize(f"{label}: {score:.2f}", cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)
 
180
  cv2.FONT_HERSHEY_SIMPLEX,
181
  0.5,
182
  (255, 255, 255),
183
+ 2
184
  )
185
  return image_with_insects
186
 
 
197
  inputs=gr.Image(type="pil"),
198
  outputs=[gr.Image(type="numpy"), gr.Image(type="numpy")],
199
  title="🐞 InsectSAM + GroundingDINO Inference",
200
+ ).launch()