4
4
from osc import Tab
5
5
from queue import Queue
6
6
from threading import Event
7
+ import re
7
8
8
9
9
10
class AlgoSettingsWidget :
10
11
def __init__ (self , widget_id : Tab , main_config : BabbleSettingsConfig , osc_queue : Queue ):
11
12
12
-
13
13
self .gui_general_settings_layout = f"-GENERALSETTINGSLAYOUT{ widget_id } -"
14
14
self .gui_multiply = f"-MULTIPLY{ widget_id } -"
15
15
self .gui_model_file = f"-MODLEFILE{ widget_id } -"
@@ -35,62 +35,57 @@ def __init__(self, widget_id: Tab, main_config: BabbleSettingsConfig, osc_queue:
35
35
size = (32 ),
36
36
tooltip = "Name of the model file." ,
37
37
),
38
- sg .Text ("Inference Threads:" , background_color = '#424042' ),
39
- sg .InputText (
40
- self .config .gui_inference_threads ,
41
- key = self .gui_inference_threads ,
42
- size = (4 ),
43
- tooltip = "How many threads to use for processing the model." ,
44
- ),
38
+ sg .Text ("Inference Threads:" , background_color = '#424042' ),
39
+ sg .InputText (
40
+ self .config .gui_inference_threads ,
41
+ key = self .gui_inference_threads ,
42
+ size = (4 ),
43
+ tooltip = "How many threads to use for processing the model." ,
44
+ enable_events = True ,
45
+ ),
46
+ ],
47
+ [sg .Text ("Runtime:" , background_color = '#424042' ),
48
+ sg .OptionMenu (
49
+ self .runtime_list ,
50
+ self .config .gui_runtime ,
51
+ key = self .gui_runtime ,
52
+ ),
53
+ sg .Text ("GPU Index:" , background_color = '#424042' ),
54
+ sg .InputText (
55
+ self .config .gui_gpu_index ,
56
+ key = self .gui_gpu_index ,
57
+ size = (4 ),
58
+ tooltip = "Select which device to run inference." ,
59
+ enable_events = True ,
60
+ ),
61
+ sg .Checkbox (
62
+ "Use GPU" ,
63
+ default = self .config .gui_use_gpu ,
64
+ key = self .gui_use_gpu ,
65
+ background_color = '#424042' ,
66
+ tooltip = "Toggle GPU execution." ,
67
+ ),
45
68
],
46
- [sg .Text ("Runtime:" , background_color = '#424042' ),
47
- sg .OptionMenu (
48
- self .runtime_list ,
49
- self .config .gui_runtime ,
50
- key = self .gui_runtime ,
51
- ),
52
- #sg.InputText(
53
- # self.config.gui_runtime,
54
- # key=self.gui_runtime,
55
- # size=(4),
56
- # tooltip = "Method to run the model.",
57
-
58
- #),
59
-
60
- sg .Text ("GPU Index:" , background_color = '#424042' ), # Replace with Dropdown once I have internet to view docs.
61
- sg .InputText (
62
- self .config .gui_gpu_index ,
63
- key = self .gui_gpu_index ,
64
- size = (4 ),
65
- tooltip = "Select which device to run inference." ,
66
- ),
67
-
68
- sg .Checkbox (
69
- "Use GPU" ,
70
- default = self .config .gui_use_gpu ,
71
- key = self .gui_use_gpu ,
72
- background_color = '#424042' ,
73
- tooltip = "Toggle GPU execution." ,
74
- ),
75
- ],
76
69
[sg .Text ("Model output multiplier:" , background_color = '#424042' ),
77
- sg .InputText (
78
- self .config .gui_multiply ,
79
- key = self .gui_multiply ,
80
- size = (4 ),
81
- tooltip = "Model output modifier." ,
82
- ),
70
+ sg .InputText (
71
+ self .config .gui_multiply ,
72
+ key = self .gui_multiply ,
73
+ size = (4 ),
74
+ tooltip = "Model output modifier." ,
75
+ enable_events = True ,
76
+ ),
83
77
],
84
78
[sg .Text ("Calibration Deadzone:" , background_color = '#424042' ),
85
- sg .InputText (
86
- self .config .calib_deadzone ,
87
- key = self .calib_deadzone ,
88
- size = (4 ),
89
- tooltip = "Offset the minimum calibrated values." ,
90
- ),
79
+ sg .InputText (
80
+ self .config .calib_deadzone ,
81
+ key = self .calib_deadzone ,
82
+ size = (4 ),
83
+ tooltip = "Offset the minimum calibrated values." ,
84
+ enable_events = True ,
85
+ ),
91
86
],
92
87
[
93
- sg .Text ("One Euro Filter Paramaters :" , background_color = '#242224' ),
88
+ sg .Text ("One Euro Filter Parameters :" , background_color = '#242224' ),
94
89
],
95
90
[
96
91
@@ -99,35 +94,32 @@ def __init__(self, widget_id: Tab, main_config: BabbleSettingsConfig, osc_queue:
99
94
self .config .gui_min_cutoff ,
100
95
key = self .gui_min_cutoff ,
101
96
size = (7 ),
97
+ enable_events = True ,
102
98
),
103
- # ],
104
- # [
105
99
sg .Text ("Speed Coefficient" , background_color = '#424042' ),
106
100
sg .InputText (
107
101
self .config .gui_speed_coefficient ,
108
102
key = self .gui_speed_coefficient ,
109
103
size = (5 ),
104
+ enable_events = True ,
110
105
),
111
106
],
112
107
113
-
114
108
]
115
109
116
-
117
110
self .widget_layout = [
118
- [
111
+ [
119
112
sg .Text ("Model Settings:" , background_color = '#242224' ),
120
113
],
121
114
[
122
- sg .Column (self .general_settings_layout , key = self .gui_general_settings_layout , background_color = '#424042' ),
115
+ sg .Column (self .general_settings_layout , key = self .gui_general_settings_layout , background_color = '#424042' ),
123
116
],
124
117
]
125
118
126
- self .cancellation_event = Event () # Set the event until start is called, otherwise we can block if shutdown is called.
119
+ self .cancellation_event = Event () # Set the event until start is called, otherwise we can block if shutdown is called.
127
120
self .cancellation_event .set ()
128
121
self .image_queue = Queue (maxsize = 2 )
129
122
130
-
131
123
def started (self ):
132
124
return not self .cancellation_event .is_set ()
133
125
@@ -143,48 +135,115 @@ def stop(self):
143
135
return
144
136
self .cancellation_event .set ()
145
137
138
+ def is_valid_float_input (self , value ):
139
+ # Allow empty string, negative sign, or a float number
140
+ return bool (re .match (r'^-?\d*\.?\d*$' , value ))
141
+
142
+ def is_valid_int_input (self , value ):
143
+ # Allow empty string, negative sign, or an integer number
144
+ return bool (re .match (r'^-?\d*$' , value ))
145
+
146
146
def render (self , window , event , values ):
147
- # If anything has changed in our configuration settings, change/update those.
147
+ # Input validation for the fields
148
+ if event == self .gui_multiply :
149
+ value = values [self .gui_multiply ]
150
+ if not self .is_valid_float_input (value ):
151
+ # Invalid input, remove last character
152
+ value = value [:- 1 ]
153
+ window [self .gui_multiply ].update (value )
154
+ values [self .gui_multiply ] = value
155
+
156
+ elif event == self .calib_deadzone :
157
+ value = values [self .calib_deadzone ]
158
+ if not self .is_valid_float_input (value ):
159
+ value = value [:- 1 ]
160
+ window [self .calib_deadzone ].update (value )
161
+ values [self .calib_deadzone ] = value
162
+
163
+ elif event == self .gui_inference_threads :
164
+ value = values [self .gui_inference_threads ]
165
+ if not self .is_valid_int_input (value ):
166
+ value = value [:- 1 ]
167
+ window [self .gui_inference_threads ].update (value )
168
+ values [self .gui_inference_threads ] = value
169
+
170
+ elif event == self .gui_gpu_index :
171
+ value = values [self .gui_gpu_index ]
172
+ if not self .is_valid_int_input (value ):
173
+ value = value [:- 1 ]
174
+ window [self .gui_gpu_index ].update (value )
175
+ values [self .gui_gpu_index ] = value
176
+
177
+ elif event == self .gui_min_cutoff :
178
+ value = values [self .gui_min_cutoff ]
179
+ if not self .is_valid_float_input (value ):
180
+ value = value [:- 1 ]
181
+ window [self .gui_min_cutoff ].update (value )
182
+ values [self .gui_min_cutoff ] = value
183
+
184
+ elif event == self .gui_speed_coefficient :
185
+ value = values [self .gui_speed_coefficient ]
186
+ if not self .is_valid_float_input (value ):
187
+ value = value [:- 1 ]
188
+ window [self .gui_speed_coefficient ].update (value )
189
+ values [self .gui_speed_coefficient ] = value
190
+
148
191
changed = False
149
192
150
- if self .config .gui_multiply != float (values [self .gui_multiply ]):
151
- self .config .gui_multiply = float (values [self .gui_multiply ])
152
- changed = True
193
+ try :
194
+ if values [self .gui_multiply ] != '' :
195
+ if self .config .gui_multiply != float (values [self .gui_multiply ]):
196
+ self .config .gui_multiply = float (values [self .gui_multiply ])
197
+ changed = True
198
+ except ValueError :
199
+ pass # Ignore invalid float conversion
153
200
154
201
if self .config .gui_model_file != values [self .gui_model_file ]:
155
202
self .config .gui_model_file = values [self .gui_model_file ]
156
203
changed = True
157
-
158
- if self .config .calib_deadzone != float (values [self .calib_deadzone ]):
159
- self .config .calib_deadzone = float (values [self .calib_deadzone ])
160
- changed = True
204
+
205
+ try :
206
+ if values [self .calib_deadzone ] != '' :
207
+ if self .config .calib_deadzone != float (values [self .calib_deadzone ]):
208
+ self .config .calib_deadzone = float (values [self .calib_deadzone ])
209
+ changed = True
210
+ except ValueError :
211
+ pass # Ignore invalid float conversion
161
212
162
213
if self .config .gui_use_gpu != values [self .gui_use_gpu ]:
163
214
self .config .gui_use_gpu = values [self .gui_use_gpu ]
164
215
changed = True
165
- if values [self .gui_gpu_index ] != '' :
166
- if self .config .gui_gpu_index != int (values [self .gui_gpu_index ]):
167
- self .config .gui_gpu_index = int (values [self .gui_gpu_index ])
168
- changed = True
216
+
217
+ try :
218
+ if values [self .gui_gpu_index ] != '' :
219
+ if self .config .gui_gpu_index != int (values [self .gui_gpu_index ]):
220
+ self .config .gui_gpu_index = int (values [self .gui_gpu_index ])
221
+ changed = True
222
+ except ValueError :
223
+ pass # Ignore invalid int conversion
169
224
170
225
if self .config .gui_runtime != str (values [self .gui_runtime ]):
171
226
self .config .gui_runtime = str (values [self .gui_runtime ])
172
227
changed = True
173
-
174
- if values [self .gui_inference_threads ] != '' :
175
- if self .config .gui_inference_threads != int (values [self .gui_inference_threads ]):
176
- self .config .gui_inference_threads = int (values [self .gui_inference_threads ])
177
- changed = True
228
+
229
+ try :
230
+ if values [self .gui_inference_threads ] != '' :
231
+ if self .config .gui_inference_threads != int (values [self .gui_inference_threads ]):
232
+ self .config .gui_inference_threads = int (values [self .gui_inference_threads ])
233
+ changed = True
234
+ except ValueError :
235
+ pass # Ignore invalid int conversion
236
+
178
237
if values [self .gui_min_cutoff ] != '' :
179
238
if self .config .gui_min_cutoff != values [self .gui_min_cutoff ]:
180
239
self .config .gui_min_cutoff = values [self .gui_min_cutoff ]
181
240
changed = True
241
+
182
242
if values [self .gui_speed_coefficient ] != '' :
183
243
if self .config .gui_speed_coefficient != values [self .gui_speed_coefficient ]:
184
244
self .config .gui_speed_coefficient = values [self .gui_speed_coefficient ]
185
245
changed = True
186
246
187
247
if changed :
188
248
self .main_config .save ()
189
- #print(self.main_config)
190
249
self .osc_queue .put (Tab .ALGOSETTINGS )
0 commit comments