Created
November 13, 2020 14:00
-
-
Save papr/b938ddc6315525d0f03da3668568e75c to your computer and use it in GitHub Desktop.
Example pupil detector plugin that extends the default 2D detector. Requires Pupil v2.6 or higher. Install instructions: https://docs.pupil-labs.com/developer/core/plugin-api/#adding-a-plugin Author: @romanroibu
This file contains 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 logging | |
from pupil_detectors import Detector2D, DetectorBase, Roi | |
from pyglui import ui | |
from methods import normalize | |
from pupil_detector_plugins import available_detector_plugins | |
from pupil_detector_plugins.detector_base_plugin import ( | |
PropertyProxy, | |
PupilDetectorPlugin, | |
) | |
from pupil_detector_plugins.visualizer_2d import draw_pupil_outline | |
logger = logging.getLogger(__name__) | |
class CustomDetector2DPlugin(PupilDetectorPlugin): | |
uniqueness = "by_class" | |
icon_font = "pupil_icons" | |
icon_chr = chr(0xEC18) | |
label = "Custom 2d detector" | |
# Use the same identifier as the built-in 2D pupil detector | |
identifier = "2d" | |
order = 0.9 | |
@property | |
def pretty_class_name(self): | |
return "Custom Pupil Detector 2D" | |
@property | |
def pupil_detector(self) -> DetectorBase: | |
return self.__detector_2d | |
def __init__(self, g_pool=None): | |
super().__init__(g_pool=g_pool) | |
self.__detector_2d = Detector2D({}) | |
self._stop_other_pupil_detectors() | |
def _stop_other_pupil_detectors(self): | |
plugin_list = self.g_pool.plugins | |
# Deactivate other PupilDetectorPlugin instances | |
for plugin in plugin_list: | |
if isinstance(plugin, PupilDetectorPlugin) and plugin is not self: | |
plugin.alive = False | |
# Force Plugin_List to remove deactivated plugins | |
plugin_list.clean() | |
def detect(self, frame, **kwargs): | |
debug_img = frame.bgr if self.g_pool.display_mode == "algorithm" else None | |
result = self.__detector_2d.detect( | |
gray_img=frame.gray, | |
color_img=debug_img, | |
roi=Roi(*self.g_pool.roi.bounds), | |
) | |
eye_id = self.g_pool.eye_id | |
result["id"] = eye_id | |
result["topic"] = f"pupil.{eye_id}.{self.identifier}" | |
result["method"] = "custom-2d-c++" | |
result["timestamp"] = frame.timestamp | |
result["norm_pos"] = normalize( | |
result["location"], (frame.width, frame.height), flip_y=True | |
) | |
return result | |
def init_ui(self): | |
super().init_ui() | |
self.menu.label = self.pretty_class_name | |
self.menu_icon.label_font = "pupil_icons" | |
info = ui.Info_Text("Custom 2D Pupil Detector Plugin") | |
self.menu.append(info) | |
def gl_display(self): | |
if self._recent_detection_result: | |
draw_pupil_outline(self._recent_detection_result, color_rgb=(0.3, 1.0, 0.1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment