Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,23 +4,36 @@ from ultralytics import YOLO
|
|
| 4 |
import torch
|
| 5 |
|
| 6 |
# ------------------------
|
| 7 |
-
#
|
| 8 |
# ------------------------
|
| 9 |
MODEL_PATH = "best.pt"
|
| 10 |
model = YOLO(MODEL_PATH)
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# ------------------------
|
| 14 |
-
#
|
| 15 |
# ------------------------
|
| 16 |
def predict_orientation(image: Image.Image):
|
| 17 |
-
# inference
|
| 18 |
results = model.predict(source=image, device="cpu", imgsz=224, verbose=False)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
probs = results[0].probs.data.cpu()
|
| 22 |
pred = torch.argmax(probs)
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
confidence = round(probs[pred].item(), 2)
|
| 25 |
|
| 26 |
return f"Orientation: {orientation} | Confidence: {confidence}"
|
|
|
|
| 4 |
import torch
|
| 5 |
|
| 6 |
# ------------------------
|
| 7 |
+
# Load the YOLOv11 classification model
|
| 8 |
# ------------------------
|
| 9 |
MODEL_PATH = "best.pt"
|
| 10 |
model = YOLO(MODEL_PATH)
|
| 11 |
+
|
| 12 |
+
# Mapping from model's short labels to full orientation names
|
| 13 |
+
ORIENTATION_MAP = {
|
| 14 |
+
"ax": "axial",
|
| 15 |
+
"co": "coaxial",
|
| 16 |
+
"sa": "sagittal"
|
| 17 |
+
}
|
| 18 |
|
| 19 |
# ------------------------
|
| 20 |
+
# Prediction function
|
| 21 |
# ------------------------
|
| 22 |
def predict_orientation(image: Image.Image):
|
| 23 |
+
# Perform inference using the model
|
| 24 |
results = model.predict(source=image, device="cpu", imgsz=224, verbose=False)
|
| 25 |
|
| 26 |
+
# Extract probabilities
|
| 27 |
probs = results[0].probs.data.cpu()
|
| 28 |
pred = torch.argmax(probs)
|
| 29 |
+
|
| 30 |
+
# Get the original class label from the model
|
| 31 |
+
original_class = model.names[pred.item()] # "ax", "co", or "sa"
|
| 32 |
+
|
| 33 |
+
# Map to full orientation name
|
| 34 |
+
orientation = ORIENTATION_MAP.get(original_class, original_class)
|
| 35 |
+
|
| 36 |
+
# Get confidence score
|
| 37 |
confidence = round(probs[pred].item(), 2)
|
| 38 |
|
| 39 |
return f"Orientation: {orientation} | Confidence: {confidence}"
|