Gagan0141 commited on
Commit
ab112af
·
verified ·
1 Parent(s): f89f744

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -8
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
- model = YOLO('yolov8n.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"])
@@ -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", 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')
 
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)