Skip to content

Commit

Permalink
fix: LEAP Eyelid, tune RANSAC BLINK
Browse files Browse the repository at this point in the history
  • Loading branch information
RedHawk989 committed Sep 30, 2023
1 parent 03a1a96 commit 5a6248b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
22 changes: 12 additions & 10 deletions EyeTrackApp/eye_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def capture_crop_rotate_image(self):
pass

def UPDATE(self):

# print(self.eyeopen)
if self.settings.gui_BLINK:
self.eyeopen = BLINK(self)

Expand All @@ -269,6 +269,7 @@ def UPDATE(self):
self.eyeopen = 0.0

if self.bd_blink == True:
prin("blinks")
pass

if self.settings.gui_IBO and self.eyeopen != 0.0:
Expand All @@ -280,17 +281,17 @@ def UPDATE(self):
self.settings.ibo_average_output_samples,
)

if self.settings.gui_LEAP_lid:
if self.settings.gui_LEAP_lid and self.eyeopen != 0.0:
(
self.current_image_gray,
self.rawx,
self.rawy,
self.eyeopen,
) = self.er_leap.run(self.current_image_gray)
print(self.eyeopen)
# print(self.eyeopen)

if (
len(self.prev_y_list) >= 200
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.append(self.out_y)
Expand All @@ -300,10 +301,10 @@ def UPDATE(self):
# print(abs(self.eyeopen - self.past_blink))
blink_vec = min(abs(self.eyeopen - self.past_blink), 1) # clamp to 1

# if blink_vec >= 0.17:
# if blink_vec >= 0.1 or blink_vec == 0.0 and (self.out_y - self.prev_y) < 0.0:
# self.out_x = sum(self.prev_x_list) / len(self.prev_x_list)
# self.out_y = sum(self.prev_y_list) / len(self.prev_y_list)
#if blink_vec >= 0.2:
if blink_vec >= 0.15 or blink_vec == 0.0 and (self.out_y - self.prev_y) < 0.0:
#self.out_x = sum(self.prev_x_list) / len(self.prev_x_list)
self.out_y = sum(self.prev_y_list) / len(self.prev_y_list)
# print('AVG', self.out_y, len(self.prev_y_list))

self.past_blink = self.eyeopen
Expand All @@ -317,7 +318,7 @@ def UPDATE(self):
if self.settings.gui_RANSACBLINK and self.eyeopen == 0.0:
pass
else:
self.eyeopen = 0.9
self.eyeopen = 0.8

def BLINKM(self):
self.eyeopen = BLINK(self)
Expand All @@ -330,7 +331,7 @@ def LEAPM(self):
self.thresh = self.current_image_gray.copy()
self.out_x, self.out_y = cal.cal_osc(self, self.rawx, self.rawy)
self.current_algorithm = EyeInfoOrigin.LEAP
print(self.eyeopen)
# print(self.eyeopen)

def DADDYM(self):
# todo: We should have a proper variable for drawing.
Expand Down Expand Up @@ -363,6 +364,7 @@ def HSRACM(self):
self.rawx, self.rawy, self.thresh, ranblink = RANSAC3D(self, True)
if self.settings.gui_RANSACBLINK: # might be redundant
self.eyeopen = ranblink
# print("RANBLINK", ranblink)

# print(self.radius)
# if self.prev_x is None:
Expand Down
35 changes: 21 additions & 14 deletions EyeTrackApp/leap.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,16 @@ def __init__(self):
process.nice(psutil.BELOW_NORMAL_PRIORITY_CLASS) # Windows
process.nice()
# See https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getpriorityclass#return-value for values
min_cutoff = 0.04
beta = 0.9
min_cutoff = 0.9
beta = 5.0
# print(np.random.rand(22, 2))
# noisy_point = np.array([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1])
one_euro_filter = OneEuroFilter(
self.one_euro_filter = OneEuroFilter(
np.random.rand(7, 2), min_cutoff=min_cutoff, beta=beta
)
#self.one_euro_filter_open = OneEuroFilter(
# np.random.rand(1, 2), min_cutoff=0.01, beta=0.04
#)
self.dmax = 0
self.dmin = 0
self.openlist = []
Expand Down Expand Up @@ -174,6 +177,7 @@ def leap_run(self):
if not self.output_queue.empty():

frame, pre_landmark = self.output_queue.get()
pre_landmark = self.one_euro_filter(pre_landmark)
# frame = cv2.resize(frame, (112, 112))

for point in pre_landmark:
Expand Down Expand Up @@ -220,16 +224,16 @@ def leap_run(self):
# d2 = math.dist(pre_landmark[2], pre_landmark[4])
# d = d + d2

if len(self.openlist) < 2000: # TODO expose as setting?
if len(self.openlist) < 5000: # TODO expose as setting?
self.openlist.append(d)
else:
if d >= np.percentile(self.openlist, 99) or d <= np.percentile(
self.openlist, 1
):
pass
else:
self.openlist.pop(0)
self.openlist.append(d)
# if d >= np.percentile(self.openlist, 99) or d <= np.percentile(
# self.openlist, 1
# ):
# pass
#else:
self.openlist.pop(0)
self.openlist.append(d)

try:
per = (d - max(self.openlist)) / (
Expand All @@ -239,15 +243,18 @@ def leap_run(self):
except:
per = 0.7
pass
# print(d, per)
x = pre_landmark[6][0]
y = pre_landmark[6][1]
frame = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

per = d - 0.1
# per = d - 0.1
self.last_lid = per
if per <= 0.1:
# pera = np.array([per, per])
#self.one_euro_filter_open(pera)
if per <= 0.2: #TODO: EXPOSE AS SETTING
per == 0.0
# print(per)
# print(per)
return frame, float(x), float(y), per

frame = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
Expand Down
9 changes: 5 additions & 4 deletions EyeTrackApp/osc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def output_osc(eye_x, eye_y, eye_blink, last_blink, self):
self.l_eye_blink = eye_blink

if self.l_eye_blink == 0.0:
if last_blink > 0.2: #when binary blink is on, blinks may be too fast for OSC so we repeat them.
for i in range(5):
if last_blink > 0.15: #when binary blink is on, blinks may be too fast for OSC so we repeat them.
for i in range(4):
self.client.send_message(self.config.osc_left_eye_close_address, eyelid_transformer(self,self.l_eye_blink))
last_blink = time.time() - last_blink
if self.config.gui_eye_falloff:
Expand All @@ -69,8 +69,9 @@ def output_osc(eye_x, eye_y, eye_blink, last_blink, self):
self.r_eye_blink = eye_blink

if self.r_eye_blink == 0.0:
if last_blink > 0.2: #when binary blink is on, blinks may be too fast for OSC so we repeat them.
for i in range(5):
if last_blink > 0.15: #when binary blink is on, blinks may be too fast for OSC so we repeat them.
print("REPEATING R BLINK")
for i in range(4):
self.client.send_message(self.config.osc_right_eye_close_address, eyelid_transformer(self,self.r_eye_blink))
last_blink = time.time() - last_blink
if self.config.gui_eye_falloff:
Expand Down
3 changes: 2 additions & 1 deletion EyeTrackApp/ransac.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,9 @@ def RANSAC3D(self, hsrac_en):
elif len(self.blink_list) < 10000:
self.blink_list.append(abs(perscalarw - perscalarh))

if abs(perscalarw - perscalarh) >= np.percentile(self.blink_list, 94):
if abs(perscalarw - perscalarh) >= np.percentile(self.blink_list, 92):
blink = 0.0
print("RANSAC BLINK")

try:
cv2.drawContours(
Expand Down

0 comments on commit 5a6248b

Please sign in to comment.