Skip to content

Commit

Permalink
Vis Eye Overlay: Fix overlay movement on Retina displays
Browse files Browse the repository at this point in the history
  • Loading branch information
papr committed Feb 2, 2018
1 parent f1ce664 commit abac88c
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions pupil_src/shared_modules/vis_eye_video_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import cv2
import numpy as np
from pyglui import ui
from glfw import glfwGetCursorPos, glfwGetWindowSize, glfwGetCurrentContext
from glfw import glfwGetCursorPos, glfwGetFramebufferSize, glfwGetWindowSize, glfwGetCurrentContext

from plugin import Visualizer_Plugin_Base
from player_methods import transparent_image_overlay
Expand Down Expand Up @@ -44,7 +44,7 @@ def correlate_eye_world(eye_timestamps, world_timestamps):


class Eye_Wrapper(object):
def __init__(self, g_pool, eyeid, pos, hflip=False, vflip=False):
def __init__(self, g_pool, eyeid, pos, hdpi_fac=1., hflip=False, vflip=False):
super().__init__()
self.g_pool = g_pool
self.eyeid = eyeid
Expand All @@ -56,6 +56,7 @@ def __init__(self, g_pool, eyeid, pos, hflip=False, vflip=False):
self.current_eye_frame = None
self.drag_offset = None
self.menu = None
self.hdpi_fac = hdpi_fac

def initliaze_video(self, rec_dir, world_timestamps):
eye_loc = os.path.join(rec_dir, 'eye{}.*'.format(self.eyeid))
Expand Down Expand Up @@ -115,7 +116,8 @@ def visualize(self, frame, alpha, scale, show_ellipses, pupil_positions):

# 2. dragging image
if self.drag_offset is not None:
pos = glfwGetCursorPos(glfwGetCurrentContext())
x, y = glfwGetCursorPos(glfwGetCurrentContext())
pos = x * self.hdpi_fac, y * self.hdpi_fac
pos = normalize(pos, self.g_pool.camera_render_size)
# Position in img pixels
pos = denormalize(pos, (frame.img.shape[1], frame.img.shape[0]))
Expand Down Expand Up @@ -155,11 +157,12 @@ def visualize(self, frame, alpha, scale, show_ellipses, pupil_positions):

transparent_image_overlay(self.pos, eyeimage, frame.img, alpha)

def on_click(self, pos, button, action, scale):
def on_click(self, pos, button, action, hdpi_fac, eye_scale):
self.hdpi_fac = hdpi_fac
if not self.initialized:
return False # click event has not been consumed

video_size = round(self.current_eye_frame.width * scale), round(self.current_eye_frame.height * scale)
video_size = round(self.current_eye_frame.width * eye_scale), round(self.current_eye_frame.height * eye_scale)

if (self.pos[0] < pos[0] < self.pos[0] + video_size[0] and
self.pos[1] < pos[1] < self.pos[1] + video_size[1]):
Expand All @@ -169,6 +172,7 @@ def on_click(self, pos, button, action, scale):
self.drag_offset = None
return False


def getEllipsePts(e, num_pts=10):
c1 = e[0][0]
c2 = e[0][1]
Expand All @@ -191,6 +195,7 @@ def getEllipsePts(e, num_pts=10):

return pts_rot


class Vis_Eye_Video_Overlay(Visualizer_Plugin_Base):
icon_chr = chr(0xec02)
icon_font = 'pupil_icons'
Expand All @@ -205,8 +210,11 @@ def __init__(self, g_pool, alpha=0.6, eye_scale_factor=.5, show_ellipses=True,
self.show_ellipses = show_ellipses
self.move_around = False

self.eye0 = Eye_Wrapper(g_pool, 0, **eye0_config)
self.eye1 = Eye_Wrapper(g_pool, 1, **eye1_config)
window = g_pool.main_window
self.hdpi_factor = float(glfwGetFramebufferSize(window)[0] / glfwGetWindowSize(window)[0])

self.eye0 = Eye_Wrapper(g_pool, 0, hdpi_fac=self.hdpi_factor, **eye0_config)
self.eye1 = Eye_Wrapper(g_pool, 1, hdpi_fac=self.hdpi_factor, **eye1_config)

self.eye0.initliaze_video(g_pool.rec_dir, g_pool.timestamps)
self.eye1.initliaze_video(g_pool.rec_dir, g_pool.timestamps)
Expand Down Expand Up @@ -242,8 +250,8 @@ def on_click(self, pos, button, action):
# eye1 is drawn above eye0. Therefore eye1 gets the priority during click event handling
# wrapper.on_click returns bool indicating the consumption of the click event
if self.move_around and action == 1:
if not self.eye1.on_click(pos, button, action, self.eye_scale_factor):
self.eye0.on_click(pos, button, action, self.eye_scale_factor)
if not self.eye1.on_click(pos, button, action, self.hdpi_factor, self.eye_scale_factor):
self.eye0.on_click(pos, button, action, self.hdpi_factor, self.eye_scale_factor)
else:
self.eye0.drag_offset = None
self.eye1.drag_offset = None
Expand All @@ -263,3 +271,7 @@ def deinit_ui(self):
def get_init_dict(self):
return {'alpha': self.alpha, 'eye_scale_factor': self.eye_scale_factor, 'show_ellipses': self.show_ellipses,
'eye0_config': self.eye0.config, 'eye1_config': self.eye1.config}

def on_window_resize(self, window, camera_render_width, camera_render_height):
self.hdpi_factor = float(glfwGetFramebufferSize(window)[0] / glfwGetWindowSize(window)[0])

0 comments on commit abac88c

Please sign in to comment.