From d6d19383bd0dba3e8af39d2101658eb2dbbaf847 Mon Sep 17 00:00:00 2001 From: Sebastian Fitt Date: Sat, 29 Apr 2023 14:32:54 +0200 Subject: [PATCH] refactor: extract EyeInfo enums and dataclasses --- EyeTrackApp/camera_widget.py | 8 +++--- EyeTrackApp/eye.py | 26 ++++++++++++++++++ EyeTrackApp/eye_processor.py | 51 +++++++++++------------------------- EyeTrackApp/ransac.py | 4 +-- 4 files changed, 48 insertions(+), 41 deletions(-) create mode 100644 EyeTrackApp/eye.py diff --git a/EyeTrackApp/camera_widget.py b/EyeTrackApp/camera_widget.py index 9043d70..361757d 100644 --- a/EyeTrackApp/camera_widget.py +++ b/EyeTrackApp/camera_widget.py @@ -2,7 +2,7 @@ import PySimpleGUI as sg from config import EyeTrackConfig from config import EyeTrackSettingsConfig from threading import Event, Thread -from eye_processor import EyeProcessor, InformationOrigin +from eye_processor import EyeProcessor, EyeInfoOrigin from enum import Enum from queue import Queue, Empty from camera import Camera, CameraState @@ -312,7 +312,7 @@ class CameraWidget: graph = window[self.gui_output_graph] graph.erase() - if eye_info.info_type != InformationOrigin.FAILURE: #and not eye_info.blink: + if eye_info.info_type != EyeInfoOrigin.FAILURE: #and not eye_info.blink: graph.update(background_color="white") if not np.isnan(eye_info.x) and not np.isnan(eye_info.y): @@ -332,10 +332,10 @@ class CameraWidget: # elif eye_info.blink: # graph.update(background_color="#6f4ca1") - elif eye_info.info_type == InformationOrigin.FAILURE: + elif eye_info.info_type == EyeInfoOrigin.FAILURE: graph.update(background_color="red") # Relay information to OSC - if eye_info.info_type != InformationOrigin.FAILURE: + if eye_info.info_type != EyeInfoOrigin.FAILURE: self.osc_queue.put((self.eye_id, eye_info)) except Empty: pass diff --git a/EyeTrackApp/eye.py b/EyeTrackApp/eye.py new file mode 100644 index 0000000..04eb6ef --- /dev/null +++ b/EyeTrackApp/eye.py @@ -0,0 +1,26 @@ +from dataclasses import dataclass +from enum import Enum, IntEnum + +class EyeId(IntEnum): + RIGHT = 0 + LEFT = 1 + BOTH = 2 + SETTINGS = 3 + + +class EyeInfoOrigin(Enum): + RANSAC = 1 + BLOB = 2 + FAILURE = 3 + HSF = 4 + HSRAC = 5 + DADDY = 6 + + +@dataclass +class EyeInfo: + info_type: EyeInfoOrigin + x: float + y: float + pupil_dialation: float + blink: float diff --git a/EyeTrackApp/eye_processor.py b/EyeTrackApp/eye_processor.py index 5b3c471..7abd9f1 100644 --- a/EyeTrackApp/eye_processor.py +++ b/EyeTrackApp/eye_processor.py @@ -59,29 +59,9 @@ from ransac import * from hsrac import External_Run_HSRACS from blink import * - +from eye import EyeInfo, EyeInfoOrigin from intensity_eye_open import * -class InformationOrigin(Enum): - RANSAC = 1 - BLOB = 2 - FAILURE = 3 - HSF = 4 - HSRAC = 5 - DADDY = 6 - -bbb = 0 -@dataclass -class EyeInformation: - info_type: InformationOrigin - x: float - y: float - pupil_dialation: float - blink: float - - -lowb = np.array(0) - def run_once(f): def wrapper(*args, **kwargs): @@ -179,8 +159,8 @@ class EyeProcessor: self.prev_x = None self.prev_y = None self.bd_blink = False - self.current_algo = InformationOrigin.HSRAC - + self.current_algo = EyeInfoOrigin.HSRAC + try: min_cutoff = float(self.settings.gui_min_cutoff) # 0.0004 @@ -196,7 +176,7 @@ class EyeProcessor: beta=beta ) - def output_images_and_update(self, threshold_image, output_information: EyeInformation): + def output_images_and_update(self, threshold_image, output_information: EyeInfo): try: image_stack = np.concatenate( ( @@ -280,7 +260,7 @@ class EyeProcessor: else: self.eyeopen = ibo - self.output_images_and_update(self.thresh, EyeInformation(self.current_algo, self.out_x, self.out_y, 0, self.eyeopen)) + self.output_images_and_update(self.thresh, EyeInfo(self.current_algo, self.out_x, self.out_y, 0, self.eyeopen)) @@ -293,7 +273,7 @@ class EyeProcessor: self.rawx, self.rawy, self.eyeopen = self.er_daddy.run(self.current_image_gray) # Daddy also uses a one euro filter, so I'll have to use it twice, but I'm not going to think too much about it. self.out_x, self.out_y = cal.cal_osc(self, self.rawx, self.rawy) - self.current_algorithm = InformationOrigin.DADDY + self.current_algorithm = EyeInfoOrigin.DADDY def HSRACM(self): # todo: added process to initialise er_hsrac when resolution changes @@ -302,26 +282,26 @@ class EyeProcessor: self.prev_x = self.rawx self.prev_y = self.rawy self.out_x, self.out_y = cal.cal_osc(self, self.rawx, self.rawy) - self.current_algorithm = InformationOrigin.HSRAC + self.current_algorithm = EyeInfoOrigin.HSRAC def HSFM(self): # todo: added process to initialise er_hsf when resolution changes self.rawx, self.rawy, self.thresh = self.er_hsf.run(self.current_image_gray) self.eyeopen = self.ibo.intense(self.rawx, self.rawy, self.current_image_gray) self.out_x, self.out_y = cal.cal_osc(self, self.rawx, self.rawy) - self.current_algorithm = InformationOrigin.HSF + self.current_algorithm = EyeInfoOrigin.HSF def RANSAC3DM(self): current_image_gray_copy = self.current_image_gray.copy() # Duplicate before overwriting in RANSAC3D. self.rawx, self.rawy, self.thresh = RANSAC3D(self) self.out_x, self.out_y = cal.cal_osc(self, self.rawx, self.rawy) - self.current_algorithm = InformationOrigin.RANSAC + self.current_algorithm = EyeInfoOrigin.RANSAC def BLOBM(self): print("calling") self.rawx, self.rawy, self.thresh = BLOB(self) self.out_x, self.out_y = cal.cal_osc(self, self.rawx, self.rawy) - self.current_algorithm = InformationOrigin.BLOB + self.current_algorithm = EyeInfoOrigin.BLOB @@ -359,6 +339,7 @@ class EyeProcessor: # Run the following somewhere # self.daddy = External_Run_DADDY() + self.firstalgo = None self.secondalgo = None self.thirdalgo = None @@ -473,23 +454,23 @@ class EyeProcessor: # cx, cy, thresh = HSRAC(self) # out_x, out_y = cal_osc(self, cx, cy) # if cx == 0: - # self.output_images_and_update(thresh, EyeInformation(InformationOrigin.HSRAC, out_x, out_y, 0, True)) #update app + # self.output_images_and_update(thresh, EyeInfo(EyeInfoOrigin.HSRAC, out_x, out_y, 0, True)) #update app # else: - # self.output_images_and_update(thresh, EyeInformation(InformationOrigin.HSRAC, out_x, out_y, 0, self.blinkvalue)) + # self.output_images_and_update(thresh, EyeInfo(EyeInfoOrigin.HSRAC, out_x, out_y, 0, self.blinkvalue)) # cx, cy, thresh = RANSAC3D(self) # out_x, out_y = cal_osc(self, cx, cy) - # self.output_images_and_update(thresh, EyeInformation(InformationOrigin.RANSAC, out_x, out_y, 0, False)) #update app + # self.output_images_and_update(thresh, EyeInfo(EyeInfoOrigin.RANSAC, out_x, out_y, 0, False)) #update app # cx, cy, larger_threshold = BLOB(self) # out_x, out_y = cal_osc(self, cx, cy) - # self.output_images_and_update(larger_threshold, EyeInformation(InformationOrigin.BLOB, out_x, out_y, 0, False)) #update app + # self.output_images_and_update(larger_threshold, EyeInfo(EyeInfoOrigin.BLOB, out_x, out_y, 0, False)) #update app #center_x, center_y, frame = HSF(self) #run algo #out_x, out_y = cal_osc(self, center_x, center_y) #filter and calibrate - #self.output_images_and_update(frame, EyeInformation(InformationOrigin.HSF, out_x, out_y, 0, False)) #update app + #self.output_images_and_update(frame, EyeInfo(EyeInfoOrigin.HSF, out_x, out_y, 0, False)) #update app self.ALGOSELECT() #run our algos in priority order set in settings #self.BLOBM() diff --git a/EyeTrackApp/ransac.py b/EyeTrackApp/ransac.py index 1800a40..d50766d 100644 --- a/EyeTrackApp/ransac.py +++ b/EyeTrackApp/ransac.py @@ -335,7 +335,7 @@ def RANSAC3D(self): self.failed = self.failed + 1 #we have failed, move onto next algo return 0, 0, thresh # Shove a concatenated image out to the main GUI thread for rendering - #self.output_images_and_update(thresh, EyeInformation(InformationOrigin.FAILURE, 0 ,0, 0, False)) + #self.output_images_and_update(thresh, EyeInfo(EyeInfoOrigin.FAILURE, 0 ,0, 0, False)) #self.output_images_and_update(thresh, output_info) #except: - # self.output_images_and_update(thresh, EyeInformation(InformationOrigin.RANSAC, out_x, out_y, 0, self.blinkvalue)) + # self.output_images_and_update(thresh, EyeInfo(EyeInfoOrigin.RANSAC, out_x, out_y, 0, self.blinkvalue))