Spaces:
Runtime error
Runtime error
Martin Tomov
commited on
bbox experiment with cv2
Browse files
app.py
CHANGED
|
@@ -46,7 +46,7 @@ class DetectionResult:
|
|
| 46 |
)
|
| 47 |
)
|
| 48 |
|
| 49 |
-
def annotate(image: Union[Image.Image, np.ndarray], detection_results: List[DetectionResult]) -> np.ndarray:
|
| 50 |
image_cv2 = np.array(image) if isinstance(image, Image.Image) else image
|
| 51 |
image_cv2 = cv2.cvtColor(image_cv2, cv2.COLOR_RGB2BGR)
|
| 52 |
|
|
@@ -55,7 +55,6 @@ def annotate(image: Union[Image.Image, np.ndarray], detection_results: List[Dete
|
|
| 55 |
score = detection.score
|
| 56 |
box = detection.box
|
| 57 |
mask = detection.mask
|
| 58 |
-
color = (0, 255, 255) # Yellow in BGR format
|
| 59 |
|
| 60 |
cv2.rectangle(image_cv2, (box.xmin, box.ymin), (box.xmax, box.ymax), color, 2)
|
| 61 |
cv2.putText(image_cv2, f'{label}: {score:.2f}', (box.xmin, box.ymin - 10),
|
|
@@ -155,9 +154,9 @@ def create_yellow_background_with_insects(image: np.ndarray, detections: List[De
|
|
| 155 |
for detection in detections:
|
| 156 |
if detection.mask is not None:
|
| 157 |
extract_and_paste_insect(image, detection, yellow_background)
|
| 158 |
-
#
|
| 159 |
-
|
| 160 |
-
return
|
| 161 |
|
| 162 |
def run_length_encoding(mask):
|
| 163 |
pixels = mask.flatten()
|
|
@@ -196,16 +195,15 @@ def detections_to_json(detections):
|
|
| 196 |
def process_image(image, include_json):
|
| 197 |
labels = ["insect"]
|
| 198 |
original_image, detections = grounded_segmentation(image, labels, threshold=0.3, polygon_refinement=True)
|
| 199 |
-
annotated_image = plot_detections(original_image, detections)
|
| 200 |
yellow_background_with_insects = create_yellow_background_with_insects(np.array(original_image), detections)
|
| 201 |
if include_json:
|
| 202 |
detections_json = detections_to_json(detections)
|
| 203 |
json_output_path = "insect_detections.json"
|
| 204 |
with open(json_output_path, 'w') as json_file:
|
| 205 |
json.dump(detections_json, json_file, indent=4)
|
| 206 |
-
return
|
| 207 |
else:
|
| 208 |
-
return
|
| 209 |
|
| 210 |
examples = [
|
| 211 |
["flower-night.jpg"]
|
|
@@ -214,7 +212,7 @@ examples = [
|
|
| 214 |
gr.Interface(
|
| 215 |
fn=process_image,
|
| 216 |
inputs=[gr.Image(type="pil"), gr.Checkbox(label="Include JSON", value=False)],
|
| 217 |
-
outputs=[gr.Image(type="numpy"), gr.
|
| 218 |
title="InsectSAM π",
|
| 219 |
examples=examples
|
| 220 |
).launch()
|
|
|
|
| 46 |
)
|
| 47 |
)
|
| 48 |
|
| 49 |
+
def annotate(image: Union[Image.Image, np.ndarray], detection_results: List[DetectionResult], color=(0, 255, 255)) -> np.ndarray:
|
| 50 |
image_cv2 = np.array(image) if isinstance(image, Image.Image) else image
|
| 51 |
image_cv2 = cv2.cvtColor(image_cv2, cv2.COLOR_RGB2BGR)
|
| 52 |
|
|
|
|
| 55 |
score = detection.score
|
| 56 |
box = detection.box
|
| 57 |
mask = detection.mask
|
|
|
|
| 58 |
|
| 59 |
cv2.rectangle(image_cv2, (box.xmin, box.ymin), (box.xmax, box.ymax), color, 2)
|
| 60 |
cv2.putText(image_cv2, f'{label}: {score:.2f}', (box.xmin, box.ymin - 10),
|
|
|
|
| 154 |
for detection in detections:
|
| 155 |
if detection.mask is not None:
|
| 156 |
extract_and_paste_insect(image, detection, yellow_background)
|
| 157 |
+
# Draw bounding boxes and labels on yellow background
|
| 158 |
+
annotated_background = annotate(yellow_background, detections, color=(0, 255, 255))
|
| 159 |
+
return annotated_background
|
| 160 |
|
| 161 |
def run_length_encoding(mask):
|
| 162 |
pixels = mask.flatten()
|
|
|
|
| 195 |
def process_image(image, include_json):
|
| 196 |
labels = ["insect"]
|
| 197 |
original_image, detections = grounded_segmentation(image, labels, threshold=0.3, polygon_refinement=True)
|
|
|
|
| 198 |
yellow_background_with_insects = create_yellow_background_with_insects(np.array(original_image), detections)
|
| 199 |
if include_json:
|
| 200 |
detections_json = detections_to_json(detections)
|
| 201 |
json_output_path = "insect_detections.json"
|
| 202 |
with open(json_output_path, 'w') as json_file:
|
| 203 |
json.dump(detections_json, json_file, indent=4)
|
| 204 |
+
return yellow_background_with_insects, json.dumps(detections_json, separators=(',', ':'))
|
| 205 |
else:
|
| 206 |
+
return yellow_background_with_insects, None
|
| 207 |
|
| 208 |
examples = [
|
| 209 |
["flower-night.jpg"]
|
|
|
|
| 212 |
gr.Interface(
|
| 213 |
fn=process_image,
|
| 214 |
inputs=[gr.Image(type="pil"), gr.Checkbox(label="Include JSON", value=False)],
|
| 215 |
+
outputs=[gr.Image(type="numpy"), gr.Textbox()],
|
| 216 |
title="InsectSAM π",
|
| 217 |
examples=examples
|
| 218 |
).launch()
|