-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
242 lines (204 loc) · 6.2 KB
/
utils.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import streamlit as st
def image_selector() -> str:
"""
Select an image from the predefined list or upload a new image.
Returns:
str: The path of the selected image.
"""
# File uploader
uploaded_file = st.sidebar.file_uploader(
"Upload an image...", type=["jpg", "jpeg", "png", "webp", "bmp"]
)
# Predefined images
image_list = [
"hand1.jpg",
"hand2.jpg",
"hand3.jpg",
"hand4.jpg",
"hand5.jpg",
"hand6.jpg",
"hand7.jpg",
"hand8.jpg",
"hand9.jpg",
"hand10.jpg",
"hand11.jpg",
"hand12.jpg",
"hand13.jpg",
"hand14.jpg",
"hand15.jpg",
"hand16.jpg",
"ai-hand1.jpg",
"ai-hand2.jpg",
"ai-hand3.jpg",
"ai-hand4.jpg",
"ai-hand5.jpg",
"ai-hand6.jpg",
"ai-hand7.jpeg",
"background1.jpg",
"background2.jpg",
"background3.jpg",
"background4.jpg",
"background5.jpg",
"graphic1.jpg",
"graphic2.jpg",
"graphic3.jpg",
"person1.jpg",
"person2.jpg",
"person3.jpg",
"person4.jpg",
"person5.jpg",
]
selected_image = st.sidebar.selectbox("Select an predefined image", image_list)
if uploaded_file is None:
uploaded_file = f"images/{selected_image}"
return uploaded_file
def video_selector() -> str:
"""
Select a video from the predefined list or upload a new video.
Returns:
str: The path of the selected video.
"""
# File uploader
uploaded_file = st.sidebar.file_uploader(
"Upload a video...", type=["mp4", "avi", "mov"]
)
# Predefined videos
video_list = [
"video1.mp4",
"video2.mp4",
"video3.mp4",
"video4.mp4",
"video5.mp4",
"video6.mp4",
"video7.mp4",
"video8.mp4",
"video9.mp4",
"video10.mp4",
"video11.mp4",
"video12.mp4",
"video13.mp4",
]
selected_video = st.sidebar.selectbox("Select a video", video_list)
if uploaded_file is None:
video_path = f"videos/{selected_video}"
else:
video_path = uploaded_file
return video_path
def yolo_classify_sidebar_options() -> tuple:
"""
Sidebar options for YOLO model selection and confidence threshold.
Returns:
tuple: The selected model type and confidence threshold.
"""
st.sidebar.header("Model Configuration")
# Model Options
model_type = st.sidebar.radio(
"Model (more ℹ️ see Home)",
[
"RPS✔️",
"ASL Count✔️",
"ASL Alphabet✔️",
"HaGRID✔️",
"Hands✔️",
],
)
# Selecting model path based on model type
model_paths = {
"RPS✔️": "models/rps_classify.pt",
"ASL Count✔️": "models/asl_count_classify.pt",
"ASL Alphabet✔️": "models/asl_alphabet_classify.pt",
"HaGRID✔️": "models/hagrid_classify.pt",
"Hands✔️": "models/hand_classify.pt",
}
model_path = model_paths[model_type]
return model_path
def yolo_detect_sidebar_options() -> tuple:
"""
Sidebar options for YOLO model selection and confidence threshold.
Returns:
tuple: The selected model type and confidence threshold.
"""
st.sidebar.header("Model Configuration")
# Model Options
model_type = st.sidebar.radio(
"Model (more ℹ️ see Home)",
[
"RPS✔️",
"ASL Alphabet✔️",
"AI Open Palm✔️",
"AI Hands✔️",
"AI Hands2✔️",
],
)
confidence = float(
st.sidebar.slider(
"Confidence Threshold (default is 0.25)", 0.0, 1.0, 0.25, 0.01
)
)
iou = float(
st.sidebar.slider("IoU Threshold (default is 0.7)", 0.0, 1.0, 0.7, 0.01)
)
# Selecting model path based on model type
model_paths = {
"RPS✔️": "models/rps_detect.pt",
"ASL Alphabet✔️": "models/asl_alphabet_detect.pt",
"AI Open Palm✔️": "models/ai_open_palm_detect",
"AI Hands✔️": "models/ai_hands_detect.pt",
"AI Hands2✔️": "models/ai_hands2_detect.pt",
}
model_path = model_paths[model_type]
return model_path, confidence, iou
def mediapipe_sidebar_options() -> tuple:
"""
Sidebar options for YOLO model selection and confidence threshold.
Returns:
tuple: The selected model type and confidence threshold.
"""
st.sidebar.header("Model Configuration")
# Model Options
model_type = st.sidebar.radio(
"Model (more ℹ️ see Home)",
[
"DEFAULT✔️",
"RPS✔️",
"ASL Count✔️",
"ASL Alphabet✔️",
],
)
# Selecting model path based on model type
model_paths = {
"DEFAULT✔️": "models/gesture_recognizer.task",
"RPS✔️": "models/rps.task",
"ASL Count✔️": "models/asl_count.task",
"ASL Alphabet✔️": "models/asl_alphabet.task",
}
model_path = model_paths[model_type]
num_hands = int(
st.sidebar.slider("The maximum number of hands to detect", 0, 10, 1, 1)
)
return model_path, num_hands
def show_inference(speeds) -> None:
md_inference_speed = f"""
## ⏱️ Inference timings / Speed in ms
- Preprocess: :blue-background[{speeds['preprocess']:.2f}] ms
- Inference: :blue-background[{speeds['inference']:.2f}] ms
- Postprocess: :blue-background[{speeds['postprocess']:.2f}] ms
"""
st.markdown(md_inference_speed)
def show_data_objects(
speeds=None, original_shape=None, probabilities=None, class_names=None
) -> None:
st.markdown("## 🗂️ Data Objects")
if class_names is not None:
st.markdown("### Class Names:")
st.write(class_names)
if probabilities is not None:
st.markdown("### Probabilities:")
st.write(probabilities)
if speeds is not None:
st.markdown("### Speed:")
st.write(speeds)
if original_shape is not None:
st.markdown("### Original image shape (height, width):")
st.write(original_shape)
pass