Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ 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
|
|
@@ -13,25 +12,20 @@ import pandas as pd
|
|
| 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
|
| 19 |
st.markdown(
|
| 20 |
"""
|
| 21 |
-
|
| 22 |
-
You can adjust detection parameters in the sidebar.
|
| 23 |
-
"""
|
| 24 |
-
)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 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 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
# -------------------------------
|
| 37 |
# Load YOLO Model
|
|
@@ -52,8 +46,9 @@ if uploaded_file is not None:
|
|
| 52 |
image = Image.open(uploaded_file).convert("RGB")
|
| 53 |
img_array = np.array(image)
|
| 54 |
|
|
|
|
| 55 |
with st.spinner("π Detecting objects..."):
|
| 56 |
-
results = model
|
| 57 |
|
| 58 |
# Annotated results
|
| 59 |
annotated_img = results[0].plot()
|
|
@@ -65,28 +60,8 @@ if uploaded_file is not None:
|
|
| 65 |
with col2:
|
| 66 |
st.image(annotated_img, caption="Detected Objects", use_column_width=True)
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 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)
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
from ultralytics import YOLO
|
| 7 |
from PIL import Image
|
|
|
|
| 8 |
|
| 9 |
# -------------------------------
|
| 10 |
# Streamlit Page Config
|
|
|
|
| 12 |
st.set_page_config(page_title="YOLOv8 Object Detection", page_icon="π€", layout="wide")
|
| 13 |
|
| 14 |
# -------------------------------
|
| 15 |
+
# App Title & Description
|
| 16 |
# -------------------------------
|
| 17 |
+
st.title("π€ YOLOv8 Object Detection")
|
| 18 |
st.markdown(
|
| 19 |
"""
|
| 20 |
+
Welcome to the **YOLOv8 Object Detection App** π―
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
- Upload an image and let YOLOv8 detect objects in real-time.
|
| 23 |
+
- This demo is built with **[Ultralytics YOLOv8](https://docs.ultralytics.com/)** and **[Streamlit](https://streamlit.io/)**.
|
| 24 |
+
- Download the annotated image with detected objects.
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
---
|
| 27 |
+
"""
|
| 28 |
+
)
|
| 29 |
|
| 30 |
# -------------------------------
|
| 31 |
# Load YOLO Model
|
|
|
|
| 46 |
image = Image.open(uploaded_file).convert("RGB")
|
| 47 |
img_array = np.array(image)
|
| 48 |
|
| 49 |
+
# Perform Inference
|
| 50 |
with st.spinner("π Detecting objects..."):
|
| 51 |
+
results = model(img_array)
|
| 52 |
|
| 53 |
# Annotated results
|
| 54 |
annotated_img = results[0].plot()
|
|
|
|
| 60 |
with col2:
|
| 61 |
st.image(annotated_img, caption="Detected Objects", use_column_width=True)
|
| 62 |
|
| 63 |
+
# Show some info about the result
|
| 64 |
+
st.success(f"β
Objects detected: {len(results[0].boxes)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Option to download the annotated image
|
| 67 |
result_bgr = cv2.cvtColor(annotated_img, cv2.COLOR_RGB2BGR)
|