From 7cb3cc4731af40029920db0857ab6db73c7ee1e2 Mon Sep 17 00:00:00 2001 From: PeterWaIIace Date: Sun, 29 Dec 2024 19:21:30 +0100 Subject: [PATCH] adding lowpass filter for keypoints --- examples/simple_example_v3.py | 2 +- eyeGestures/__init__.py | 8 +++++++- eyeGestures/utils.py | 11 +++++++++++ pyproject.toml | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/examples/simple_example_v3.py b/examples/simple_example_v3.py index 2764b72..dddb8fb 100644 --- a/examples/simple_example_v3.py +++ b/examples/simple_example_v3.py @@ -14,7 +14,7 @@ # Set up the screen screen = pygame.display.set_mode((screen_width, screen_height)) -pygame.display.set_caption("EyeGestures v2 example") +pygame.display.set_caption("EyeGestures v3 example") font_size = 48 bold_font = pygame.font.Font(None, font_size) bold_font.set_bold(True) # Set the font to bold diff --git a/eyeGestures/__init__.py b/eyeGestures/__init__.py index 3573233..d707562 100644 --- a/eyeGestures/__init__.py +++ b/eyeGestures/__init__.py @@ -5,7 +5,7 @@ from eyeGestures.calibration_v1 import Calibrator as Calibrator_v1 from eyeGestures.calibration_v2 import Calibrator as Calibrator_v2 from eyeGestures.gevent import Gevent, Cevent -from eyeGestures.utils import timeit, Buffor +from eyeGestures.utils import timeit, Buffor, low_pass_filter_fourier import numpy as np import pickle import time @@ -40,6 +40,7 @@ def __init__(self, calibration_radius = 1000): self.velocity_max = dict() self.velocity_min = dict() self.fixationTracker = dict() + self.key_points_buffer = dict() self.starting_head_position = np.zeros((1,2)) self.starting_size = np.zeros((1,2)) @@ -130,6 +131,7 @@ def addContext(self, context): self.velocity_max[context] = 0 self.velocity_min[context] = 100000000 self.fixationTracker[context] = Fixation(0,0,100) + self.key_points_buffer[context] = [] def step(self, frame, calibration, width, height, context="main"): @@ -138,6 +140,10 @@ def step(self, frame, calibration, width, height, context="main"): self.calibration[context] = calibration key_points, blink, sub_frame = self.getLandmarks(frame) + self.key_points_buffer[context].append(key_points) + if len(self.key_points_buffer[context]) > 10: + self.key_points_buffer[context].pop(0) + key_points = low_pass_filter_fourier(key_points,200) y_point = self.clb[context].predict(key_points) self.average_points[context][1:,:] = self.average_points[context][:(self.average_points[context].shape[0] - 1),:] diff --git a/eyeGestures/utils.py b/eyeGestures/utils.py index 58c9be2..5445494 100644 --- a/eyeGestures/utils.py +++ b/eyeGestures/utils.py @@ -24,6 +24,17 @@ def inner(*args, **kwargs): return ret return inner +def low_pass_filter_fourier(data, cutoff_frequency): + # Apply Fourier Transform-based filter column-wise + filtered_data = np.zeros_like(data, dtype=float) + for col in range(data.shape[1]): # Iterate over each column + fft_data = np.fft.fft(data[:, col]) + frequencies = np.fft.fftfreq(len(data[:, col])) + # Apply the low-pass filter + fft_data[np.abs(frequencies) > cutoff_frequency] = 0 + # Perform Inverse Fourier Transform + filtered_data[:, col] = np.fft.ifft(fft_data).real + return filtered_data def shape_to_np(shape, dtype="int"): """ diff --git a/pyproject.toml b/pyproject.toml index 2404cd6..62681a6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,7 +42,7 @@ exclude = [ [project] name = "eyeGestures" -version = "3.1.4" +version = "3.2.4" authors = [ { name="Piotr Walas", email="piotr.walas@eyegestures.com" }, ]