Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,95 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
| 3 |
-
import streamlit as st
|
| 4 |
-
import cv2
|
| 5 |
-
import numpy as np
|
| 6 |
-
from ultralytics import YOLO
|
| 7 |
-
from PIL import Image
|
| 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 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
from ultralytics import YOLO
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import pandas as pd
|
| 9 |
+
|
| 10 |
+
# -------------------------------
|
| 11 |
+
# Streamlit Page Config
|
| 12 |
+
# -------------------------------
|
| 13 |
+
st.set_page_config(page_title="YOLOv8 Object Detection", page_icon="π€", layout="wide")
|
| 14 |
+
|
| 15 |
+
# -------------------------------
|
| 16 |
+
# App Title
|
| 17 |
+
# -------------------------------
|
| 18 |
+
st.title("π€ YOLOv8 Object Detection App")
|
| 19 |
+
st.markdown(
|
| 20 |
+
"""
|
| 21 |
+
Upload an image and let **YOLOv8** detect objects in real-time.
|
| 22 |
+
You can adjust detection parameters in the sidebar.
|
| 23 |
+
"""
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# -------------------------------
|
| 27 |
+
# Sidebar Controls
|
| 28 |
+
# -------------------------------
|
| 29 |
+
st.sidebar.header("βοΈ Detection Settings")
|
| 30 |
+
confidence = st.sidebar.slider("Confidence Threshold", 0.1, 1.0, 0.25, 0.05)
|
| 31 |
+
iou = st.sidebar.slider("IoU Threshold", 0.1, 1.0, 0.45, 0.05)
|
| 32 |
+
|
| 33 |
+
st.sidebar.markdown("---")
|
| 34 |
+
st.sidebar.info("Built with [YOLOv8](https://docs.ultralytics.com/) and [Streamlit](https://streamlit.io/)")
|
| 35 |
+
|
| 36 |
+
# -------------------------------
|
| 37 |
+
# Load YOLO Model
|
| 38 |
+
# -------------------------------
|
| 39 |
+
@st.cache_resource
|
| 40 |
+
def load_model(model_name="yolov8n.pt"):
|
| 41 |
+
return YOLO(model_name)
|
| 42 |
+
|
| 43 |
+
model = load_model()
|
| 44 |
+
|
| 45 |
+
# -------------------------------
|
| 46 |
+
# Upload Image
|
| 47 |
+
# -------------------------------
|
| 48 |
+
uploaded_file = st.file_uploader("π· Upload an image", type=["jpg", "jpeg", "png"])
|
| 49 |
+
|
| 50 |
+
if uploaded_file is not None:
|
| 51 |
+
# Load uploaded image
|
| 52 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 53 |
+
img_array = np.array(image)
|
| 54 |
+
|
| 55 |
+
with st.spinner("π Detecting objects..."):
|
| 56 |
+
results = model.predict(img_array, conf=confidence, iou=iou)
|
| 57 |
+
|
| 58 |
+
# Annotated results
|
| 59 |
+
annotated_img = results[0].plot()
|
| 60 |
+
|
| 61 |
+
# Display Input and Output
|
| 62 |
+
col1, col2 = st.columns(2)
|
| 63 |
+
with col1:
|
| 64 |
+
st.image(image, caption="Original Image", use_column_width=True)
|
| 65 |
+
with col2:
|
| 66 |
+
st.image(annotated_img, caption="Detected Objects", use_column_width=True)
|
| 67 |
+
|
| 68 |
+
# -------------------------------
|
| 69 |
+
# Show Detection Details
|
| 70 |
+
# -------------------------------
|
| 71 |
+
st.subheader("π Detection Results")
|
| 72 |
+
|
| 73 |
+
if len(results[0].boxes) > 0:
|
| 74 |
+
data = []
|
| 75 |
+
for box in results[0].boxes:
|
| 76 |
+
cls = model.names[int(box.cls)]
|
| 77 |
+
conf = float(box.conf)
|
| 78 |
+
x1, y1, x2, y2 = box.xyxy[0].tolist()
|
| 79 |
+
data.append([cls, round(conf, 3), int(x1), int(y1), int(x2), int(y2)])
|
| 80 |
+
|
| 81 |
+
df = pd.DataFrame(data, columns=["Class", "Confidence", "X1", "Y1", "X2", "Y2"])
|
| 82 |
+
st.dataframe(df, use_container_width=True)
|
| 83 |
+
|
| 84 |
+
# Option to download CSV
|
| 85 |
+
csv = df.to_csv(index=False).encode("utf-8")
|
| 86 |
+
st.download_button("π₯ Download Results as CSV", csv, "detections.csv", "text/csv")
|
| 87 |
+
|
| 88 |
+
else:
|
| 89 |
+
st.warning("No objects detected.")
|
| 90 |
+
|
| 91 |
+
# Option to download the annotated image
|
| 92 |
+
result_bgr = cv2.cvtColor(annotated_img, cv2.COLOR_RGB2BGR)
|
| 93 |
+
cv2.imwrite("annotated_output.jpg", result_bgr)
|
| 94 |
+
with open("annotated_output.jpg", "rb") as f:
|
| 95 |
+
st.download_button("π₯ Download Annotated Image", f, "annotated_output.jpg")
|