Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Load YOLO model
|
| 6 |
+
model = YOLO("best.pt") # keep best.pt in the same folder
|
| 7 |
+
|
| 8 |
+
def predict(image):
|
| 9 |
+
# Run YOLO inference
|
| 10 |
+
results = model.predict(image, save=False)
|
| 11 |
+
|
| 12 |
+
# Plot results (draw bounding boxes)
|
| 13 |
+
result_image = Image.fromarray(results[0].plot()[:, :, ::-1]) # BGR → RGB
|
| 14 |
+
|
| 15 |
+
# Collect labels and confidence scores
|
| 16 |
+
labels = []
|
| 17 |
+
for box in results[0].boxes:
|
| 18 |
+
cls = results[0].names[int(box.cls)]
|
| 19 |
+
conf = float(box.conf)
|
| 20 |
+
labels.append(f"{cls}: {conf:.2f}")
|
| 21 |
+
|
| 22 |
+
return result_image, "\n".join(labels) if labels else "No objects detected"
|
| 23 |
+
|
| 24 |
+
# Gradio UI
|
| 25 |
+
demo = gr.Interface(
|
| 26 |
+
fn=predict,
|
| 27 |
+
inputs=gr.Image(type="pil"),
|
| 28 |
+
outputs=[gr.Image(type="pil"), gr.Textbox(label="Detections")],
|
| 29 |
+
title="YOLOv8 Object Detection",
|
| 30 |
+
description="Upload an image to detect objects with bounding boxes and confidence scores."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
demo.launch()
|