Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
#
|
| 2 |
|
| 3 |
import streamlit as st
|
| 4 |
import cv2
|
|
@@ -7,39 +7,21 @@ from ultralytics import YOLO
|
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
# -------------------------------
|
| 10 |
-
# Streamlit
|
| 11 |
# -------------------------------
|
| 12 |
-
st.
|
| 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
|
| 32 |
-
# -------------------------------
|
| 33 |
@st.cache_resource
|
| 34 |
-
def load_model(model_name
|
| 35 |
return YOLO(model_name)
|
| 36 |
|
| 37 |
-
model = load_model()
|
| 38 |
-
|
| 39 |
# -------------------------------
|
| 40 |
# Upload Image
|
| 41 |
# -------------------------------
|
| 42 |
-
uploaded_file = st.file_uploader("
|
| 43 |
|
| 44 |
if uploaded_file is not None:
|
| 45 |
# Load uploaded image
|
|
@@ -47,12 +29,15 @@ if uploaded_file is not None:
|
|
| 47 |
img_array = np.array(image)
|
| 48 |
|
| 49 |
# Perform Inference
|
| 50 |
-
|
| 51 |
-
results = model(img_array)
|
| 52 |
|
| 53 |
# Annotated results
|
| 54 |
annotated_img = results[0].plot()
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# Display Input and Output
|
| 57 |
col1, col2 = st.columns(2)
|
| 58 |
with col1:
|
|
@@ -60,11 +45,8 @@ if uploaded_file is not None:
|
|
| 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)
|
| 68 |
cv2.imwrite("annotated_output.jpg", result_bgr)
|
| 69 |
with open("annotated_output.jpg", "rb") as f:
|
| 70 |
-
st.download_button("
|
|
|
|
| 1 |
+
# streamlit_yolo_app.py
|
| 2 |
|
| 3 |
import streamlit as st
|
| 4 |
import cv2
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
# -------------------------------
|
| 10 |
+
# Streamlit App Title
|
| 11 |
# -------------------------------
|
| 12 |
+
st.title("YOLOv8 Object Detection")
|
| 13 |
+
model = YOLO("yolov8n.pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# -------------------------------
|
| 16 |
# Load YOLO Model
|
|
|
|
| 17 |
@st.cache_resource
|
| 18 |
+
def load_model(model_name):
|
| 19 |
return YOLO(model_name)
|
| 20 |
|
|
|
|
|
|
|
| 21 |
# -------------------------------
|
| 22 |
# Upload Image
|
| 23 |
# -------------------------------
|
| 24 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 25 |
|
| 26 |
if uploaded_file is not None:
|
| 27 |
# Load uploaded image
|
|
|
|
| 29 |
img_array = np.array(image)
|
| 30 |
|
| 31 |
# Perform Inference
|
| 32 |
+
results = model(img_array)
|
|
|
|
| 33 |
|
| 34 |
# Annotated results
|
| 35 |
annotated_img = results[0].plot()
|
| 36 |
|
| 37 |
+
# Get shape of annotated image
|
| 38 |
+
h, w, c = annotated_img.shape
|
| 39 |
+
st.write(f"**Annotated Image Shape:** {h} x {w} x {c}")
|
| 40 |
+
|
| 41 |
# Display Input and Output
|
| 42 |
col1, col2 = st.columns(2)
|
| 43 |
with col1:
|
|
|
|
| 45 |
with col2:
|
| 46 |
st.image(annotated_img, caption="Detected Objects", use_column_width=True)
|
| 47 |
|
|
|
|
|
|
|
|
|
|
| 48 |
# Option to download the annotated image
|
| 49 |
result_bgr = cv2.cvtColor(annotated_img, cv2.COLOR_RGB2BGR)
|
| 50 |
cv2.imwrite("annotated_output.jpg", result_bgr)
|
| 51 |
with open("annotated_output.jpg", "rb") as f:
|
| 52 |
+
st.download_button("Download Annotated Image", f, "annotated_output.jpg")
|