Spaces:
Runtime error
Runtime error
Martin Tomov
commited on
json output attempt
Browse files
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
|
|
@@ -13,6 +13,7 @@ import matplotlib.pyplot as plt
|
|
| 13 |
from transformers import AutoModelForMaskGeneration, AutoProcessor, pipeline
|
| 14 |
import gradio as gr
|
| 15 |
import spaces
|
|
|
|
| 16 |
|
| 17 |
@dataclass
|
| 18 |
class BoundingBox:
|
|
@@ -142,13 +143,11 @@ def extract_and_paste_insect(original_image: np.ndarray, detection: DetectionRes
|
|
| 142 |
insect_crop = original_image[ymin:ymax, xmin:xmax]
|
| 143 |
mask_crop = mask[ymin:ymax, xmin:xmax]
|
| 144 |
|
| 145 |
-
# Ensure that we keep the original colors of the insect
|
| 146 |
insect = cv2.bitwise_and(insect_crop, insect_crop, mask=mask_crop)
|
| 147 |
|
| 148 |
x_offset, y_offset = xmin, ymin
|
| 149 |
x_end, y_end = x_offset + insect.shape[1], y_offset + insect.shape[0]
|
| 150 |
|
| 151 |
-
# Place the insect onto the yellow background
|
| 152 |
background[y_offset:y_end, x_offset:x_end] = insect
|
| 153 |
|
| 154 |
def create_yellow_background_with_insects(image: np.ndarray, detections: List[DetectionResult]) -> np.ndarray:
|
|
@@ -158,44 +157,54 @@ def create_yellow_background_with_insects(image: np.ndarray, detections: List[De
|
|
| 158 |
extract_and_paste_insect(image, detection, yellow_background)
|
| 159 |
return yellow_background
|
| 160 |
|
| 161 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
for detection in detections:
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
(
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
)
|
| 177 |
-
cv2.putText(
|
| 178 |
-
image_with_insects,
|
| 179 |
-
f"{label}: {score:.2f}",
|
| 180 |
-
(box.xmin, box.ymin - baseline),
|
| 181 |
-
cv2.FONT_HERSHEY_SIMPLEX,
|
| 182 |
-
0.5,
|
| 183 |
-
(255, 255, 255),
|
| 184 |
-
2
|
| 185 |
-
)
|
| 186 |
-
return image_with_insects
|
| 187 |
|
| 188 |
def process_image(image):
|
| 189 |
labels = ["insect"]
|
| 190 |
original_image, detections = grounded_segmentation(image, labels, threshold=0.3, polygon_refinement=True)
|
| 191 |
annotated_image = plot_detections(original_image, detections)
|
| 192 |
yellow_background_with_insects = create_yellow_background_with_insects(np.array(original_image), detections)
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
gr.Interface(
|
| 197 |
fn=process_image,
|
| 198 |
inputs=gr.Image(type="pil"),
|
| 199 |
-
outputs=[gr.Image(type="numpy"), gr.
|
| 200 |
title="π InsectSAM + GroundingDINO Inference",
|
| 201 |
-
).launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.system('pip install gradio==4.29.0')
|
| 3 |
|
| 4 |
import random
|
| 5 |
from dataclasses import dataclass
|
|
|
|
| 13 |
from transformers import AutoModelForMaskGeneration, AutoProcessor, pipeline
|
| 14 |
import gradio as gr
|
| 15 |
import spaces
|
| 16 |
+
import json
|
| 17 |
|
| 18 |
@dataclass
|
| 19 |
class BoundingBox:
|
|
|
|
| 143 |
insect_crop = original_image[ymin:ymax, xmin:xmax]
|
| 144 |
mask_crop = mask[ymin:ymax, xmin:xmax]
|
| 145 |
|
|
|
|
| 146 |
insect = cv2.bitwise_and(insect_crop, insect_crop, mask=mask_crop)
|
| 147 |
|
| 148 |
x_offset, y_offset = xmin, ymin
|
| 149 |
x_end, y_end = x_offset + insect.shape[1], y_offset + insect.shape[0]
|
| 150 |
|
|
|
|
| 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:
|
|
|
|
| 157 |
extract_and_paste_insect(image, detection, yellow_background)
|
| 158 |
return yellow_background
|
| 159 |
|
| 160 |
+
def run_length_encoding(mask):
|
| 161 |
+
pixels = mask.flatten()
|
| 162 |
+
rle = []
|
| 163 |
+
last_val = 0
|
| 164 |
+
count = 0
|
| 165 |
+
for pixel in pixels:
|
| 166 |
+
if pixel == last_val:
|
| 167 |
+
count += 1
|
| 168 |
+
else:
|
| 169 |
+
if count > 0:
|
| 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):
|
| 178 |
+
detections_list = []
|
| 179 |
for detection in detections:
|
| 180 |
+
detection_dict = {
|
| 181 |
+
"score": detection.score,
|
| 182 |
+
"label": detection.label,
|
| 183 |
+
"box": {
|
| 184 |
+
"xmin": detection.box.xmin,
|
| 185 |
+
"ymin": detection.box.ymin,
|
| 186 |
+
"xmax": detection.box.xmax,
|
| 187 |
+
"ymax": detection.box.ymax
|
| 188 |
+
},
|
| 189 |
+
"mask": run_length_encoding(detection.mask) if detection.mask is not None else None
|
| 190 |
+
}
|
| 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 |
annotated_image = plot_detections(original_image, detections)
|
| 198 |
yellow_background_with_insects = create_yellow_background_with_insects(np.array(original_image), detections)
|
| 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 annotated_image, json.dumps(detections_json, separators=(',', ':'))
|
| 204 |
|
| 205 |
gr.Interface(
|
| 206 |
fn=process_image,
|
| 207 |
inputs=gr.Image(type="pil"),
|
| 208 |
+
outputs=[gr.Image(type="numpy"), gr.Textbox()],
|
| 209 |
title="π InsectSAM + GroundingDINO Inference",
|
| 210 |
+
).launch()
|