Last active
April 1, 2020 10:42
-
-
Save papr/345fcd2502e86e452377da96fb3c192b to your computer and use it in GitHub Desktop.
Pupil Player plugin to visualize left/right hemianopsia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import enum | |
import cv2 | |
from plugin import Visualizer_Plugin_Base | |
import numpy as np | |
from pyglui import ui | |
from methods import denormalize | |
import logging | |
logger = logging.getLogger(__name__) | |
class ImpairedSide(enum.Enum): | |
left = "left" | |
right = "right" | |
class Vis_Hemianopsia(Visualizer_Plugin_Base): | |
uniqueness = "not_unique" | |
icon_chr = "H" | |
def __init__(self, g_pool, impaired=ImpairedSide.left._value_, visibility=0.8): | |
super().__init__(g_pool) | |
self.order = 0.8 | |
self.menu = None | |
self.impaired = ImpairedSide(impaired) | |
self.visibility = visibility | |
def recent_events(self, events): | |
frame = events.get("frame") | |
if not frame: | |
return | |
img = frame.img | |
pts = [ | |
denormalize(pt["norm_pos"], frame.img.shape[:-1][::-1], flip_y=True) | |
for pt in events.get("gaze", []) | |
if pt["confidence"] >= self.g_pool.min_data_confidence | |
] | |
overlay = np.ones(img.shape[:-1]) | |
# draw recent gaze postions as black dots on an overlay image. | |
for gaze_point in pts: | |
if self.impaired is ImpairedSide.left: | |
impaired_area = slice(None, int(gaze_point[0])) | |
elif self.impaired is ImpairedSide.right: | |
impaired_area = slice(int(gaze_point[0]), None) | |
overlay[:, impaired_area] = 1 - self.visibility | |
img[:] = np.multiply(img, overlay[..., np.newaxis], casting="unsafe") | |
def init_ui(self): | |
self.add_menu() | |
self.menu.label = "Hemianopsia Visualization" | |
self.menu.append( | |
ui.Slider( | |
"visibility", | |
self, | |
min=0.0, | |
max=1.0, | |
label="Degree of impairment [0.0 - 1.0]", | |
) | |
) | |
self.menu.append( | |
ui.Selector( | |
"impaired", | |
self, | |
selection=list(ImpairedSide), | |
labels=[s._name_ for s in ImpairedSide], | |
label="Impaired side", | |
) | |
) | |
def deinit_ui(self): | |
self.remove_menu() | |
def get_init_dict(self): | |
return {"visibility": self.visibility, "impaired": self.impaired._value_} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment