shimaa22 commited on
Commit
fa01726
·
verified ·
1 Parent(s): cf5b670

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -6
app.py CHANGED
@@ -4,23 +4,36 @@ from ultralytics import YOLO
4
  import torch
5
 
6
  # ------------------------
7
- # تحميل الموديل (YOLOv11 classification .pt)
8
  # ------------------------
9
  MODEL_PATH = "best.pt"
10
  model = YOLO(MODEL_PATH)
11
- CLASS_NAMES = ["ax", "co", "sa"]
 
 
 
 
 
 
12
 
13
  # ------------------------
14
- # prediction
15
  # ------------------------
16
  def predict_orientation(image: Image.Image):
17
- # inference باستخدام Ultralytics
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
- orientation = CLASS_NAMES[pred.item()]
 
 
 
 
 
 
 
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}"