Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from PIL import Image
|
| 4 |
-
|
| 5 |
-
# Import FastAPI app
|
| 6 |
-
from api_fastapi import app as fastapi_app
|
| 7 |
|
| 8 |
# =====================================
|
| 9 |
-
# LOAD MODEL
|
| 10 |
# =====================================
|
| 11 |
MODEL_PATH = "best.pt"
|
| 12 |
model = YOLO(MODEL_PATH)
|
| 13 |
|
| 14 |
-
CLASS_NAMES = model.names
|
| 15 |
IMG_SIZE = 640
|
| 16 |
CONF_THRESH = 0.15
|
| 17 |
|
| 18 |
-
print("✅ YOLOv8 model loaded
|
| 19 |
|
| 20 |
# =====================================
|
| 21 |
-
# PREDICTION FUNCTION
|
| 22 |
# =====================================
|
| 23 |
def predict(image: Image.Image):
|
| 24 |
if image is None:
|
| 25 |
return {"error": "No image provided"}
|
| 26 |
|
|
|
|
| 27 |
results = model.predict(
|
| 28 |
source=image,
|
| 29 |
imgsz=IMG_SIZE,
|
| 30 |
conf=CONF_THRESH,
|
| 31 |
-
device="cpu",
|
| 32 |
verbose=False
|
| 33 |
)
|
| 34 |
|
|
@@ -44,6 +42,7 @@ def predict(image: Image.Image):
|
|
| 44 |
}
|
| 45 |
}
|
| 46 |
|
|
|
|
| 47 |
idx = r.boxes.conf.argmax()
|
| 48 |
cls_id = int(r.boxes.cls[idx])
|
| 49 |
conf = float(r.boxes.conf[idx])
|
|
@@ -52,7 +51,7 @@ def predict(image: Image.Image):
|
|
| 52 |
CLASS_NAMES[0]: 0.0,
|
| 53 |
CLASS_NAMES[1]: 0.0
|
| 54 |
}
|
| 55 |
-
probs[CLASS_NAMES[cls_id]] =
|
| 56 |
|
| 57 |
return {
|
| 58 |
"prediction": CLASS_NAMES[cls_id],
|
|
@@ -61,9 +60,9 @@ def predict(image: Image.Image):
|
|
| 61 |
}
|
| 62 |
|
| 63 |
# =====================================
|
| 64 |
-
# GRADIO
|
| 65 |
# =====================================
|
| 66 |
-
|
| 67 |
fn=predict,
|
| 68 |
inputs=gr.Image(type="pil", label="Upload O-Ring Image"),
|
| 69 |
outputs=gr.JSON(label="Prediction"),
|
|
@@ -75,19 +74,11 @@ gradio_app = gr.Interface(
|
|
| 75 |
"- pelise\n\n"
|
| 76 |
"Input images are resized to 640×640 automatically."
|
| 77 |
),
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
# =====================================
|
| 81 |
-
# MOUNT FASTAPI INTO GRADIO
|
| 82 |
-
# =====================================
|
| 83 |
-
app = gr.mount_gradio_app(
|
| 84 |
-
app=fastapi_app,
|
| 85 |
-
blocks=gradio_app,
|
| 86 |
-
path="/api"
|
| 87 |
)
|
| 88 |
|
| 89 |
# =====================================
|
| 90 |
-
#
|
| 91 |
# =====================================
|
| 92 |
if __name__ == "__main__":
|
| 93 |
-
app.launch(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from PIL import Image
|
| 4 |
+
import numpy as np
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# =====================================
|
| 7 |
+
# LOAD MODEL
|
| 8 |
# =====================================
|
| 9 |
MODEL_PATH = "best.pt"
|
| 10 |
model = YOLO(MODEL_PATH)
|
| 11 |
|
| 12 |
+
CLASS_NAMES = model.names # {0: 'paregi', 1: 'pelise'}
|
| 13 |
IMG_SIZE = 640
|
| 14 |
CONF_THRESH = 0.15
|
| 15 |
|
| 16 |
+
print("✅ YOLOv8 model loaded")
|
| 17 |
|
| 18 |
# =====================================
|
| 19 |
+
# PREDICTION FUNCTION
|
| 20 |
# =====================================
|
| 21 |
def predict(image: Image.Image):
|
| 22 |
if image is None:
|
| 23 |
return {"error": "No image provided"}
|
| 24 |
|
| 25 |
+
# Run inference
|
| 26 |
results = model.predict(
|
| 27 |
source=image,
|
| 28 |
imgsz=IMG_SIZE,
|
| 29 |
conf=CONF_THRESH,
|
|
|
|
| 30 |
verbose=False
|
| 31 |
)
|
| 32 |
|
|
|
|
| 42 |
}
|
| 43 |
}
|
| 44 |
|
| 45 |
+
# Pick highest-confidence detection
|
| 46 |
idx = r.boxes.conf.argmax()
|
| 47 |
cls_id = int(r.boxes.cls[idx])
|
| 48 |
conf = float(r.boxes.conf[idx])
|
|
|
|
| 51 |
CLASS_NAMES[0]: 0.0,
|
| 52 |
CLASS_NAMES[1]: 0.0
|
| 53 |
}
|
| 54 |
+
probs[CLASS_NAMES[cls_id]] = conf
|
| 55 |
|
| 56 |
return {
|
| 57 |
"prediction": CLASS_NAMES[cls_id],
|
|
|
|
| 60 |
}
|
| 61 |
|
| 62 |
# =====================================
|
| 63 |
+
# GRADIO UI
|
| 64 |
# =====================================
|
| 65 |
+
app = gr.Interface(
|
| 66 |
fn=predict,
|
| 67 |
inputs=gr.Image(type="pil", label="Upload O-Ring Image"),
|
| 68 |
outputs=gr.JSON(label="Prediction"),
|
|
|
|
| 74 |
"- pelise\n\n"
|
| 75 |
"Input images are resized to 640×640 automatically."
|
| 76 |
),
|
| 77 |
+
examples=None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
)
|
| 79 |
|
| 80 |
# =====================================
|
| 81 |
+
# LAUNCH
|
| 82 |
# =====================================
|
| 83 |
if __name__ == "__main__":
|
| 84 |
+
app.launch()
|