Merge pull request #129 from Fracas42/v2.0-save-leap-calibration

feat: LEAP save calibration
This commit is contained in:
Prohurtz 2024-12-27 19:05:14 -08:00 committed by GitHub
commit 52956eda87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 17 deletions

View File

@ -56,6 +56,9 @@ class EyeTrackCameraConfig(BaseModel):
calib_YOFF: Union[float, None] = None calib_YOFF: Union[float, None] = None
calibration_points: List[List[Union[float, None]]] = [] calibration_points: List[List[Union[float, None]]] = []
calibration_points_3d: List[List[Union[float, None]]] = [] calibration_points_3d: List[List[Union[float, None]]] = []
leap_calibration_percentile_90: float = 0
leap_calibration_percentile_2: float = 0
leap_calibrated: bool = False
def update_capture_source(self, new_camera_address: str): def update_capture_source(self, new_camera_address: str):

View File

@ -319,7 +319,7 @@ class EyeProcessor:
self.rawx, self.rawx,
self.rawy, self.rawy,
self.eyeopen, self.eyeopen,
) = self.er_leap.run(self.current_image_gray, self.current_image_gray_clean, self.calibration_frame_counter, self.settings.leap_calibration_samples) ) = self.er_leap.run(self.current_image_gray, self.current_image_gray_clean, self.calibration_frame_counter)
if len(self.prev_y_list) >= 100: # "lock" eye when close/blink IN TESTING, kinda broke if len(self.prev_y_list) >= 100: # "lock" eye when close/blink IN TESTING, kinda broke
self.prev_y_list.pop(0) self.prev_y_list.pop(0)
@ -391,7 +391,7 @@ class EyeProcessor:
def LEAPM(self): def LEAPM(self):
self.thresh = self.current_image_gray.copy() self.thresh = self.current_image_gray.copy()
(self.current_image_gray, self.rawx, self.rawy, self.eyeopen,) = self.er_leap.run( (self.current_image_gray, self.rawx, self.rawy, self.eyeopen,) = self.er_leap.run(
self.current_image_gray, self.current_image_gray_clean, self.calibration_frame_counter, self.settings.leap_calibration_samples self.current_image_gray, self.current_image_gray_clean, self.calibration_frame_counter
) # TODO: make own self var and LEAP toggle ) # TODO: make own self var and LEAP toggle
self.thresh = self.current_image_gray.copy() self.thresh = self.current_image_gray.copy()
# todo: lorow, fix this as well # todo: lorow, fix this as well
@ -699,7 +699,7 @@ class EyeProcessor:
if self.settings.gui_LEAP or self.settings.gui_LEAP_lid: if self.settings.gui_LEAP or self.settings.gui_LEAP_lid:
if self.er_leap is None: if self.er_leap is None:
self.er_leap = External_Run_LEAP() self.er_leap = External_Run_LEAP(self.config, self.baseconfig)
algolist[self.settings.gui_LEAP] = self.LEAPM algolist[self.settings.gui_LEAP] = self.LEAPM
else: else:
if self.er_leap is not None: if self.er_leap is not None:

View File

@ -6,6 +6,7 @@ import time
import math import math
from queue import Queue from queue import Queue
import threading import threading
from config import EyeTrackCameraConfig, EyeTrackConfig
from one_euro_filter import OneEuroFilter from one_euro_filter import OneEuroFilter
import psutil import psutil
from utils.misc_utils import resource_path from utils.misc_utils import resource_path
@ -42,7 +43,7 @@ def run_onnx_model(queues, session, frame):
class LEAP_C: class LEAP_C:
def __init__(self): def __init__(self, eye_config: EyeTrackCameraConfig, config: EyeTrackConfig):
self.last_lid = None self.last_lid = None
self.current_image_gray = None self.current_image_gray = None
self.current_image_gray_clean = None self.current_image_gray_clean = None
@ -78,6 +79,8 @@ class LEAP_C:
self.old_per = 0.0 self.old_per = 0.0
self.delta_per_neg = 0.0 self.delta_per_neg = 0.0
self.ort_session1 = onnxruntime.InferenceSession(self.model_path, opts, providers=["CPUExecutionProvider"]) self.ort_session1 = onnxruntime.InferenceSession(self.model_path, opts, providers=["CPUExecutionProvider"])
self.eye_config: EyeTrackCameraConfig = eye_config
self.config: EyeTrackConfig = config
for i in range(self.num_threads): for i in range(self.num_threads):
thread = threading.Thread( thread = threading.Thread(
@ -111,21 +114,22 @@ class LEAP_C:
d2 = math.dist(pre_landmark[2], pre_landmark[4]) d2 = math.dist(pre_landmark[2], pre_landmark[4])
d = (d1 + d2) / 2 d = (d1 + d2) / 2
# if len(self.openlist) > 0 and d >= np.percentile(self.openlist, 80) and len(self.openlist) < self.calibration_samples:
# self.maxlist.append(d)
normal_open = np.percentile(self.openlist, 90) if len(self.openlist) >= 10 else 0.8
if self.calib == 0: if self.calib == 0:
self.openlist = [] self.openlist = []
self.eye_config.leap_calibrated = False
if len(self.openlist) < self.calibration_samples: if not self.eye_config.leap_calibrated:
self.openlist.append(d) self.openlist.append(d)
self.eye_config.leap_calibration_percentile_90 = np.percentile(self.openlist, 90) if len(self.openlist) >= 10 else 0.8
self.eye_config.leap_calibration_percentile_2 = np.percentile(self.openlist, 2) - self.eye_config.leap_calibration_percentile_90
if len(self.openlist) >= self.config.settings.leap_calibration_samples:
self.eye_config.leap_calibrated = True
self.config.save()
print(f"[INFO] {'Left' if self.eye_config is self.config.left_eye else 'Right'} eye calibrated")
try: try:
if len(self.openlist) > 0: if len(self.openlist) > 0 or self.eye_config.leap_calibrated:
per = (d - normal_open) / (np.percentile(self.openlist, 2) - normal_open) per = (d - self.eye_config.leap_calibration_percentile_90) / self.eye_config.leap_calibration_percentile_2
per = 1 - per per = 1 - per
per = np.clip(per, 0.0, 1.0) per = np.clip(per, 0.0, 1.0)
else: else:
@ -150,13 +154,12 @@ class LEAP_C:
class External_Run_LEAP: class External_Run_LEAP:
def __init__(self): def __init__(self, eye_config: EyeTrackCameraConfig, config: EyeTrackConfig):
self.algo = LEAP_C() self.algo = LEAP_C(eye_config, config)
def run(self, current_image_gray, current_image_gray_clean, calib, calibration_samples): def run(self, current_image_gray, current_image_gray_clean, calib):
self.algo.current_image_gray = current_image_gray self.algo.current_image_gray = current_image_gray
self.algo.current_image_gray_clean = current_image_gray_clean self.algo.current_image_gray_clean = current_image_gray_clean
self.algo.calib = calib self.algo.calib = calib
self.algo.calibration_samples = calibration_samples
img, x, y, per = self.algo.leap_run() img, x, y, per = self.algo.leap_run()
return img, x, y, per return img, x, y, per