app that detects objects using YOLO
Drop files here
or click to upload
import streamlit as st
from ultralytics import YOLO
from PIL import Image
import numpy as np
st.title("Object Detection with YOLO")
# Load the YOLO model
@st.cache_resource
def load_model():
return YOLO('yolov8n.pt')
model = load_model()
# File uploader
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Convert the file to an opencv image.
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image', use_column_width=True)
if st.button('Detect Objects'):
st.write("Detecting objects...")
# Perform prediction
results = model.predict(image)
# Display results
for result in results:
# Plot results image
st.image(result.plot(), caption='Detected Objects', use_column_width=True)
# Display detection information
boxes = result.boxes
if len(boxes) > 0:
st.write("Detected Objects:")
for box in boxes:
confidence = box.conf[0]
class_id = box.cls[0]
class_name = model.names[int(class_id)]
st.write(f"- {class_name} (Confidence: {confidence:.2f})")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?