File size: 1,521 Bytes
5457740 41d5ab8 5457740 41d5ab8 |
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 44 45 46 |
import streamlit as st
from PIL import Image
from ultralytics import YOLO
import torch
st.set_page_config(page_title="Animal Detection App", layout="centered")
# Load YOLOv8 model
@st.cache_resource
def load_model():
return YOLO("yolov8s.pt")
model = load_model()
st.title("🐾 Animal Detection App")
st.write("Upload an image and let the YOLOv8 model detect animals!")
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file:
image = Image.open(uploaded_file).convert("RGB")
st.image(image, caption="Uploaded Image", use_column_width=True)
with st.spinner("Detecting..."):
results = model(image)
# Display detection results
for r in results:
rendered_img = r.plot() # r.plot() gives the image with detections
st.image(rendered_img, caption="Detected Image", use_container_width=True)
result_img = Image.fromarray(results[0].plot()[:, :, ::-1])
st.image(result_img, caption="Detected Animals", use_column_width=True)
# Filter animal predictions
animal_labels = ["cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "bird"]
names = model.names
detections = results[0].boxes.data.cpu().numpy()
st.subheader("Detections:")
for det in detections:
class_id = int(det[5])
label = names[class_id]
if label in animal_labels:
st.markdown(f"- **{label}** (Confidence: {det[4]:.2f})")
|