File size: 1,333 Bytes
e51e579
 
 
 
 
 
 
 
 
48755c1
 
 
9153607
 
e51e579
9153607
 
e51e579
88d4e8e
 
d54db78
 
 
 
88d4e8e
 
 
 
 
e51e579
8169027
 
 
 
0893340
8169027
 
 
e51e579
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
import cv2
import gradio as gr
from ultralytics import YOLO

# Load the pretrained YOLOv8n model
model = YOLO('yolov8n.pt')

# Define the prediction function
def predict(image):
    if image is None:
        return None, "No image uploaded."

    # Perform detection
    results = model(image)
    
    # Annotate the image with detection results
    annotated_frame = results[0].plot()
    
    # Extract labels and confidence scores
    descriptions = []
    for det in results[0].boxes:
        label = det.cls[0].item()  # Class label
        confidence = det.conf[0].item()  # Confidence score
        descriptions.append(f"{model.names[int(label)]}: {confidence:.2f}")
    
    # Join descriptions into a single string
    description_text = "\n".join(descriptions)
    
    return annotated_frame, description_text

# Create a Gradio interface
demo = gr.Interface(
    fn=predict, 
    inputs=gr.Image(type="numpy", label="Upload an Image"), 
    outputs=[gr.Image(type="numpy", label="Detection Result"), gr.Textbox(label="Detection Description")],
    title="Object Detection System",
    description="Upload an image to detect objects. Note: This model is a prototype and may not always be accurate. Results can vary based on input and other factors."
)

# Launch the interface
if __name__ == "__main__":
    demo.launch()