Skip to content

Commit

Permalink
Merge pull request #32 from NativeSensors/develop
Browse files Browse the repository at this point in the history
Develop 2.7.4
  • Loading branch information
PeterWaIIace authored Oct 23, 2024
2 parents 4e7b7a0 + 9438c12 commit 3c7b37e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
60 changes: 39 additions & 21 deletions examples/simple_example_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,55 @@
import pygame
import numpy as np

pygame.init()
pygame.font.init()

# Get the display dimensions
screen_info = pygame.display.Info()
screen_width = screen_info.current_w
screen_height = screen_info.current_h

# Set up the screen
screen = pygame.display.set_mode((screen_width, screen_height), pygame.FULLSCREEN)
pygame.display.set_caption("EyeGestures v2 example")
font_size = 48
bold_font = pygame.font.Font(None, font_size)
bold_font.set_bold(True) # Set the font to bold

dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(f'{dir_path}/..')

from eyeGestures.utils import VideoCapture
from eyeGestures.eyegestures import EyeGestures_v2

gestures = EyeGestures_v2()
gestures.uploadCalibrationMap([[1,0],[1,1],[0,1],[0.01,0.01],[0.5,0.5],[0.5,1],[0.5,0],[0,0.5],[1,0.5]])
gestures.enableCNCalib()
gestures.setClassicalImpact(2)
gestures.setFixation(1.0)
cap = VideoCapture(0)

# Initialize Pygame
pygame.init()
pygame.font.init()

# Get the display dimensions
screen_info = pygame.display.Info()
screen_width = screen_info.current_w
screen_height = screen_info.current_h
x = np.arange(0, 1.1, 0.2)
y = np.arange(0, 1.1, 0.2)

# Set up the screen
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Fullscreen Red Cursor")
xx, yy = np.meshgrid(x, y)

calibration_map = np.column_stack([xx.ravel(), yy.ravel()])
np.random.shuffle(calibration_map)
gestures.uploadCalibrationMap(calibration_map,context="my_context")
gestures.setClassicalImpact(2)
gestures.setFixation(1.0)
# Initialize Pygame
# Set up colors
RED = (255, 0, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 100)
BLUE = (100, 0, 255)
GREEN = (0, 255, 0)
YELLOW = (255,255,0)
BLANK = (0,0,0)
WHITE = (255, 255, 255)

clock = pygame.time.Clock()

# Main game loop
running = True
iterator = 0
prev_x = 0
prev_y = 0
while running:
# Event handling
for event in pygame.event.get():
Expand All @@ -55,8 +67,7 @@
ret, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

calibrate = (iterator <= 600)
iterator += 1
calibrate = (iterator <= 25) # calibrate 25 points

event, calibration = gestures.step(frame, calibrate, screen_width, screen_height, context="my_context")

Expand All @@ -72,10 +83,17 @@
text_surface = my_font.render(f'{event.fixation}', False, (0, 0, 0))
screen.blit(text_surface, (0,0))
if calibrate:
if calibration.point[0] != prev_x or calibration.point[1] != prev_y:
iterator += 1
prev_x = calibration.point[0]
prev_y = calibration.point[1]
# pygame.draw.circle(screen, GREEN, fit_point, calibration_radius)
pygame.draw.circle(screen, BLUE, calibration.point, calibration.acceptance_radius)
text_surface = bold_font.render(f"{iterator}/{25}", True, WHITE)
text_square = text_surface.get_rect(center=calibration.point)
screen.blit(text_surface, text_square)
else:
pygame.draw.circle(screen, YELLOW, calibration.point, calibration.acceptance_radius)
pass
pygame.draw.circle(screen, RED, event.point, 50)
pygame.display.flip()

Expand Down
12 changes: 7 additions & 5 deletions eyeGestures/calibration_v2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import numpy as np
import sklearn.linear_model as scireg
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestRegressor

def euclidean_distance(point1, point2):
return np.linalg.norm(point1 - point2)
Expand All @@ -15,8 +17,8 @@ def __init__(self,CALIBRATION_RADIUS=1000):
self.Y_y = []
self.Y_x = []
self.reg = None
self.reg_x = scireg.Ridge(alpha=1.0)
self.reg_y = scireg.Ridge(alpha=1.0)
self.reg_x = scireg.Lasso(alpha=0.5)
self.reg_y = scireg.Lasso(alpha=0.5)
self.fitted = False

self.matrix = CalibrationMatrix()
Expand All @@ -31,9 +33,9 @@ def add(self,x,y):
self.Y_y.append(y[1])
self.Y_x.append(y[0])

__tmp_X =np.array(self.X)
__tmp_Y_y =np.array(self.Y_y)
__tmp_Y_x =np.array(self.Y_x)
__tmp_X = np.array(self.X)
__tmp_Y_y = np.array(self.Y_y)
__tmp_Y_x = np.array(self.Y_x)

self.reg_x.fit(__tmp_X,__tmp_Y_x)
self.reg_y.fit(__tmp_X,__tmp_Y_y)
Expand Down
2 changes: 1 addition & 1 deletion eyeGestures/eyegestures.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def step(self, frame, calibration, width, height, context="main"):
if self.iterator[context] > 10:
self.iterator[context] = 0
self.clb[context].movePoint()
self.clb[context].increase_precision()
# self.clb[context].increase_precision()

gevent = Gevent(averaged_point,blink,fixation)
cevent = Cevent(self.clb[context].getCurrentPoint(width,height),self.clb[context].acceptance_radius, self.clb[context].calibration_radius)
Expand Down

0 comments on commit 3c7b37e

Please sign in to comment.