Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="Animal Detection App", layout="centered")
|
| 7 |
+
|
| 8 |
+
# Load YOLOv8 model
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def load_model():
|
| 11 |
+
return YOLO("yolov8s.pt")
|
| 12 |
+
|
| 13 |
+
model = load_model()
|
| 14 |
+
|
| 15 |
+
st.title("🐾 Animal Detection App")
|
| 16 |
+
st.write("Upload an image and let the YOLOv8 model detect animals!")
|
| 17 |
+
|
| 18 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 19 |
+
|
| 20 |
+
if uploaded_file:
|
| 21 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 22 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 23 |
+
|
| 24 |
+
with st.spinner("Detecting..."):
|
| 25 |
+
results = model(image)
|
| 26 |
+
|
| 27 |
+
# Display detection results
|
| 28 |
+
results.render() # Draw on image
|
| 29 |
+
result_img = Image.fromarray(results[0].plot()[:, :, ::-1])
|
| 30 |
+
st.image(result_img, caption="Detected Animals", use_column_width=True)
|
| 31 |
+
|
| 32 |
+
# Filter animal predictions
|
| 33 |
+
animal_labels = ["cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "bird"]
|
| 34 |
+
names = model.names
|
| 35 |
+
detections = results[0].boxes.data.cpu().numpy()
|
| 36 |
+
|
| 37 |
+
st.subheader("Detections:")
|
| 38 |
+
for det in detections:
|
| 39 |
+
class_id = int(det[5])
|
| 40 |
+
label = names[class_id]
|
| 41 |
+
if label in animal_labels:
|
| 42 |
+
st.markdown(f"- **{label}** (Confidence: {det[4]:.2f})")
|