Spaces:
Sleeping
Sleeping
Delete model_inference.py
Browse files- model_inference.py +0 -46
model_inference.py
DELETED
|
@@ -1,46 +0,0 @@
|
|
| 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 |
-
|
| 8 |
-
# Load metadata
|
| 9 |
-
metadata_path = "data/venomstatus_with_antivenom.csv"
|
| 10 |
-
metadata = pd.read_csv(metadata_path)
|
| 11 |
-
|
| 12 |
-
# Prepare LabelBinarizer
|
| 13 |
-
labels_species = metadata['binomial']
|
| 14 |
-
lb_species = LabelBinarizer()
|
| 15 |
-
lb_species.fit(np.asarray(labels_species))
|
| 16 |
-
|
| 17 |
-
# Load ONNX model
|
| 18 |
-
model_path = "models/ViT_PreProcessing-ops11-preprocessing-int-dynam_graph.onnx"
|
| 19 |
-
ort_session = ort.InferenceSession(model_path)
|
| 20 |
-
|
| 21 |
-
def predict_image_int32(image_path, ort_session=ort_session, lb_species=lb_species, height=384, width=384):
|
| 22 |
-
"""Predict snake species using ONNX model"""
|
| 23 |
-
image = cv2.imread(image_path)
|
| 24 |
-
if image is None:
|
| 25 |
-
raise FileNotFoundError(f"Image not found: {image_path}")
|
| 26 |
-
|
| 27 |
-
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 28 |
-
image = cv2.resize(image, (width, height))
|
| 29 |
-
image = image.astype(np.int32)
|
| 30 |
-
|
| 31 |
-
outputs = ort_session.run(None, {ort_session.get_inputs()[0].name: image})
|
| 32 |
-
logits = outputs[0]
|
| 33 |
-
probs = softmax(logits)
|
| 34 |
-
pred_class = int(np.argmax(probs))
|
| 35 |
-
confidence = float(np.max(probs) * 100)
|
| 36 |
-
species_name = lb_species.classes_[pred_class]
|
| 37 |
-
|
| 38 |
-
return pred_class, species_name, confidence
|
| 39 |
-
|
| 40 |
-
def get_snake_info(pred_class):
|
| 41 |
-
"""Retrieve venom status and antivenom info from metadata"""
|
| 42 |
-
row = metadata[metadata["class_id"] == pred_class].iloc[0]
|
| 43 |
-
venom_status = "Venomous" if row["venom_status"] == 1 else "Non-Venomous"
|
| 44 |
-
antivenom = row["antivenom Name"]
|
| 45 |
-
habitat = row.get("region", "Not available")
|
| 46 |
-
return venom_status, antivenom, habitat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|