Martin Tomov commited on
Commit
93c8712
Β·
verified Β·
1 Parent(s): 04092d2

test outputs

Browse files
Files changed (1) hide show
  1. app.py +31 -3
app.py CHANGED
@@ -151,22 +151,50 @@ def extract_and_paste_insect(original_image: np.ndarray, detection: DetectionRes
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_like(image, (0, 255, 255), dtype=np.uint8)
155
  for detection in detections:
156
  if detection.mask is not None:
157
  extract_and_paste_insect(image, detection, yellow_background)
158
  return yellow_background
159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  def process_image(image):
161
  labels = ["insect"]
162
  original_image, detections = grounded_segmentation(image, labels, threshold=0.3, polygon_refinement=True)
163
  annotated_image = plot_detections(original_image, detections)
164
  yellow_background_with_insects = create_yellow_background_with_insects(np.array(original_image), detections)
165
- return annotated_image, yellow_background_with_insects
 
166
 
167
  gr.Interface(
168
  fn=process_image,
169
  inputs=gr.Image(type="pil"),
170
  outputs=[gr.Image(type="numpy"), gr.Image(type="numpy")],
171
  title="🐞 Insect Detection and Masking"
172
- ).launch()
 
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)
155
  for detection in detections:
156
  if detection.mask is not None:
157
  extract_and_paste_insect(image, detection, yellow_background)
158
  return yellow_background
159
 
160
+ def draw_classification_boxes(image_with_insects, detections):
161
+ for detection in detections:
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)
169
+ cv2.rectangle(
170
+ image_with_insects,
171
+ (box.xmin, box.ymin - text_height - baseline),
172
+ (box.xmin + text_width, box.ymin),
173
+ color,
174
+ thickness=cv2.FILLED
175
+ )
176
+ cv2.putText(
177
+ image_with_insects,
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="🐞 Insect Detection and Masking"
200
+ ).launch()