saurabh75's picture
Update app.py
48755c1 verified
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()