Spaces:
Runtime error
Runtime error
Martin Tomov
commited on
json optional
Browse files
app.py
CHANGED
|
@@ -191,15 +191,18 @@ def detections_to_json(detections):
|
|
| 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 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
examples = [
|
| 205 |
["flower-night.jpg"]
|
|
@@ -207,7 +210,7 @@ examples = [
|
|
| 207 |
|
| 208 |
gr.Interface(
|
| 209 |
fn=process_image,
|
| 210 |
-
inputs=gr.Image(type="pil"),
|
| 211 |
outputs=[gr.Image(type="numpy"), gr.Textbox()],
|
| 212 |
title="InsectSAM π + GroundingDINO Inference",
|
| 213 |
examples=examples
|
|
|
|
| 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 π + GroundingDINO Inference",
|
| 216 |
examples=examples
|