File size: 867 Bytes
d11a60e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d6d7a7
 
 
d11a60e
 
 
 
 
 
0d6d7a7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import gradio as gr
from ultralytics import YOLO
from PIL import Image
import numpy as np

# Load YOLOv8 model
model = YOLO("yolov8n.pt")

# Detection function
def detect_objects(image):
    if image is None:
        return None
    if isinstance(image, np.ndarray):
        image = Image.fromarray(image)
    results = model.predict(image)
    result_image = results[0].plot()
    return Image.fromarray(result_image)

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## 🧠 YOLOv8 Object Detection (Image Upload or Camera)")

    image_input = gr.Image(label="📷 Upload or Capture Image", type="numpy", height=300)
    result_output = gr.Image(label="🔍 Detection Result", type="pil", height=300)

    detect_button = gr.Button("🚀 Detect Objects")

    detect_button.click(fn=detect_objects, inputs=image_input, outputs=result_output)

demo.launch()