Skip to content

Commit 12f0d9a

Browse files
committed
fix calibration? ONNX optimization
1 parent 1d18c24 commit 12f0d9a

File tree

3 files changed

+47
-30
lines changed

3 files changed

+47
-30
lines changed

BabbleApp/babble_processor.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
from dataclasses import dataclass
44
import sys
55
import asyncio
6-
7-
8-
96
sys.path.append(".")
107
from config import BabbleCameraConfig
118
from config import BabbleSettingsConfig
@@ -21,7 +18,8 @@
2118
from osc_calibrate_filter import *
2219
from eye import CamInfo, EyeInfoOrigin
2320
from babble_model_loader import *
24-
21+
import os
22+
os.environ["OMP_NUM_THREADS"] = "1"
2523
def run_once(f):
2624
def wrapper(*args, **kwargs):
2725
if not wrapper.has_run:
@@ -112,12 +110,14 @@ def __init__(
112110
self.model = self.settings.gui_model_file
113111
self.use_gpu = self.settings.gui_use_gpu
114112
self.output = []
113+
self.val_list = []
115114
self.calibrate_config = np.empty((1, 45))
115+
self.min_max_array = np.empty((2, 45))
116116

117117
self.opts = ort.SessionOptions()
118118
self.opts.intra_op_num_threads = 1
119119
self.opts.inter_op_num_threads = 1
120-
self.opts.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_EXTENDED
120+
self.opts.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
121121
if not self.use_gpu:
122122
self.sess = ort.InferenceSession(self.model, self.opts, providers=['CPUExecutionProvider'])
123123
else:

BabbleApp/camera_widget.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,12 @@ def render(self, window, event, values):
280280
self.x1, self.y1 = values[self.gui_roi_selection]
281281

282282
if event == self.gui_restart_calibration:
283-
self.ransac.calibration_frame_counter = 300
283+
self.ransac.calibration_frame_counter = 1500
284284
PlaySound('Audio/start.wav', SND_FILENAME | SND_ASYNC)
285285

286286
if event == self.gui_stop_calibration:
287287
self.ransac.calibration_frame_counter = 0
288288

289-
if event == self.gui_recenter_eyes:
290-
self.settings.gui_recenter_eyes = True
291-
292289
needs_roi_set = self.config.roi_window_h <= 0 or self.config.roi_window_w <= 0
293290

294291
# TODO: Refactor if statements below...

BabbleApp/osc_calibrate_filter.py

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,62 @@ class EyeId(IntEnum):
1212

1313
class cal():
1414
def cal_osc(self, array):
15-
15+
#print(self.calibration_frame_counter)
1616
if self.calibration_frame_counter == 0:
17+
1718
self.calibration_frame_counter = None
18-
lower_threshold = np.percentile(self.calibrate_config, 1)
19-
upper_threshold = np.percentile(self.calibrate_config, 99)
20-
self.calibrate_config = self.calibrate_config[(self.calibrate_config >= lower_threshold) & (self.calibrate_config <= upper_threshold)]
19+
values = np.array(self.val_list)
20+
21+
# Initialize the min_max_array with shape (2, num_outputs)
22+
num_outputs = values.shape[1]
23+
self.min_max_array = np.zeros((2, num_outputs))
24+
25+
# Iterate over each output index
26+
for i in range(num_outputs):
27+
# Calculate the lower and upper thresholds for the current index
28+
lower_threshold = np.percentile(values[:, i], 1)
29+
upper_threshold = np.percentile(values[:, i], 99)
30+
31+
# Filter out values within the thresholds for the current index
32+
filtered_values = values[(values[:, i] >= lower_threshold) & (values[:, i] <= upper_threshold), i]
33+
34+
# Extract the minimum and maximum values for the current index
35+
min_value = np.min(filtered_values)
36+
max_value = np.max(filtered_values)
2137

22-
# Find maximum and minimum values in the array
23-
max_values = np.amax(self.calibrate_config, axis=1)
24-
min_values = np.amin(self.calibrate_config, axis=1)
25-
result = np.column_stack((min_values, max_values))
26-
result_flat = result.flatten()
38+
# Store the min and max values in the min_max_array
39+
self.min_max_array[0, i] = min_value
40+
self.min_max_array[1, i] = max_value
2741

42+
self.settings.calib_array = self.min_max_array
2843
print("[INFO] Calibration completed.")
29-
#print(self.calibrate_config)
30-
self.settings.calib_array = result_flat
44+
3145
PlaySound('Audio/completed.wav', SND_FILENAME | SND_ASYNC)
3246
if self.calibration_frame_counter == 10:
3347

3448
self.calibration_frame_counter -= 1
35-
3649
elif self.calibration_frame_counter != None:
3750

51+
self.val_list.append(array)
52+
3853

39-
self.calibrate_config = np.vstack((self.calibrate_config, array.T))
54+
# self.calibrate_config = np.vstack((self.calibrate_config, array.T))
4055
# np.append(self.calibrate_config, array.reshape(-1, 1), axis=1)
4156

4257
self.calibration_frame_counter -= 1
4358
varcount = 0
4459
filtered_output = []
45-
if self.settings.calib_array != None:
46-
for value in array:
47-
low_v = self.settings.calib_array[varcount][0]
48-
high_v = self.settings.calib_array[varcount][1]
49-
filterv = (value - low_v) / (high_v - low_v)
50-
filtered_output.append(filterv)
51-
varcount += 1
52-
array = filtered_output
60+
if self.settings.calib_array is not None and np.any(self.settings.calib_array):
61+
calibrated_array = np.zeros_like(array)
62+
for i, value in enumerate(array):
63+
min_value = self.min_max_array[0, i]
64+
max_value = self.min_max_array[1, i]
65+
66+
if min_value == max_value:
67+
calibrated_value = 0.0 # Set to a default value (can be adjusted as needed)
68+
else:
69+
calibrated_value = (value - min_value) / (max_value - min_value)
70+
71+
calibrated_array[i] = calibrated_value
72+
array = calibrated_array
5373
return array

0 commit comments

Comments
 (0)