File size: 6,931 Bytes
6ca6607 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
from IPython.display import display, JSON
import matplotlib.pyplot as plt
from speciesnet import DEFAULT_MODEL, SUPPORTED_MODELS, SpeciesNet
import numpy as np
import time
import gradio as gr
import json
import cv2
import os
# ------------------------------------------------------
# LOAD MODEL
# ------------------------------------------------------
print("Default SpeciesNet model:", DEFAULT_MODEL)
print("Supported SpeciesNet models:", SUPPORTED_MODELS)
model = SpeciesNet(DEFAULT_MODEL)
# ------------------------------------------------------
# VALIDATION FUNCTIONS
# ------------------------------------------------------
def validate_predictions_structure(pred):
"""
Validate internal structure for both detection and classification.
This ensures correct keys exist and formats are valid.
"""
required_keys = ["filepath", "detections", "classifications"]
for key in required_keys:
if key not in pred:
raise ValueError(f" Missing key '{key}' in prediction block")
# --- Validate detections (list of dicts) ---
if not isinstance(pred["detections"], list):
raise ValueError(" detections must be a list")
for det in pred["detections"]:
if not all(k in det for k in ["bbox", "conf", "label"]):
raise ValueError(" Each detection must contain bbox, conf, label")
if len(det["bbox"]) != 4:
raise ValueError(" bbox must be [x, y, w, h]")
# --- Validate classifications ---
cls = pred["classifications"]
if not isinstance(cls, dict):
raise ValueError(" classifications must be a dictionary")
for key in ["classes", "scores"]:
if key not in cls:
raise ValueError(f" classifications missing '{key}'")
if len(cls["classes"]) != len(cls["scores"]):
raise ValueError(" classes and scores length mismatch")
return True
def validate_model_output(predictions_dict):
"""
Validates entire output returned by SpeciesNet before visualization.
"""
if "predictions" not in predictions_dict:
raise ValueError(" Output missing top-level 'predictions' key")
if not isinstance(predictions_dict["predictions"], list):
raise ValueError(" 'predictions' must be a list")
print(f" Total prediction entries: {len(predictions_dict['predictions'])}")
# Validate each prediction block
for i, pred in enumerate(predictions_dict["predictions"]):
print(f"\n--- Checking prediction #{i+1} ---")
validate_predictions_structure(pred)
print("\n Output format validated successfully!\n")
# ------------------------------------------------------
# VISUALIZATION
# ------------------------------------------------------
def draw_predictions(image_path, predictions_dict):
img = cv2.imread(image_path)
if img is None:
raise ValueError(f"Could not load image: {image_path}")
img_h, img_w, _ = img.shape
for pred in predictions_dict.get("predictions", []):
detections = pred.get("detections", [])
classifications = pred.get("classifications", {})
classes = classifications.get("classes", [])
scores = classifications.get("scores", [])
top_class_name = None
top_score = None
if len(classes) > 0:
top_class_name = classes[0].split(";")[-1]
top_score = scores[0]
# SKIP NON-ANIMALS
if len(classes) == 0:
continue
taxon = classes[0].lower()
if not ("mammalia" in taxon or "aves" in taxon):
continue
for det in detections:
bbox = det["bbox"]
conf = det["conf"]
label = det["label"]
x, y, w, h = bbox
x1 = int(x * img_w)
y1 = int(y * img_h)
x2 = int((x + w) * img_w)
y2 = int((y + h) * img_h)
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 3)
detection_text = f"{label} ({conf:.2f})"
classification_text = (
f"{top_class_name} ({top_score:.2f})" if top_class_name else ""
)
text_lines = []
if classification_text:
text_lines.append(classification_text)
text_lines.append(detection_text)
total_text_height = 0
text_widths = []
for line in text_lines:
(text_w, text_h), _ = cv2.getTextSize(
line, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2
)
total_text_height += text_h + 5
text_widths.append(text_w)
max_text_width = max(text_widths)
cv2.rectangle(
img,
(x1, max(y1 - total_text_height - 10, 0)),
(x1 + max_text_width + 10, y1),
(0, 255, 0),
-1,
)
y_text = y1 - 5
for line in text_lines[::-1]:
cv2.putText(
img,
line,
(x1 + 5, y_text),
cv2.FONT_HERSHEY_SIMPLEX,
0.6,
(0, 0, 0),
2,
cv2.LINE_AA,
)
(_, text_h), _ = cv2.getTextSize(
line, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2
)
y_text -= text_h + 5
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# ------------------------------------------------------
# INFERENCE FUNCTION
# ------------------------------------------------------
def inference(image):
filepath = "temp_image.jpg"
image.save(filepath)
start = time.time()
predictions_dict = model.predict(
instances_dict={
"instances": [
{
"filepath": filepath,
# "country": "VNM",
}
]
}
)
end = time.time()
print(f"\n⏱ Inference Time: {end - start:.2f} sec")
# --- Validate format ---
validate_model_output(predictions_dict)
# --- Save JSON ---
with open("last_output.json", "w") as f:
json.dump(predictions_dict, f, indent=4)
print(" Saved JSON to last_output.json\n")
# --- Draw Visualization ---
annotated_image = draw_predictions(filepath, predictions_dict)
pretty_json = json.dumps(predictions_dict, indent=4)
return annotated_image, pretty_json
# ------------------------------------------------------
# GRADIO UI
# ------------------------------------------------------
iface = gr.Interface(
fn=inference,
inputs=gr.Image(type="pil"),
outputs=[
gr.Image(label="Detection + Classification Output"),
gr.JSON(label="Raw Model Output"),
],
title=" SpeciesNet Wildlife Detector + Classifier",
description="Upload a wildlife camera image.",
)
iface.launch()
|