| 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") |
|
|
| |
| @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) |
|
|
| |
| for r in results: |
| rendered_img = r.plot() |
| 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) |
|
|
| |
| 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})") |
|
|