Repository navigation
Expand file tree
/
Copy pathsimple_app.py
More file actions
151 lines (123 loc) · 4.82 KB
/
Copy pathsimple_app.py
File metadata and controls
151 lines (123 loc) · 4.82 KB
1
2
3
4
5
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import streamlit as st
import cv2
import numpy as np
from PIL import Image
from pathlib import Path
from simple_yolo_detector import SimpleYOLODetector
# Streamlit configuration
st.set_page_config(page_title="YOLO26 Multi-Task", layout="wide")
# Sidebar
st.sidebar.title("Configuration")
task = st.sidebar.selectbox(
"Select Task",
["detect", "segment", "classify"],
help="Choose the computer vision task"
)
model_size = st.sidebar.selectbox(
"Select Model Size",
["nano", "small", "medium", "large", "xlarge"],
index=0, # Changed from 2 to 0 (nano instead of medium) for faster AWS startup
help="Model size - smaller is faster, larger is more accurate"
)
# Task-specific parameters
if task in ["detect", "segment"]:
confidence = st.sidebar.slider(
"Confidence Threshold",
min_value=0.01,
max_value=1.0,
value=0.25,
step=0.01,
help="Minimum confidence score"
)
elif task == "classify":
topk = st.sidebar.slider(
"Number of Top Predictions",
min_value=1,
max_value=10,
value=5,
step=1,
help="Number of predictions to display"
)
# Load model with caching
@st.cache_resource
def load_model(task_type, size):
"""Load and cache YOLO model."""
with st.spinner(f"Loading {size} model for {task_type}... (may download if not cached)"):
try:
return SimpleYOLODetector(task=task_type, model_size=size)
except Exception as e:
st.error(f"Failed to load model: {str(e)}")
raise
# Force reload when task changes
if "last_task" not in st.session_state:
st.session_state.last_task = task
elif st.session_state.last_task != task:
load_model.clear()
st.session_state.last_task = task
yolo = load_model(task, model_size)
# Main UI
st.title(f"YOLO26 {task.capitalize()}")
# Task info
if task == "detect":
st.info("🔍 **Object Detection**: Identify and locate objects with bounding boxes")
elif task == "segment":
st.info("🎨 **Instance Segmentation**: Identify objects and their pixel-level masks")
elif task == "classify":
st.info("🏷️ **Image Classification**: Classify the image into ImageNet classes")
# File uploader
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
try:
# Load image
image = Image.open(uploaded_file)
if image.mode != "RGB":
image = image.convert("RGB")
# Convert to numpy BGR
image_np = np.array(image, dtype=np.uint8)
image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
# Process based on task
if task == "detect":
result_image, detections = yolo.detect_objects(image_np)
# Display
result_rgb = cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)
st.image(result_rgb, caption="Detected Objects", use_container_width=True)
if detections:
st.write("### Detection Results")
for i, det in enumerate(detections, 1):
st.write(f"{i}. **{det['label']}** - Confidence: {det['confidence']:.2%}")
else:
st.warning("No objects detected")
elif task == "segment":
result_image, detections = yolo.segment_objects(image_np)
# Display
result_rgb = cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)
st.image(result_rgb, caption="Segmented Objects", use_container_width=True)
if detections:
st.write("### Segmentation Results")
for i, det in enumerate(detections, 1):
st.write(f"{i}. **{det['label']}** - Confidence: {det['confidence']:.2%}")
else:
st.warning("No objects segmented")
elif task == "classify":
result_image, predictions = yolo.classify_image(image_np, topk=topk)
# Display
result_rgb = cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)
st.image(result_rgb, caption="Classification Results", use_container_width=True)
if predictions:
st.write("### Classification Results")
for i, pred in enumerate(predictions, 1):
st.write(f"{i}. **{pred['label']}** - Confidence: {pred['confidence']:.2%}")
# Download button
pred_dir = Path(__file__).parent / "pred_images"
pred_dir.mkdir(exist_ok=True)
save_path = pred_dir / f"{task}_{uploaded_file.name}"
cv2.imwrite(str(save_path), result_image)
with open(save_path, "rb") as f:
st.download_button(
label="Download Result",
data=f,
file_name=save_path.name,
mime="image/jpeg"
)
except Exception as e:
st.error(f"Error processing image: {str(e)}")