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,56 +135,118 @@ 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 str (self .config .gui_multiply ) != values [self .gui_multiply ]:
151
- try :
152
- self .config .gui_multiply = float (values [self .gui_multiply ])
153
- changed = True
154
- except : print ("Not a Float" )
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
200
+
155
201
156
202
if self .config .gui_model_file != values [self .gui_model_file ]:
157
203
self .config .gui_model_file = values [self .gui_model_file ]
158
204
changed = True
159
-
160
- if str (self .config .calib_deadzone ) != values [self .calib_deadzone ]:
161
- try :
162
- self .config .calib_deadzone = float (values [self .calib_deadzone ])
163
- changed = True
164
- except : print ("Not a Float" )
205
+
206
+ try :
207
+ if values [self .calib_deadzone ] != '' :
208
+ if self .config .calib_deadzone != float (values [self .calib_deadzone ]):
209
+ self .config .calib_deadzone = float (values [self .calib_deadzone ])
210
+ changed = True
211
+ except ValueError :
212
+ pass # Ignore invalid float conversion
213
+
165
214
166
215
if self .config .gui_use_gpu != values [self .gui_use_gpu ]:
167
216
self .config .gui_use_gpu = values [self .gui_use_gpu ]
168
217
changed = True
169
- if values [self .gui_gpu_index ] != '' :
170
- if str (self .config .gui_gpu_index ) != values [self .gui_gpu_index ]:
171
- try :
218
+
219
+ try :
220
+ if values [self .gui_gpu_index ] != '' :
221
+ if self .config .gui_gpu_index != int (values [self .gui_gpu_index ]):
172
222
self .config .gui_gpu_index = int (values [self .gui_gpu_index ])
173
223
changed = True
174
- except : print ("Not an Int" )
224
+ except ValueError :
225
+ pass # Ignore invalid int conversion
226
+
175
227
176
228
if self .config .gui_runtime != str (values [self .gui_runtime ]):
177
229
self .config .gui_runtime = str (values [self .gui_runtime ])
178
230
changed = True
179
-
180
- if values [ self . gui_inference_threads ] != '' :
181
- if str ( self .config . gui_inference_threads ) != values [ self . gui_inference_threads ] :
182
- try :
183
- self .config .gui_inference_threads = int (values [self .gui_inference_threads ])
231
+
232
+ try :
233
+ if values [ self .gui_inference_threads ] != '' :
234
+ if self . config . gui_inference_threads != int ( values [ self . gui_inference_threads ]):
235
+ self .config .gui_inference_threads = int (values [self .gui_inference_threads ])
184
236
changed = True
185
- except : print ("Not an Int" )
237
+ except ValueError :
238
+ pass # Ignore invalid int conversion
239
+
186
240
if values [self .gui_min_cutoff ] != '' :
187
241
if self .config .gui_min_cutoff != values [self .gui_min_cutoff ]:
188
242
self .config .gui_min_cutoff = values [self .gui_min_cutoff ]
189
243
changed = True
244
+
190
245
if values [self .gui_speed_coefficient ] != '' :
191
246
if self .config .gui_speed_coefficient != values [self .gui_speed_coefficient ]:
192
247
self .config .gui_speed_coefficient = values [self .gui_speed_coefficient ]
193
248
changed = True
194
249
195
250
if changed :
196
251
self .main_config .save ()
197
- #print(self.main_config)
198
252
self .osc_queue .put (Tab .ALGOSETTINGS )
0 commit comments