Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load YOLOv8 model
|
| 7 |
+
model = YOLO("yolov8n.pt")
|
| 8 |
+
|
| 9 |
+
# Detection function
|
| 10 |
+
def detect_objects(image):
|
| 11 |
+
if image is None:
|
| 12 |
+
return None
|
| 13 |
+
if isinstance(image, np.ndarray):
|
| 14 |
+
image = Image.fromarray(image)
|
| 15 |
+
results = model.predict(image)
|
| 16 |
+
result_image = results[0].plot()
|
| 17 |
+
return Image.fromarray(result_image)
|
| 18 |
+
|
| 19 |
+
# Gradio UI
|
| 20 |
+
with gr.Blocks() as demo:
|
| 21 |
+
gr.Markdown("## 🧠 YOLOv8 Object Detection (Image Upload or Camera)")
|
| 22 |
+
|
| 23 |
+
image_input = gr.Image(label="📷 Upload or Capture Image", type="numpy")
|
| 24 |
+
result_output = gr.Image(label="🔍 Detection Result", type="pil")
|
| 25 |
+
|
| 26 |
+
detect_button = gr.Button("🚀 Detect Objects")
|
| 27 |
+
|
| 28 |
+
detect_button.click(fn=detect_objects, inputs=image_input, outputs=result_output)
|
| 29 |
+
|
| 30 |
+
demo.launch()
|