Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from PIL import Image
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
st.title("YOLO Object Detection with Streamlit")
|
| 10 |
-
st.write("Upload an image
|
| 11 |
|
| 12 |
# Upload image
|
| 13 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
|
@@ -15,15 +19,28 @@ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png
|
|
| 15 |
if uploaded_file is not None:
|
| 16 |
# Convert uploaded file to PIL image
|
| 17 |
image = Image.open(uploaded_file).convert("RGB")
|
| 18 |
-
st.image(image, caption="Uploaded Image",
|
| 19 |
|
| 20 |
-
#
|
| 21 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
|
| 22 |
temp_path = tmp_file.name
|
| 23 |
image.save(temp_path)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
# Run YOLO inference
|
| 26 |
-
results = model(temp_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
|
|
|
| 1 |
+
import warnings
|
| 2 |
+
warnings.filterwarnings("ignore")
|
| 3 |
+
|
| 4 |
import streamlit as st
|
| 5 |
from ultralytics import YOLO
|
| 6 |
from PIL import Image
|
| 7 |
import tempfile
|
| 8 |
import os
|
| 9 |
|
| 10 |
+
# Load model
|
| 11 |
+
model = YOLO('yolov8n.pt') # You can change to yolov10s.pt or custom weights
|
| 12 |
|
| 13 |
+
st.title("🧠 YOLO Object Detection with Streamlit")
|
| 14 |
+
st.write("Upload an image to run real-time object detection.")
|
| 15 |
|
| 16 |
# Upload image
|
| 17 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
|
|
|
| 19 |
if uploaded_file is not None:
|
| 20 |
# Convert uploaded file to PIL image
|
| 21 |
image = Image.open(uploaded_file).convert("RGB")
|
| 22 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 23 |
|
| 24 |
+
# Save to a temporary file for YOLO inference
|
| 25 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
|
| 26 |
temp_path = tmp_file.name
|
| 27 |
image.save(temp_path)
|
| 28 |
|
| 29 |
+
# Confidence threshold
|
| 30 |
+
conf = st.slider("Confidence Threshold", 0.0, 1.0, 0.25)
|
| 31 |
+
|
| 32 |
# Run YOLO inference
|
| 33 |
+
results = model(temp_path, conf=conf)
|
| 34 |
+
|
| 35 |
+
# Display detection results
|
| 36 |
+
st.image(results[0].plot(), caption="Detected Objects", use_column_width=True)
|
| 37 |
+
|
| 38 |
+
# Detection details
|
| 39 |
+
with st.expander("Detection Details"):
|
| 40 |
+
for box in results[0].boxes:
|
| 41 |
+
cls = model.names[int(box.cls)]
|
| 42 |
+
conf = float(box.conf)
|
| 43 |
+
st.write(f"**{cls}** — Confidence: {conf:.2f}")
|
| 44 |
|
| 45 |
+
# Clean up temp file
|
| 46 |
+
os.remove(temp_path)
|