Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
-
from ultralytics import
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
# ------------------------
|
| 7 |
-
# تحميل الموديل التصنيفي
|
| 8 |
-
# ------------------------
|
| 9 |
MODEL_PATH = "best.pt"
|
| 10 |
-
model =
|
|
|
|
|
|
|
| 11 |
CLASS_NAMES = ["ax", "co", "sa"]
|
| 12 |
|
| 13 |
# ------------------------
|
| 14 |
# prediction
|
| 15 |
# ------------------------
|
| 16 |
def predict_orientation(image: Image.Image):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
orientation = CLASS_NAMES[pred.item()]
|
| 26 |
-
confidence = round(
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
return "Error: Could not get probabilities from model."
|
| 30 |
|
| 31 |
# ------------------------
|
| 32 |
# Gradio Interface
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
+
from ultralytics.nn.tasks import ClassificationModel
|
| 4 |
import torch
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
MODEL_PATH = "best.pt"
|
| 7 |
+
model = ClassificationModel(MODEL_PATH)
|
| 8 |
+
model.eval()
|
| 9 |
+
|
| 10 |
CLASS_NAMES = ["ax", "co", "sa"]
|
| 11 |
|
| 12 |
# ------------------------
|
| 13 |
# prediction
|
| 14 |
# ------------------------
|
| 15 |
def predict_orientation(image: Image.Image):
|
| 16 |
+
import torchvision.transforms as transforms
|
| 17 |
+
transform = transforms.Compose([
|
| 18 |
+
transforms.Resize((224,224)),
|
| 19 |
+
transforms.ToTensor(),
|
| 20 |
+
])
|
| 21 |
+
img_tensor = transform(image).unsqueeze(0) # batch dimension
|
| 22 |
+
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model(img_tensor)
|
| 25 |
+
probs = torch.softmax(outputs, dim=1)
|
| 26 |
+
conf, pred = torch.max(probs, 1)
|
| 27 |
orientation = CLASS_NAMES[pred.item()]
|
| 28 |
+
confidence = round(conf.item(), 2)
|
| 29 |
+
|
| 30 |
+
return f"Orientation: {orientation} | Confidence: {confidence}"
|
|
|
|
| 31 |
|
| 32 |
# ------------------------
|
| 33 |
# Gradio Interface
|