Skip to content

Commit 59b5f52

Browse files
authored
Merge branch 'autocalibrator' into main
2 parents dfeeb87 + b2bad82 commit 59b5f52

File tree

9 files changed

+359
-320
lines changed

9 files changed

+359
-320
lines changed

BabbleApp/Locale/Afrikaans/locale.json

Lines changed: 163 additions & 160 deletions
Large diffs are not rendered by default.

BabbleApp/Locale/English/locale.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@
150150
"calibration.maxLeftValue": "Max Left Value",
151151
"calibration.minRightValue": "Min Right Value",
152152
"calibration.maxRightValue": "Max Right Value",
153+
"algorithm.clearCalibration": "Clear Calibration",
154+
"algorithm.clearCalibrationTooltip": "Reset all calibration values to their defaults",
153155
"general.theme": "Theme",
154156
"general.theme.system": "System",
155157
"general.theme.light": "Light",

BabbleApp/algo_settings_widget.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def __init__(
2424
self.gui_gpu_index = f"GPUINDEX{widget_id}"
2525
self.calib_deadzone = f"CALIBDEADZONE{widget_id}"
2626
self.main_config = main_config
27+
self.main_config.settings.widget_id = widget_id
2728
self.config = main_config.settings
2829
self.osc_queue = osc_queue
2930
self.runtime_list = ("Default (ONNX)", "ONNX")
@@ -153,6 +154,14 @@ def __init__(
153154
background_color=bg_color_highlight,
154155
),
155156
],
157+
[
158+
sg.Button(
159+
lang._instance.get_string("algorithm.clearCalibration"),
160+
key=f"-CLEARCALIBRATION{widget_id}-",
161+
button_color=("white", "#d9534f"),
162+
tooltip=lang._instance.get_string("algorithm.clearCalibrationTooltip"),
163+
),
164+
],
156165
]
157166

158167
self.cancellation_event = (
@@ -200,6 +209,12 @@ def render(self, window, event, values):
200209
window[self.gui_inference_threads].update(value)
201210
values[self.gui_inference_threads] = value
202211

212+
# --- Begin patch: handle clear calibration button ---
213+
if event == f"-CLEARCALIBRATION{self.main_config.settings.widget_id}-":
214+
if hasattr(self.main_config.settings, "calibration_filter") and hasattr(self.main_config.settings.calibration_filter, "clear_calibration"):
215+
self.main_config.settings.calibration_filter.clear_calibration()
216+
sg.popup(lang._instance.get_string("algorithm.calibrationCleared"), title="Calibration Reset")
217+
# --- End patch ---
203218
elif event == self.gui_gpu_index:
204219
value = values[self.gui_gpu_index]
205220
if not is_valid_int_input(value):

BabbleApp/babble_processor.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ def __init__(
137137
self.one_euro_filter = OneEuroFilter(
138138
noisy_point, min_cutoff=min_cutoff, beta=beta
139139
)
140+
# Persistent calibrator instance for continuous calibration
141+
self._calibrator = None
142+
143+
def process_output(self, output_array):
144+
# Called after model inference, applies calibration if enabled
145+
if self.settings.use_calibration:
146+
if self._calibrator is None or (hasattr(self._calibrator, 'calibrator') and self._calibrator.calibrator is not None and self._calibrator.calibrator.num_blendshapes != len(output_array)):
147+
from osc_calibrate_filter import cal
148+
self._calibrator = cal(self.settings)
149+
return self._calibrator.cal_osc(output_array)
150+
else:
151+
return output_array
140152

141153
def output_images_and_update(self, output_information: CamInfo):
142154
try:
@@ -267,8 +279,11 @@ def run(self):
267279
) # copy this frame to have a clean image for blink algo
268280

269281
run_model(self)
270-
if self.settings.use_calibration:
271-
self.output = cal.cal_osc(self, self.output)
282+
# Replace the old calibration logic with the new persistent calibrator usage
283+
# if self.settings.use_calibration:
284+
# self.output = cal(self.settings).cal_osc(self.output)
285+
# New code:
286+
self.output = self.process_output(self.output)
272287
# else:
273288
# pass
274289
# print(self.output)

BabbleApp/babbleapp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,13 @@ async def main_loop(window, config, cams, settings, thread_manager):
378378
config.cam_display_id = Tab.SETTINGS
379379
config.save()
380380

381-
elif (
381+
elif (
382382
values[UIConstants.ALGO_SETTINGS_RADIO_NAME]
383383
and config.cam_display_id != Tab.ALGOSETTINGS
384384
):
385385
cams[0].stop()
386386
settings[0].stop()
387-
settings[2].stop()
387+
settings[2].stop()
388388
settings[1].start()
389389
window[UIConstants.CAM_NAME].update(visible=False)
390390
window[UIConstants.SETTINGS_NAME].update(visible=False)

BabbleApp/camera.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import serial.tools.list_ports
1212
from lang_manager import LocaleStringManager as lang
1313
from colorama import Fore
14-
from config import BabbleConfig, BabbleSettingsConfig
14+
from config import BabbleConfig, BabbleSettingsConfig, BabbleCameraConfig
1515
from PIL import Image
1616
from io import BytesIO
1717
from utils.misc_utils import get_camera_index_by_name, list_camera_names, os_type

BabbleApp/camera_widget.py

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ def __init__(self, widget_id: Tab, main_config: BabbleConfig, osc_queue: Queue):
3333
self.gui_tracking_fps = f"-TRACKINGFPS{widget_id}-"
3434
self.gui_tracking_bps = f"-TRACKINGBPS{widget_id}-"
3535
self.gui_output_graph = f"-OUTPUTGRAPH{widget_id}-"
36-
self.gui_restart_calibration = f"-RESTARTCALIBRATION{widget_id}-"
37-
self.gui_stop_calibration = f"-STOPCALIBRATION{widget_id}-"
3836
self.gui_mode_readout = f"-APPMODE{widget_id}-"
3937
self.gui_roi_message = f"-ROIMESSAGE{widget_id}-"
4038
self.gui_vertical_flip = f"-VERTICALFLIP{widget_id}-"
@@ -128,22 +126,6 @@ def __init__(self, widget_id: Tab, main_config: BabbleConfig, osc_queue: Queue):
128126
tooltip=lang._instance.get_string("camera.rotationTooltip"),
129127
),
130128
],
131-
[
132-
sg.Button(
133-
lang._instance.get_string("camera.startCalibration"),
134-
key=self.gui_restart_calibration,
135-
button_color=button_color,
136-
tooltip=lang._instance.get_string("camera.startCalibrationTooltip"),
137-
disabled=not self.settings_config.use_calibration,
138-
),
139-
sg.Button(
140-
lang._instance.get_string("camera.stopCalibration"),
141-
key=self.gui_stop_calibration,
142-
button_color=button_color,
143-
tooltip=lang._instance.get_string("camera.startCalibrationTooltip"),
144-
disabled=not self.settings_config.use_calibration,
145-
),
146-
],
147129
[
148130
sg.Checkbox(
149131
f'{lang._instance.get_string("camera.enableCalibration")}:',
@@ -388,13 +370,14 @@ def render(self, window, event, values):
388370

389371
if event == self.use_calibration:
390372
if self.settings_config.use_calibration == True:
391-
window[self.gui_restart_calibration].update(disabled=False)
392-
window[self.gui_stop_calibration].update(disabled=False)
393373
print(f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.enabled")}')
374+
# Optionally, trigger BlendShapeCalibrator reset/init here if needed
375+
playSound(os.path.join("Audio", "start.wav"))
394376
else:
395-
window[self.gui_restart_calibration].update(disabled=True)
396-
window[self.gui_stop_calibration].update(disabled=True)
397377
print(f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.disabled")}')
378+
# Stop calibration if it was running
379+
if self.babble_cnn.calibration_frame_counter is not None:
380+
self.babble_cnn.calibration_frame_counter = None
398381

399382
if event == "{}+UP".format(self.gui_roi_selection):
400383
# Event for mouse button up in ROI mode
@@ -438,18 +421,15 @@ def render(self, window, event, values):
438421
#print(self.camera_list)
439422
window[self.gui_camera_addr].update(values=self.camera_list,size=(20,0))
440423

441-
if event == self.gui_restart_calibration:
424+
442425
if (
443426
values[self.use_calibration] == True
444427
): # Don't start recording if the calibration filter is disabled.
445-
self.babble_cnn.calibration_frame_counter = 1500
428+
self.babble_cnn.calibration_frame_counter = None # Disable old frame counter logic
429+
# Optionally, trigger BlendShapeCalibrator reset/init here if needed
446430
playSound(os.path.join("Audio", "start.wav"))
447431

448-
if event == self.gui_stop_calibration:
449-
if (
450-
self.babble_cnn.calibration_frame_counter != None
451-
): # Only assign the variable if we are in calibration mode.
452-
self.babble_cnn.calibration_frame_counter = 0
432+
# Calibration is now controlled by the checkbox only
453433

454434
needs_roi_set = self.config.roi_window_h <= 0 or self.config.roi_window_w <= 0
455435

BabbleApp/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from utils.misc_utils import ensurePath
66
from tab import Tab
77
from pydantic import BaseModel
8-
from typing import Union
8+
from typing import Union, Optional
99
from lang_manager import LocaleStringManager as lang
1010

1111
CONFIG_FILE_NAME: str = "babble_settings.json"
@@ -25,6 +25,7 @@ class BabbleCameraConfig(BaseModel):
2525

2626

2727
class BabbleSettingsConfig(BaseModel):
28+
widget_id: Optional[int] = None
2829
gui_min_cutoff: str = "3"
2930
gui_speed_coefficient: str = "0.9"
3031
gui_osc_address: str = "127.0.0.1"

0 commit comments

Comments
 (0)