Spaces:
Sleeping
Sleeping
Update 2.py
Browse files
2.py
CHANGED
|
@@ -1,59 +1,29 @@
|
|
| 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 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
st.title("YOLO Object Detection with Streamlit")
|
| 32 |
-
st.write("Upload an image and see object detection results in real-time!")
|
| 33 |
-
|
| 34 |
-
# Upload image
|
| 35 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 36 |
-
|
| 37 |
-
if uploaded_file is not None:
|
| 38 |
-
# Convert uploaded file to PIL image
|
| 39 |
-
image = Image.open(uploaded_file).convert("RGB")
|
| 40 |
-
st.image(image, caption="Uploaded Image", width='stretch')
|
| 41 |
-
|
| 42 |
-
# Temporary save to disk for YOLO inference
|
| 43 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
|
| 44 |
-
temp_path = tmp_file.name
|
| 45 |
-
image.save(temp_path)
|
| 46 |
-
|
| 47 |
-
# Run YOLO inference
|
| 48 |
-
results = model(temp_path)
|
| 49 |
-
|
| 50 |
-
# Show detection results in Streamlit
|
| 51 |
-
st.image(results[0].plot(), caption="Detected Objects", width='stretch')
|
| 52 |
-
|
| 53 |
-
# # Save results to output folder
|
| 54 |
-
# output_dir = "outputs"
|
| 55 |
-
# os.makedirs(output_dir, exist_ok=True)
|
| 56 |
-
# results.save(output_dir)
|
| 57 |
-
# st.write(f"Detection results saved in `{output_dir}/`")
|
| 58 |
-
# Clean up temporary file
|
| 59 |
-
os.remove(temp_path)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
model = YOLO('yolov8s.pt') # or 'yolov10s.pt'
|
| 8 |
+
|
| 9 |
+
st.title("YOLO Object Detection with Streamlit")
|
| 10 |
+
st.write("Upload an image and see object detection results in real-time!")
|
| 11 |
+
|
| 12 |
+
# Upload image
|
| 13 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 14 |
+
|
| 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", width='stretch')
|
| 19 |
+
|
| 20 |
+
# Temporary save to disk for YOLO inference
|
| 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 |
+
# Show detection results in Streamlit
|
| 29 |
+
st.image(results[0].plot(), caption="Detected Objects", width='stretch')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|