From fbd6fb212168f2e03076179899045d5c2818eb1a Mon Sep 17 00:00:00 2001 From: Blabzillaweasel Date: Sun, 9 Feb 2025 22:54:09 +1300 Subject: [PATCH] Made boolsfor commonly used checks --- EyeTrackApp/utils/smart_inversion.py | 59 +++++++++++++++++++++------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/EyeTrackApp/utils/smart_inversion.py b/EyeTrackApp/utils/smart_inversion.py index bf60b7c..0d2a1f5 100644 --- a/EyeTrackApp/utils/smart_inversion.py +++ b/EyeTrackApp/utils/smart_inversion.py @@ -30,25 +30,53 @@ def smart_inversion(self, var, out_x, out_y): if self.settings.gui_smartinversion_select_right: tracked_eye_x = var.r_eye_x tracked_eye_y = var.right_y - recessive_eye = EyeId.LEFT - dominant_eye = EyeId.RIGHT + is_rec_eye = (self.eye_id == EyeId.LEFT) + is_dom_eye = (self.eye_id == EyeId.RIGHT) else: tracked_eye_x = var.l_eye_x tracked_eye_y = var.left_y - recessive_eye = EyeId.RIGHT - dominant_eye = EyeId.LEFT + is_rec_eye = (self.eye_id == EyeId.RIGHT) + is_dom_eye = (self.eye_id == EyeId.LEFT) + + #Provides some booleans that are a bit easier to use repeatedly + dom_is_inward = ( + (self.settings.gui_smartinversion_select_right and var.r_eye_x < 0) + or + (not self.settings.gui_smartinversion_select_right and var.l_eye_x > 0) + ) + rec_is_inward = ( + (self.settings.gui_smartinversion_select_right and var.l_eye_x > 0) + or + (not self.settings.gui_smartinversion_select_right and var.r_eye_x < 0) + ) + dom_in_inv_range = ( + (self.settings.gui_smartinversion_select_right and var.r_eye_x < -self.settings.gui_smartinversion_minthresh) + or + (not self.settings.gui_smartinversion_select_right and var.l_eye_x > self.settings.gui_smartinversion_minthresh) + ) + rec_in_inv_range = ( + (self.settings.gui_smartinversion_select_right and var.l_eye_x > self.settings.gui_smartinversion_minthresh) + or + (not self.settings.gui_smartinversion_select_right and var.r_eye_x < -self.settings.gui_smartinversion_minthresh) + ) + looking_same_dir = (var.r_eye_x * var.l_eye_x > 0) + x_diff = abs(var.r_eye_x - var.l_eye_x) #Checks if eyes are straight, and then sets eye gaze forward until inversion threshold is met - if (0 < var.l_eye_x <= self.settings.gui_smartinversion_minthresh) and (self.settings.gui_smartinversion_minthresh <= var.r_eye_x < 0): + if dom_is_inward and not rec_in_inv_range and (rec_is_inward or x_diff > 0.4): if not self.smartinversion_stare_ahead: self.smartinversion_smoothing_progress = 1 self.smartinversion_is_inverted = False + print(f"Stare Ahead Activated") + self.smartinversion_stare_ahead = True tracked_eye_x = 0 - self.smartinversion_stare_ahead = True + elif self.smartinversion_stare_ahead: + print(f"Stare Ahead Deactivated") + self.smartinversion_stare_ahead = False #Checks if eyes are inverted, and then activates inversion if the conditions have been true for a specified number of frames. - if (var.l_eye_x > self.settings.gui_smartinversion_minthresh and var.r_eye_x < -self.settings.gui_smartinversion_minthresh): + if dom_is_inward and rec_in_inv_range: self.smartinversion_inverted_frame_count = min(self.smartinversion_inverted_frame_count + 1, self.settings.gui_smartinversion_frame_count) if self.smartinversion_inverted_frame_count == self.settings.gui_smartinversion_frame_count: @@ -56,22 +84,21 @@ def smart_inversion(self, var, out_x, out_y): self.smartinversion_smoothing_progress = 1 self.smartinversion_normal_frame_count = 0 self.smartinversion_is_inverted = True - self.smartinversion_stare_ahead = False tracked_eye_x = 0 print(f"Inversion Activated") elif self.smartinversion_inverted_frame_count > 0: self.smartinversion_inverted_frame_count = 0 #Checks if the eyes are no longer inverted, and then clears inversion if the conditions haven't been true for a specified number of frames. - if self.smartinversion_is_inverted and (not (var.l_eye_x > self.settings.gui_smartinversion_minthresh and var.r_eye_x < -self.settings.gui_smartinversion_minthresh)): + if self.smartinversion_is_inverted and (not (dom_in_inv_range and rec_in_inv_range)): self.smartinversion_normal_frame_count = min(self.smartinversion_normal_frame_count + 1, self.settings.gui_smartinversion_frame_count) if self.smartinversion_normal_frame_count == self.settings.gui_smartinversion_frame_count: if self.smartinversion_is_inverted: self.smartinversion_is_inverted = False self.smartinversion_inverted_frame_count = 0 - self.smartinversion_stare_ahead = False - print(f"Inversion Cleared") + print(f"Inversion Deactivated") + elif self.smartinversion_normal_frame_count > 0: self.smartinversion_normal_frame_count = 0 @@ -83,7 +110,7 @@ def smart_inversion(self, var, out_x, out_y): lerp_factor = 0.2 if self.smartinversion_is_inverted: - if self.eye_id == recessive_eye: + if is_rec_eye: self.smartinversion_smoothed_eye_x += (-tracked_eye_x - self.smartinversion_smoothed_eye_x) * lerp_factor else: @@ -93,14 +120,16 @@ def smart_inversion(self, var, out_x, out_y): out_x = self.smartinversion_smoothed_eye_x #Logic if inversion is active, but smoothing is not active - elif self.smartinversion_is_inverted and self.eye_id == recessive_eye: + elif self.smartinversion_is_inverted and is_rec_eye: out_x = -tracked_eye_x #Limits the maximum allowed inwards rotation if detected as cross-eyed if self.smartinversion_is_inverted: if self.eye_id == EyeId.LEFT: - out_x = min(out_x, self.settings.gui_smartinversion_rotation_clamp) + clamped_x = min(out_x, self.settings.gui_smartinversion_rotation_clamp) + out_x = clamped_x else: - out_x = max(out_x, -self.settings.gui_smartinversion_rotation_clamp) + clamped_x = max(out_x, -self.settings.gui_smartinversion_rotation_clamp) + out_x = clamped_x return out_x, out_y