From 12f0d9aa75ee8ad70fa416d02b900ee9853a6802 Mon Sep 17 00:00:00 2001 From: Prohurtz <48768484+RedHawk989@users.noreply.github.com> Date: Sun, 4 Jun 2023 06:43:07 -0700 Subject: [PATCH] fix calibration? ONNX optimization --- BabbleApp/babble_processor.py | 10 ++--- BabbleApp/camera_widget.py | 5 +-- BabbleApp/osc_calibrate_filter.py | 62 ++++++++++++++++++++----------- 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/BabbleApp/babble_processor.py b/BabbleApp/babble_processor.py index 3529d8a..ce887c6 100644 --- a/BabbleApp/babble_processor.py +++ b/BabbleApp/babble_processor.py @@ -3,9 +3,6 @@ from operator import truth from dataclasses import dataclass import sys import asyncio - - - sys.path.append(".") from config import BabbleCameraConfig from config import BabbleSettingsConfig @@ -21,7 +18,8 @@ from osc import Tab from osc_calibrate_filter import * from eye import CamInfo, EyeInfoOrigin from babble_model_loader import * - +import os +os.environ["OMP_NUM_THREADS"] = "1" def run_once(f): def wrapper(*args, **kwargs): if not wrapper.has_run: @@ -112,12 +110,14 @@ class EyeProcessor: self.model = self.settings.gui_model_file self.use_gpu = self.settings.gui_use_gpu self.output = [] + self.val_list = [] self.calibrate_config = np.empty((1, 45)) + self.min_max_array = np.empty((2, 45)) self.opts = ort.SessionOptions() self.opts.intra_op_num_threads = 1 self.opts.inter_op_num_threads = 1 - self.opts.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_EXTENDED + self.opts.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL if not self.use_gpu: self.sess = ort.InferenceSession(self.model, self.opts, providers=['CPUExecutionProvider']) else: diff --git a/BabbleApp/camera_widget.py b/BabbleApp/camera_widget.py index 0fa1108..9631ade 100644 --- a/BabbleApp/camera_widget.py +++ b/BabbleApp/camera_widget.py @@ -280,15 +280,12 @@ class CameraWidget: self.x1, self.y1 = values[self.gui_roi_selection] if event == self.gui_restart_calibration: - self.ransac.calibration_frame_counter = 300 + self.ransac.calibration_frame_counter = 1500 PlaySound('Audio/start.wav', SND_FILENAME | SND_ASYNC) if event == self.gui_stop_calibration: self.ransac.calibration_frame_counter = 0 - if event == self.gui_recenter_eyes: - self.settings.gui_recenter_eyes = True - needs_roi_set = self.config.roi_window_h <= 0 or self.config.roi_window_w <= 0 # TODO: Refactor if statements below... diff --git a/BabbleApp/osc_calibrate_filter.py b/BabbleApp/osc_calibrate_filter.py index f082763..1b7efc2 100644 --- a/BabbleApp/osc_calibrate_filter.py +++ b/BabbleApp/osc_calibrate_filter.py @@ -12,42 +12,62 @@ class EyeId(IntEnum): class cal(): def cal_osc(self, array): - + #print(self.calibration_frame_counter) if self.calibration_frame_counter == 0: + self.calibration_frame_counter = None - lower_threshold = np.percentile(self.calibrate_config, 1) - upper_threshold = np.percentile(self.calibrate_config, 99) - self.calibrate_config = self.calibrate_config[(self.calibrate_config >= lower_threshold) & (self.calibrate_config <= upper_threshold)] + values = np.array(self.val_list) - # Find maximum and minimum values in the array - max_values = np.amax(self.calibrate_config, axis=1) - min_values = np.amin(self.calibrate_config, axis=1) - result = np.column_stack((min_values, max_values)) - result_flat = result.flatten() + # Initialize the min_max_array with shape (2, num_outputs) + num_outputs = values.shape[1] + self.min_max_array = np.zeros((2, num_outputs)) + # Iterate over each output index + for i in range(num_outputs): + # Calculate the lower and upper thresholds for the current index + lower_threshold = np.percentile(values[:, i], 1) + upper_threshold = np.percentile(values[:, i], 99) + + # Filter out values within the thresholds for the current index + filtered_values = values[(values[:, i] >= lower_threshold) & (values[:, i] <= upper_threshold), i] + + # Extract the minimum and maximum values for the current index + min_value = np.min(filtered_values) + max_value = np.max(filtered_values) + + # Store the min and max values in the min_max_array + self.min_max_array[0, i] = min_value + self.min_max_array[1, i] = max_value + + self.settings.calib_array = self.min_max_array print("[INFO] Calibration completed.") - #print(self.calibrate_config) - self.settings.calib_array = result_flat + PlaySound('Audio/completed.wav', SND_FILENAME | SND_ASYNC) if self.calibration_frame_counter == 10: self.calibration_frame_counter -= 1 - elif self.calibration_frame_counter != None: + self.val_list.append(array) - self.calibrate_config = np.vstack((self.calibrate_config, array.T)) + + # self.calibrate_config = np.vstack((self.calibrate_config, array.T)) # np.append(self.calibrate_config, array.reshape(-1, 1), axis=1) self.calibration_frame_counter -= 1 varcount = 0 filtered_output = [] - if self.settings.calib_array != None: - for value in array: - low_v = self.settings.calib_array[varcount][0] - high_v = self.settings.calib_array[varcount][1] - filterv = (value - low_v) / (high_v - low_v) - filtered_output.append(filterv) - varcount += 1 - array = filtered_output + if self.settings.calib_array is not None and np.any(self.settings.calib_array): + calibrated_array = np.zeros_like(array) + for i, value in enumerate(array): + min_value = self.min_max_array[0, i] + max_value = self.min_max_array[1, i] + + if min_value == max_value: + calibrated_value = 0.0 # Set to a default value (can be adjusted as needed) + else: + calibrated_value = (value - min_value) / (max_value - min_value) + + calibrated_array[i] = calibrated_value + array = calibrated_array return array \ No newline at end of file