Martin Tomov commited on
Commit
2a212bf
β€’
1 Parent(s): 0764038
Files changed (1) hide show
  1. app.py +10 -16
app.py CHANGED
@@ -191,25 +191,19 @@ def detections_to_json(detections):
191
  detections_list.append(detection_dict)
192
  return detections_list
193
 
194
- def process_images(image_files):
195
  labels = ["insect"]
196
- results = []
197
- json_outputs = []
198
- for image_file in image_files:
199
- image = Image.open(image_file).convert("RGB")
200
- original_image, detections = grounded_segmentation(image, labels, threshold=0.3, polygon_refinement=True)
201
- yellow_background_with_insects = create_yellow_background_with_insects(np.array(original_image), detections)
202
- detections_json = detections_to_json(detections)
203
- results.append((yellow_background_with_insects, json.dumps(detections_json, separators=(',', ':'))))
204
- json_outputs.append(detections_json)
205
  json_output_path = "insect_detections.json"
206
  with open(json_output_path, 'w') as json_file:
207
- json.dump(json_outputs, json_file, indent=4)
208
- return results
209
 
210
  gr.Interface(
211
- fn=process_images,
212
- inputs=gr.File(file_count="multiple", type="filepath"),
213
- outputs=[gr.Gallery(), gr.Textbox()],
214
  title="🐞 InsectSAM + GroundingDINO Inference",
215
- ).launch()
 
191
  detections_list.append(detection_dict)
192
  return detections_list
193
 
194
+ def process_image(image):
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
+ detections_json = detections_to_json(detections)
 
 
 
 
 
 
199
  json_output_path = "insect_detections.json"
200
  with open(json_output_path, 'w') as json_file:
201
+ json.dump(detections_json, json_file, indent=4)
202
+ return yellow_background_with_insects, json.dumps(detections_json, separators=(',', ':'))
203
 
204
  gr.Interface(
205
+ fn=process_image,
206
+ inputs=gr.Image(type="pil"),
207
+ outputs=[gr.Image(type="numpy"), gr.Textbox()],
208
  title="🐞 InsectSAM + GroundingDINO Inference",
209
+ ).launch()