Spaces:
Runtime error
Runtime error
Martin Tomov
commited on
output original_image and detections v2
Browse files
app.py
CHANGED
|
@@ -170,8 +170,7 @@ def run_length_encoding(mask):
|
|
| 170 |
rle.append(count)
|
| 171 |
count = 1
|
| 172 |
last_val = pixel
|
| 173 |
-
if count > 0
|
| 174 |
-
rle.append(count)
|
| 175 |
return rle
|
| 176 |
|
| 177 |
def detections_to_json(detections):
|
|
@@ -191,18 +190,28 @@ def detections_to_json(detections):
|
|
| 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 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
examples = [
|
| 208 |
["flower-night.jpg"]
|
|
@@ -210,8 +219,8 @@ examples = [
|
|
| 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()
|
|
|
|
| 170 |
rle.append(count)
|
| 171 |
count = 1
|
| 172 |
last_val = pixel
|
| 173 |
+
if count > 0, rle.append(count)
|
|
|
|
| 174 |
return rle
|
| 175 |
|
| 176 |
def detections_to_json(detections):
|
|
|
|
| 190 |
detections_list.append(detection_dict)
|
| 191 |
return detections_list
|
| 192 |
|
| 193 |
+
def process_image(image, include_json, include_raw):
|
| 194 |
labels = ["insect"]
|
| 195 |
original_image, detections = grounded_segmentation(image, labels, threshold=0.3, polygon_refinement=True)
|
| 196 |
yellow_background_with_insects = create_yellow_background_with_insects(np.array(original_image), detections)
|
| 197 |
+
|
| 198 |
+
output1 = yellow_background_with_insects
|
| 199 |
+
output2 = None
|
| 200 |
+
output3 = None
|
| 201 |
+
output4 = None
|
| 202 |
+
|
| 203 |
if include_json:
|
| 204 |
detections_json = detections_to_json(detections)
|
| 205 |
json_output_path = "insect_detections.json"
|
| 206 |
with open(json_output_path, 'w') as json_file:
|
| 207 |
json.dump(detections_json, json_file, indent=4)
|
| 208 |
+
output2 = json.dumps(detections_json, separators=(',', ':'))
|
| 209 |
+
|
| 210 |
+
if include_raw:
|
| 211 |
+
output3 = original_image
|
| 212 |
+
output4 = detections
|
| 213 |
+
|
| 214 |
+
return output1, output2, output3, output4
|
| 215 |
|
| 216 |
examples = [
|
| 217 |
["flower-night.jpg"]
|
|
|
|
| 219 |
|
| 220 |
gr.Interface(
|
| 221 |
fn=process_image,
|
| 222 |
+
inputs=[gr.Image(type="pil"), gr.Checkbox(label="Include JSON", value=False), gr.Checkbox(label="Include Raw Outputs", value=False)],
|
| 223 |
+
outputs=[gr.Image(type="numpy"), gr.Textbox(), gr.Image(type="numpy"), gr.JSON()],
|
| 224 |
title="InsectSAM π",
|
| 225 |
examples=examples
|
| 226 |
+
).launch()
|