Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,45 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
|
|
3 |
import torch
|
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 |
-
st.subheader("๐ Encoder Attention Heatmap:")
|
47 |
-
|
48 |
-
fig, ax = plt.subplots(figsize=(10, 6))
|
49 |
-
sns.heatmap(attention, cmap="YlGnBu", ax=ax)
|
50 |
-
ax.set_title("Encoder Self-Attention Heatmap ๐ซ")
|
51 |
-
st.pyplot(fig)
|
|
|
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 |
+
for r in results:
|
29 |
+
rendered_img = r.plot() # r.plot() gives the image with detections
|
30 |
+
st.image(rendered_img, caption="Detected Image", use_container_width=True)
|
31 |
+
|
32 |
+
result_img = Image.fromarray(results[0].plot()[:, :, ::-1])
|
33 |
+
st.image(result_img, caption="Detected Animals", use_column_width=True)
|
34 |
+
|
35 |
+
# Filter animal predictions
|
36 |
+
animal_labels = ["cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "bird"]
|
37 |
+
names = model.names
|
38 |
+
detections = results[0].boxes.data.cpu().numpy()
|
39 |
+
|
40 |
+
st.subheader("Detections:")
|
41 |
+
for det in detections:
|
42 |
+
class_id = int(det[5])
|
43 |
+
label = names[class_id]
|
44 |
+
if label in animal_labels:
|
45 |
+
st.markdown(f"- **{label}** (Confidence: {det[4]:.2f})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|