Martin Tomov commited on
Commit
1dff9a5
·
verified ·
1 Parent(s): e0e0d79

app.py update, change of outputs

Browse files
Files changed (1) hide show
  1. app.py +6 -34
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import os
2
- os.system('pip install gradio==4.29.0')
3
 
4
  import random
5
  from dataclasses import dataclass
@@ -132,17 +132,6 @@ def grounded_segmentation(image: Union[Image.Image, str], labels: List[str], thr
132
  detections = segment(image, detections, polygon_refinement, segmenter_id)
133
  return np.array(image), detections
134
 
135
- def extract_insect_masks(image: np.ndarray, detections: List[DetectionResult]) -> List[np.ndarray]:
136
- return [detection.mask for detection in detections if detection.mask is not None]
137
-
138
- def put_masks_on_yellow_background(image_shape: Tuple[int, int], masks: List[np.ndarray]) -> np.ndarray:
139
- yellow_background = np.full((image_shape[0], image_shape[1], 3), (0, 255, 255), dtype=np.uint8)
140
- for mask in masks:
141
- mask_rgb = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)
142
- for c in range(3):
143
- yellow_background[:,:,c] = cv2.bitwise_or(yellow_background[:,:,c], mask_rgb[:,:,c])
144
- return yellow_background
145
-
146
  def mask_to_min_max(mask: np.ndarray) -> Tuple[int, int, int, int]:
147
  y, x = np.where(mask)
148
  return x.min(), y.min(), x.max(), y.max()
@@ -168,33 +157,16 @@ def create_yellow_background_with_insects(image: np.ndarray, detections: List[De
168
  extract_and_paste_insect(image, detection, yellow_background)
169
  return yellow_background
170
 
171
- def draw_classification_boxes(image_with_insects: np.ndarray, detections: List[DetectionResult]) -> np.ndarray:
172
- for detection in detections:
173
- label = detection.label
174
- score = detection.score
175
- box = detection.box
176
- color = np.random.randint(0, 256, size=3).tolist()
177
- cv2.rectangle(image_with_insects, (box.xmin, box.ymin), (box.xmax, box.ymax), color, 2)
178
- (text_width, text_height), baseline = cv2.getTextSize(f"{label}: {score:.2f}", cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)
179
- cv2.rectangle(image_with_insects, (box.xmin, box.ymin - text_height - baseline), (box.xmin + text_width, box.ymin),
180
- color, thickness=cv2.FILLED)
181
- cv2.putText(image_with_insects, f"{label}: {score:.2f}", (box.xmin, box.ymin - baseline),
182
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)
183
- return image_with_insects
184
-
185
  def process_image(image):
186
  labels = ["insect"]
187
  original_image, detections = grounded_segmentation(image, labels, threshold=0.3, polygon_refinement=True)
188
- masked_image = plot_detections(original_image, detections)
189
- insect_masks = extract_insect_masks(original_image, detections)
190
- yellow_background_with_masks = put_masks_on_yellow_background(original_image.shape[:2], insect_masks)
191
- yellow_background_with_insects = create_yellow_background_with_insects(original_image, detections)
192
- yellow_background_with_boxes = draw_classification_boxes(yellow_background_with_insects, detections)
193
- return masked_image, yellow_background_with_masks, 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"), gr.Image(type="numpy")],
199
- title="🐞 Insect Detection and Masking"
200
  ).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
 
132
  detections = segment(image, detections, polygon_refinement, segmenter_id)
133
  return np.array(image), detections
134
 
 
 
 
 
 
 
 
 
 
 
 
135
  def mask_to_min_max(mask: np.ndarray) -> Tuple[int, int, int, int]:
136
  y, x = np.where(mask)
137
  return x.min(), y.min(), x.max(), y.max()
 
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()