-
Notifications
You must be signed in to change notification settings - Fork 684
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
280 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
''' | ||
(*)~---------------------------------------------------------------------------------- | ||
Pupil - eye tracking platform | ||
Copyright (C) 2012-2013 Moritz Kassner & William Patera | ||
Distributed under the terms of the CC BY-NC-SA License. | ||
License details are in the file license.txt, distributed as part of this software. | ||
----------------------------------------------------------------------------------~(*) | ||
''' | ||
|
||
import cv2 | ||
from plugin import Plugin | ||
import numpy as np | ||
import atb | ||
from methods import denormalize,normalize | ||
import logging | ||
logger = logging.getLogger(__name__) | ||
|
||
class Scan_Path(Plugin): | ||
"""docstring | ||
using this plugin will extend the recent_pupil_positions by x extra dots from previous frames. | ||
lock recent gaze points onto pixels. | ||
""" | ||
|
||
def __init__(self, g_pool): | ||
super(Scan_Path, self).__init__() | ||
self.g_pool = g_pool | ||
|
||
#let the plugin work after most other plugins. | ||
self.order = .6 | ||
|
||
|
||
#user settings | ||
self.scan_path_timeframe = 3. | ||
|
||
#algorithm working data | ||
self.prev_frame_idx = -1 | ||
self.past_pupil_positions = [] | ||
self.prev_gray = None | ||
|
||
|
||
def update(self,frame,recent_pupil_positions,events): | ||
img = frame.img | ||
img_shape = img.shape[:-1][::-1] # width,height | ||
|
||
succeeding_frame = frame.index-self.prev_frame_idx == 1 | ||
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | ||
|
||
|
||
#vars for calcOpticalFlowPyrLK | ||
lk_params = dict( winSize = (90, 90), | ||
maxLevel = 2, | ||
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 20, 0.03)) | ||
|
||
|
||
updated_past_gaze = [] | ||
|
||
#lets update past gaze using optical flow: this is like sticking the gaze points onto the pixels of the img. | ||
if self.past_pupil_positions and succeeding_frame: | ||
past_screen_gaze = np.array([denormalize(ng['norm_gaze'] ,img_shape,flip_y=True) for ng in self.past_pupil_positions],dtype=np.float32) | ||
new_pts, status, err = cv2.calcOpticalFlowPyrLK(self.prev_gray, gray_img,past_screen_gaze,minEigThreshold=0.01,**lk_params) | ||
|
||
for gaze,new_gaze_pt,s,e in zip(self.past_pupil_positions,new_pts,status,err): | ||
if s: | ||
# print "norm,updated",gaze['norm_gaze'], normalize(new_gaze_pt,img_shape[:-1],flip_y=True) | ||
gaze['norm_gaze'] = normalize(new_gaze_pt,img_shape,flip_y=True) | ||
updated_past_gaze.append(gaze) | ||
# logger.debug("updated gaze") | ||
|
||
else: | ||
# logger.debug("dropping gaze") | ||
# Since we will replace self.past_pupil_positions later, | ||
# not appedning tu updated_past_gaze is like deliting this data point. | ||
pass | ||
|
||
|
||
# print "new_gaze", len(recent_pupil_positions) | ||
# print "from before", len(updated_past_gaze) | ||
|
||
# trim of gaze that is too old | ||
if recent_pupil_positions: | ||
now = recent_pupil_positions[0]['timestamp'] | ||
cutof = now-self.scan_path_timeframe | ||
updated_past_gaze = [g for g in updated_past_gaze if g['timestamp']>cutof] | ||
|
||
|
||
#inject the scan path gaze points into recent_pupil_positions | ||
recent_pupil_positions[:] = updated_past_gaze + recent_pupil_positions | ||
recent_pupil_positions.sort(key=lambda x: x['timestamp']) #this may be redundant... | ||
|
||
|
||
#update info for next frame. | ||
self.prev_gray = gray_img | ||
self.prev_frame_idx = frame.index | ||
self.past_pupil_positions = recent_pupil_positions | ||
|
||
|
||
def gl_display(self): | ||
pass | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
''' | ||
(*)~---------------------------------------------------------------------------------- | ||
Pupil - eye tracking platform | ||
Copyright (C) 2012-2013 Moritz Kassner & William Patera | ||
Distributed under the terms of the CC BY-NC-SA License. | ||
License details are in the file license.txt, distributed as part of this software. | ||
----------------------------------------------------------------------------------~(*) | ||
''' | ||
|
||
from gl_utils import draw_gl_points_norm | ||
from player_methods import transparent_cirlce | ||
from plugin import Plugin | ||
import numpy as np | ||
|
||
import cv2 | ||
|
||
from methods import denormalize | ||
|
||
class Vis_Circle(Plugin): | ||
"""docstring for DisplayGaze""" | ||
def __init__(self, g_pool): | ||
super(Vis_Circle, self).__init__() | ||
self.g_pool = g_pool | ||
self.order = .9 | ||
self.prev_frame_idx = -1 | ||
self.radius = 20 | ||
|
||
def update(self,frame,recent_pupil_positions,events): | ||
if self.prev_frame_idx != frame.index: | ||
pts = [denormalize(pt['norm_gaze'],frame.img.shape[:-1][::-1],flip_y=True) for pt in recent_pupil_positions if pt['norm_gaze'] is not None] | ||
for pt in pts: | ||
transparent_cirlce(frame.img, tuple(map(int,pt)), radius=self.radius, color=(0,255,0,100), thickness=3) | ||
self.prev_frame_idx = frame.index | ||
|
||
|
||
def gl_display(self): | ||
pass |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
''' | ||
(*)~---------------------------------------------------------------------------------- | ||
Pupil - eye tracking platform | ||
Copyright (C) 2012-2013 Moritz Kassner & William Patera | ||
Distributed under the terms of the CC BY-NC-SA License. | ||
License details are in the file license.txt, distributed as part of this software. | ||
----------------------------------------------------------------------------------~(*) | ||
''' | ||
|
||
import cv2 | ||
from plugin import Plugin | ||
import numpy as np | ||
import atb | ||
from methods import denormalize | ||
import logging | ||
logger = logging.getLogger(__name__) | ||
|
||
class Vis_Light_Points(Plugin): | ||
"""docstring | ||
show gaze dots at light dots on numpy. | ||
""" | ||
#let the plugin work after most other plugins. | ||
|
||
def __init__(self, g_pool): | ||
super(Vis_Light_Points, self).__init__() | ||
self.g_pool = g_pool | ||
|
||
self.order = .8 | ||
|
||
self.prev_frame_idx = -1 | ||
|
||
def update(self,frame,recent_pupil_positions,events): | ||
|
||
#since we edit the img inplace we should not do it in pause mode... | ||
if self.prev_frame_idx != frame.index: | ||
img = frame.img | ||
img_shape = img.shape[:-1][::-1]#width,height | ||
norm_gaze = [ng['norm_gaze'] for ng in recent_pupil_positions if ng['norm_gaze'] is not None] | ||
screen_gaze = [denormalize(ng,img_shape,flip_y=True) for ng in norm_gaze] | ||
|
||
|
||
overlay = np.ones(img.shape[:-1],dtype=img.dtype) | ||
|
||
# draw recent gaze postions as black dots on an overlay image. | ||
for gaze_point in screen_gaze: | ||
try: | ||
overlay[int(gaze_point[1]),int(gaze_point[0])] = 0 | ||
except: | ||
pass | ||
|
||
out = cv2.distanceTransform(overlay,cv2.cv.CV_DIST_L2, 5) | ||
|
||
# fix for opencv binding incositency | ||
if type(out)==tuple: | ||
out = out[0] | ||
|
||
overlay = 1/(out/20+1) | ||
|
||
img *= cv2.cvtColor(overlay,cv2.COLOR_GRAY2RGB) | ||
|
||
self.prev_frame_idx = frame.index | ||
|
||
|
||
|
||
def gl_display(self): | ||
pass | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
''' | ||
(*)~---------------------------------------------------------------------------------- | ||
Pupil - eye tracking platform | ||
Copyright (C) 2012-2013 Moritz Kassner & William Patera | ||
Distributed under the terms of the CC BY-NC-SA License. | ||
License details are in the file license.txt, distributed as part of this software. | ||
----------------------------------------------------------------------------------~(*) | ||
''' | ||
|
||
from gl_utils import draw_gl_points_norm | ||
from plugin import Plugin | ||
import numpy as np | ||
|
||
import cv2 | ||
|
||
from methods import denormalize | ||
|
||
class Vis_Polyline(Plugin): | ||
"""docstring for DisplayGaze""" | ||
def __init__(self, g_pool): | ||
super(Vis_Polyline, self).__init__() | ||
self.g_pool = g_pool | ||
self.order = .9 | ||
self.prev_frame_idx = -1 | ||
|
||
def update(self,frame,recent_pupil_positions,events): | ||
if self.prev_frame_idx != frame.index: | ||
pts = [denormalize(pt['norm_gaze'],frame.img.shape[:-1][::-1],flip_y=True) for pt in recent_pupil_positions if pt['norm_gaze'] is not None] | ||
if pts: | ||
pts = np.array([pts],dtype=np.int32) | ||
cv2.polylines(frame.img, pts, isClosed=False, color=(0,255,0), thickness=1, lineType=cv2.cv.CV_AA) | ||
|
||
self.prev_frame_idx = frame.index | ||
|
||
|
||
def gl_display(self): | ||
pass |
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