Martin Tomov commited on
Commit
52d743b
Β·
verified Β·
1 Parent(s): 7a6ef66

BGR color code issue

Browse files
Files changed (1) hide show
  1. app.py +7 -17
app.py CHANGED
@@ -151,7 +151,7 @@ def extract_and_paste_insect(original_image: np.ndarray, detection: DetectionRes
151
  background[y_offset:y_end, x_offset:x_end] = insect
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)
@@ -191,28 +191,18 @@ def detections_to_json(detections):
191
  detections_list.append(detection_dict)
192
  return detections_list
193
 
194
- def process_image(image, include_json, include_raw):
195
  labels = ["insect"]
196
  original_image, detections = grounded_segmentation(image, labels, threshold=0.3, polygon_refinement=True)
197
  yellow_background_with_insects = create_yellow_background_with_insects(np.array(original_image), detections)
198
-
199
- output1 = yellow_background_with_insects
200
- output2 = None
201
- output3 = None
202
- output4 = None
203
-
204
  if include_json:
205
  detections_json = detections_to_json(detections)
206
  json_output_path = "insect_detections.json"
207
  with open(json_output_path, 'w') as json_file:
208
  json.dump(detections_json, json_file, indent=4)
209
- output2 = json.dumps(detections_json, separators=(',', ':'))
210
-
211
- if include_raw:
212
- output3 = original_image
213
- output4 = detections
214
-
215
- return output1, output2, output3, output4
216
 
217
  examples = [
218
  ["flower-night.jpg"]
@@ -220,8 +210,8 @@ examples = [
220
 
221
  gr.Interface(
222
  fn=process_image,
223
- inputs=[gr.Image(type="pil"), gr.Checkbox(label="Include JSON", value=False), gr.Checkbox(label="Include Raw Outputs", value=False)],
224
- outputs=[gr.Image(type="numpy"), gr.Textbox(), gr.Image(type="numpy"), gr.JSON()],
225
  title="InsectSAM 🐞",
226
  examples=examples
227
  ).launch()
 
151
  background[y_offset:y_end, x_offset:x_end] = insect
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) # BGR for yellow
155
  for detection in detections:
156
  if detection.mask is not None:
157
  extract_and_paste_insect(image, detection, yellow_background)
 
191
  detections_list.append(detection_dict)
192
  return detections_list
193
 
194
+ def process_image(image, include_json):
195
  labels = ["insect"]
196
  original_image, detections = grounded_segmentation(image, labels, threshold=0.3, polygon_refinement=True)
197
  yellow_background_with_insects = create_yellow_background_with_insects(np.array(original_image), detections)
 
 
 
 
 
 
198
  if include_json:
199
  detections_json = detections_to_json(detections)
200
  json_output_path = "insect_detections.json"
201
  with open(json_output_path, 'w') as json_file:
202
  json.dump(detections_json, json_file, indent=4)
203
+ return yellow_background_with_insects, json.dumps(detections_json, separators=(',', ':'))
204
+ else:
205
+ return yellow_background_with_insects, None
 
 
 
 
206
 
207
  examples = [
208
  ["flower-night.jpg"]
 
210
 
211
  gr.Interface(
212
  fn=process_image,
213
+ inputs=[gr.Image(type="pil"), gr.Checkbox(label="Include JSON", value=False)],
214
+ outputs=[gr.Image(type="numpy"), gr.Textbox()],
215
  title="InsectSAM 🐞",
216
  examples=examples
217
  ).launch()