Spaces:
Sleeping
Sleeping
Update model_inference.py
Browse files- model_inference.py +72 -73
model_inference.py
CHANGED
|
@@ -1,74 +1,73 @@
|
|
| 1 |
-
import onnxruntime as ort
|
| 2 |
-
import cv2
|
| 3 |
-
import numpy as np
|
| 4 |
-
from scipy.special import softmax
|
| 5 |
-
import pandas as pd
|
| 6 |
-
from sklearn.preprocessing import LabelBinarizer
|
| 7 |
-
import os
|
| 8 |
-
import gdown
|
| 9 |
-
|
| 10 |
-
# -------------------------------
|
| 11 |
-
# Load metadata
|
| 12 |
-
# -------------------------------
|
| 13 |
-
metadata_path = "data/venomstatus_with_antivenom.csv"
|
| 14 |
-
metadata = pd.read_csv(metadata_path)
|
| 15 |
-
|
| 16 |
-
# Prepare LabelBinarizer
|
| 17 |
-
labels_species = metadata['binomial']
|
| 18 |
-
lb_species = LabelBinarizer()
|
| 19 |
-
lb_species.fit(np.asarray(labels_species))
|
| 20 |
-
|
| 21 |
-
# -------------------------------
|
| 22 |
-
# Download model from Google Drive
|
| 23 |
-
# -------------------------------
|
| 24 |
-
MODEL_PATH = "model.onnx"
|
| 25 |
-
|
| 26 |
-
if not os.path.exists(MODEL_PATH):
|
| 27 |
-
url = "https://drive.google.com/uc?id=1cLvN64Lh0aFIuB4CkEAh0ko422FPBHCy"
|
| 28 |
-
gdown.download(url, MODEL_PATH, quiet=False)
|
| 29 |
-
|
| 30 |
-
# -------------------------------
|
| 31 |
-
# Load ONNX model
|
| 32 |
-
# -------------------------------
|
| 33 |
-
ort_session = ort.InferenceSession(MODEL_PATH)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# -------------------------------
|
| 37 |
-
# Prediction function
|
| 38 |
-
# -------------------------------
|
| 39 |
-
def predict_image_int32(image_path, ort_session=ort_session, lb_species=lb_species, height=384, width=384):
|
| 40 |
-
"""Predict snake species using ONNX model"""
|
| 41 |
-
|
| 42 |
-
image = cv2.imread(image_path)
|
| 43 |
-
if image is None:
|
| 44 |
-
raise FileNotFoundError(f"Image not found: {image_path}")
|
| 45 |
-
|
| 46 |
-
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 47 |
-
image = cv2.resize(image, (width, height))
|
| 48 |
-
image = image.astype(np.int32)
|
| 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 |
return venom_status, antivenom, habitat
|
|
|
|
| 1 |
+
import onnxruntime as ort
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from scipy.special import softmax
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from sklearn.preprocessing import LabelBinarizer
|
| 7 |
+
import os
|
| 8 |
+
import gdown
|
| 9 |
+
|
| 10 |
+
# -------------------------------
|
| 11 |
+
# Load metadata
|
| 12 |
+
# -------------------------------
|
| 13 |
+
metadata_path = "data/venomstatus_with_antivenom.csv"
|
| 14 |
+
metadata = pd.read_csv(metadata_path)
|
| 15 |
+
|
| 16 |
+
# Prepare LabelBinarizer
|
| 17 |
+
labels_species = metadata['binomial']
|
| 18 |
+
lb_species = LabelBinarizer()
|
| 19 |
+
lb_species.fit(np.asarray(labels_species))
|
| 20 |
+
|
| 21 |
+
# -------------------------------
|
| 22 |
+
# Download model from Google Drive
|
| 23 |
+
# -------------------------------
|
| 24 |
+
MODEL_PATH = "model.onnx"
|
| 25 |
+
|
| 26 |
+
if not os.path.exists(MODEL_PATH):
|
| 27 |
+
url = "https://drive.google.com/uc?id=1cLvN64Lh0aFIuB4CkEAh0ko422FPBHCy"
|
| 28 |
+
gdown.download(url, MODEL_PATH, quiet=False)
|
| 29 |
+
|
| 30 |
+
# -------------------------------
|
| 31 |
+
# Load ONNX model
|
| 32 |
+
# -------------------------------
|
| 33 |
+
ort_session = ort.InferenceSession(MODEL_PATH)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# -------------------------------
|
| 37 |
+
# Prediction function
|
| 38 |
+
# -------------------------------
|
| 39 |
+
def predict_image_int32(image_path, ort_session=ort_session, lb_species=lb_species, height=384, width=384):
|
| 40 |
+
"""Predict snake species using ONNX model"""
|
| 41 |
+
|
| 42 |
+
image = cv2.imread(image_path)
|
| 43 |
+
if image is None:
|
| 44 |
+
raise FileNotFoundError(f"Image not found: {image_path}")
|
| 45 |
+
|
| 46 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 47 |
+
image = cv2.resize(image, (width, height))
|
| 48 |
+
image = image.astype(np.int32)
|
| 49 |
+
|
| 50 |
+
# ❌ REMOVED: image = np.expand_dims(image, axis=0)
|
| 51 |
+
|
| 52 |
+
outputs = ort_session.run(None, {ort_session.get_inputs()[0].name: image})
|
| 53 |
+
logits = outputs[0]
|
| 54 |
+
probs = softmax(logits)
|
| 55 |
+
|
| 56 |
+
pred_class = int(np.argmax(probs))
|
| 57 |
+
confidence = float(np.max(probs) * 100)
|
| 58 |
+
species_name = lb_species.classes_[pred_class]
|
| 59 |
+
|
| 60 |
+
return pred_class, species_name, confidence
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# -------------------------------
|
| 64 |
+
# Metadata lookup
|
| 65 |
+
# -------------------------------
|
| 66 |
+
def get_snake_info(pred_class):
|
| 67 |
+
row = metadata[metadata["class_id"] == pred_class].iloc[0]
|
| 68 |
+
|
| 69 |
+
venom_status = "Venomous" if row["venom_status"] == 1 else "Non-Venomous"
|
| 70 |
+
antivenom = row["antivenom Name"]
|
| 71 |
+
habitat = row.get("region", "Not available")
|
| 72 |
+
|
|
|
|
| 73 |
return venom_status, antivenom, habitat
|