Skip to content

Commit fa617b8

Browse files
committed
Bug squash
- Add final clip between 0 and auto defined range to work with avatars that use bools do define animation - redo how input validation is done. - fix babble model loader - make misc_utils platform agnostic
1 parent 67637fd commit fa617b8

File tree

4 files changed

+297
-139
lines changed

4 files changed

+297
-139
lines changed

BabbleApp/algo_settings_widget.py

Lines changed: 136 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
from osc import Tab
55
from queue import Queue
66
from threading import Event
7+
import re
78

89

910
class AlgoSettingsWidget:
1011
def __init__(self, widget_id: Tab, main_config: BabbleSettingsConfig, osc_queue: Queue):
1112

12-
1313
self.gui_general_settings_layout = f"-GENERALSETTINGSLAYOUT{widget_id}-"
1414
self.gui_multiply = f"-MULTIPLY{widget_id}-"
1515
self.gui_model_file = f"-MODLEFILE{widget_id}-"
@@ -35,62 +35,57 @@ def __init__(self, widget_id: Tab, main_config: BabbleSettingsConfig, osc_queue:
3535
size=(32),
3636
tooltip="Name of the model file.",
3737
),
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+
),
4568
],
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-
],
7669
[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+
),
8377
],
8478
[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+
),
9186
],
9287
[
93-
sg.Text("One Euro Filter Paramaters:", background_color='#242224'),
88+
sg.Text("One Euro Filter Parameters:", background_color='#242224'),
9489
],
9590
[
9691

@@ -99,35 +94,32 @@ def __init__(self, widget_id: Tab, main_config: BabbleSettingsConfig, osc_queue:
9994
self.config.gui_min_cutoff,
10095
key=self.gui_min_cutoff,
10196
size=(7),
97+
enable_events=True,
10298
),
103-
# ],
104-
# [
10599
sg.Text("Speed Coefficient", background_color='#424042'),
106100
sg.InputText(
107101
self.config.gui_speed_coefficient,
108102
key=self.gui_speed_coefficient,
109103
size=(5),
104+
enable_events=True,
110105
),
111106
],
112107

113-
114108
]
115109

116-
117110
self.widget_layout = [
118-
[
111+
[
119112
sg.Text("Model Settings:", background_color='#242224'),
120113
],
121114
[
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'),
123116
],
124117
]
125118

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.
127120
self.cancellation_event.set()
128121
self.image_queue = Queue(maxsize=2)
129122

130-
131123
def started(self):
132124
return not self.cancellation_event.is_set()
133125

@@ -143,48 +135,115 @@ def stop(self):
143135
return
144136
self.cancellation_event.set()
145137

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+
146146
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+
148191
changed = False
149192

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
153200

154201
if self.config.gui_model_file != values[self.gui_model_file]:
155202
self.config.gui_model_file = values[self.gui_model_file]
156203
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
161212

162213
if self.config.gui_use_gpu != values[self.gui_use_gpu]:
163214
self.config.gui_use_gpu = values[self.gui_use_gpu]
164215
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
169224

170225
if self.config.gui_runtime != str(values[self.gui_runtime]):
171226
self.config.gui_runtime = str(values[self.gui_runtime])
172227
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+
178237
if values[self.gui_min_cutoff] != '':
179238
if self.config.gui_min_cutoff != values[self.gui_min_cutoff]:
180239
self.config.gui_min_cutoff = values[self.gui_min_cutoff]
181240
changed = True
241+
182242
if values[self.gui_speed_coefficient] != '':
183243
if self.config.gui_speed_coefficient != values[self.gui_speed_coefficient]:
184244
self.config.gui_speed_coefficient = values[self.gui_speed_coefficient]
185245
changed = True
186246

187247
if changed:
188248
self.main_config.save()
189-
#print(self.main_config)
190249
self.osc_queue.put(Tab.ALGOSETTINGS)

BabbleApp/babble_model_loader.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ def run_model(self):
2222

2323
output = self.one_euro_filter(output)
2424

25-
for i in range(len(output)): # Clip values between 0 - 1
26-
output[i] = max(min(output[i], 1), 0)
27-
28-
self.output = output
25+
# for i in range(len(output)): # Clip values between 0 - 1
26+
# output[i] = max(min(output[i], 1), 0)
27+
## Clip values between 0 - 1
28+
output = np.clip(output, 0, 1)
29+
self.output = output

0 commit comments

Comments
 (0)