refactor: extract EyeInfo enums and dataclasses

This commit is contained in:
Sebastian Fitt 2023-04-29 14:32:54 +02:00
parent 632ec2a838
commit d6d19383bd
4 changed files with 48 additions and 41 deletions

View File

@ -2,7 +2,7 @@ import PySimpleGUI as sg
from config import EyeTrackConfig from config import EyeTrackConfig
from config import EyeTrackSettingsConfig from config import EyeTrackSettingsConfig
from threading import Event, Thread from threading import Event, Thread
from eye_processor import EyeProcessor, InformationOrigin from eye_processor import EyeProcessor, EyeInfoOrigin
from enum import Enum from enum import Enum
from queue import Queue, Empty from queue import Queue, Empty
from camera import Camera, CameraState from camera import Camera, CameraState
@ -312,7 +312,7 @@ class CameraWidget:
graph = window[self.gui_output_graph] graph = window[self.gui_output_graph]
graph.erase() 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") graph.update(background_color="white")
if not np.isnan(eye_info.x) and not np.isnan(eye_info.y): if not np.isnan(eye_info.x) and not np.isnan(eye_info.y):
@ -332,10 +332,10 @@ class CameraWidget:
# elif eye_info.blink: # elif eye_info.blink:
# graph.update(background_color="#6f4ca1") # graph.update(background_color="#6f4ca1")
elif eye_info.info_type == InformationOrigin.FAILURE: elif eye_info.info_type == EyeInfoOrigin.FAILURE:
graph.update(background_color="red") graph.update(background_color="red")
# Relay information to OSC # 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)) self.osc_queue.put((self.eye_id, eye_info))
except Empty: except Empty:
pass pass

26
EyeTrackApp/eye.py Normal file
View File

@ -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

View File

@ -59,29 +59,9 @@ from ransac import *
from hsrac import External_Run_HSRACS from hsrac import External_Run_HSRACS
from blink import * from blink import *
from eye import EyeInfo, EyeInfoOrigin
from intensity_eye_open import * 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 run_once(f):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
@ -179,8 +159,8 @@ class EyeProcessor:
self.prev_x = None self.prev_x = None
self.prev_y = None self.prev_y = None
self.bd_blink = False self.bd_blink = False
self.current_algo = InformationOrigin.HSRAC self.current_algo = EyeInfoOrigin.HSRAC
try: try:
min_cutoff = float(self.settings.gui_min_cutoff) # 0.0004 min_cutoff = float(self.settings.gui_min_cutoff) # 0.0004
@ -196,7 +176,7 @@ class EyeProcessor:
beta=beta 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: try:
image_stack = np.concatenate( image_stack = np.concatenate(
( (
@ -280,7 +260,7 @@ class EyeProcessor:
else: else:
self.eyeopen = ibo 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) 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. # 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.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): def HSRACM(self):
# todo: added process to initialise er_hsrac when resolution changes # todo: added process to initialise er_hsrac when resolution changes
@ -302,26 +282,26 @@ class EyeProcessor:
self.prev_x = self.rawx self.prev_x = self.rawx
self.prev_y = self.rawy self.prev_y = self.rawy
self.out_x, self.out_y = cal.cal_osc(self, self.rawx, 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): def HSFM(self):
# todo: added process to initialise er_hsf when resolution changes # 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.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.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.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): def RANSAC3DM(self):
current_image_gray_copy = self.current_image_gray.copy() # Duplicate before overwriting in RANSAC3D. current_image_gray_copy = self.current_image_gray.copy() # Duplicate before overwriting in RANSAC3D.
self.rawx, self.rawy, self.thresh = RANSAC3D(self) self.rawx, self.rawy, self.thresh = RANSAC3D(self)
self.out_x, self.out_y = cal.cal_osc(self, self.rawx, self.rawy) 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): def BLOBM(self):
print("calling") print("calling")
self.rawx, self.rawy, self.thresh = BLOB(self) self.rawx, self.rawy, self.thresh = BLOB(self)
self.out_x, self.out_y = cal.cal_osc(self, self.rawx, self.rawy) 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 # Run the following somewhere
# self.daddy = External_Run_DADDY() # self.daddy = External_Run_DADDY()
self.firstalgo = None self.firstalgo = None
self.secondalgo = None self.secondalgo = None
self.thirdalgo = None self.thirdalgo = None
@ -473,23 +454,23 @@ class EyeProcessor:
# cx, cy, thresh = HSRAC(self) # cx, cy, thresh = HSRAC(self)
# out_x, out_y = cal_osc(self, cx, cy) # out_x, out_y = cal_osc(self, cx, cy)
# if cx == 0: # 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: # 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) # cx, cy, thresh = RANSAC3D(self)
# out_x, out_y = cal_osc(self, cx, cy) # 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) # cx, cy, larger_threshold = BLOB(self)
# out_x, out_y = cal_osc(self, cx, cy) # 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 #center_x, center_y, frame = HSF(self) #run algo
#out_x, out_y = cal_osc(self, center_x, center_y) #filter and calibrate #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.ALGOSELECT() #run our algos in priority order set in settings
#self.BLOBM() #self.BLOBM()

View File

@ -335,7 +335,7 @@ def RANSAC3D(self):
self.failed = self.failed + 1 #we have failed, move onto next algo self.failed = self.failed + 1 #we have failed, move onto next algo
return 0, 0, thresh return 0, 0, thresh
# Shove a concatenated image out to the main GUI thread for rendering # 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) #self.output_images_and_update(thresh, output_info)
#except: #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))