Srikanth0804 commited on
Commit
6f05dea
Β·
verified Β·
1 Parent(s): 081a4bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -54
app.py CHANGED
@@ -1,54 +1,95 @@
1
- # streamlit_yolo_segmentation.py
2
-
3
- import streamlit as st
4
- import cv2
5
- import numpy as np
6
- from ultralytics import YOLO
7
- from PIL import Image
8
-
9
- # -------------------------------
10
- # Streamlit App Title
11
- # -------------------------------
12
- st.title("YOLOv8 Image Segmentation")
13
-
14
- # -------------------------------
15
- # Load YOLO Segmentation Model
16
- # -------------------------------
17
- @st.cache_resource
18
- def load_model(model_name="yolov8n-seg.pt"):
19
- return YOLO(model_name)
20
-
21
- model = load_model()
22
-
23
- # -------------------------------
24
- # Upload Image
25
- # -------------------------------
26
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
27
-
28
- if uploaded_file is not None:
29
- # Load uploaded image
30
- image = Image.open(uploaded_file).convert("RGB")
31
- img_array = np.array(image)
32
-
33
- # Perform Inference
34
- results = model(img_array)
35
-
36
- # Annotated results with masks
37
- annotated_img = results[0].plot()
38
-
39
- # Get shape of annotated image
40
- h, w, c = annotated_img.shape
41
- st.write(f"**Annotated Image Shape:** {h} x {w} x {c}")
42
-
43
- # Display Input and Output
44
- col1, col2 = st.columns(2)
45
- with col1:
46
- st.image(image, caption="Original Image", use_column_width=True)
47
- with col2:
48
- st.image(annotated_img, caption="Segmented Image", use_column_width=True)
49
-
50
- # Option to download the annotated image
51
- result_bgr = cv2.cvtColor(annotated_img, cv2.COLOR_RGB2BGR)
52
- cv2.imwrite("segmented_output.jpg", result_bgr)
53
- with open("segmented_output.jpg", "rb") as f:
54
- st.download_button("Download Segmented Image", f, "segmented_output.jpg")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
+ import streamlit as st
4
+ 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
12
+ # -------------------------------
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 App")
19
+ st.markdown(
20
+ """
21
+ Upload an image and let **YOLOv8** detect objects in real-time.
22
+ You can adjust detection parameters in the sidebar.
23
+ """
24
+ )
25
+
26
+ # -------------------------------
27
+ # Sidebar Controls
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
+ st.sidebar.markdown("---")
34
+ st.sidebar.info("Built with [YOLOv8](https://docs.ultralytics.com/) and [Streamlit](https://streamlit.io/)")
35
+
36
+ # -------------------------------
37
+ # Load YOLO Model
38
+ # -------------------------------
39
+ @st.cache_resource
40
+ def load_model(model_name="yolov8n.pt"):
41
+ return YOLO(model_name)
42
+
43
+ model = load_model()
44
+
45
+ # -------------------------------
46
+ # Upload Image
47
+ # -------------------------------
48
+ uploaded_file = st.file_uploader("πŸ“· Upload an image", type=["jpg", "jpeg", "png"])
49
+
50
+ if uploaded_file is not None:
51
+ # Load uploaded image
52
+ image = Image.open(uploaded_file).convert("RGB")
53
+ img_array = np.array(image)
54
+
55
+ with st.spinner("πŸ” Detecting objects..."):
56
+ results = model.predict(img_array, conf=confidence, iou=iou)
57
+
58
+ # Annotated results
59
+ annotated_img = results[0].plot()
60
+
61
+ # Display Input and Output
62
+ col1, col2 = st.columns(2)
63
+ with col1:
64
+ st.image(image, caption="Original Image", use_column_width=True)
65
+ with col2:
66
+ st.image(annotated_img, caption="Detected Objects", use_column_width=True)
67
+
68
+ # -------------------------------
69
+ # Show Detection Details
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)
93
+ cv2.imwrite("annotated_output.jpg", result_bgr)
94
+ with open("annotated_output.jpg", "rb") as f:
95
+ st.download_button("πŸ“₯ Download Annotated Image", f, "annotated_output.jpg")