-
Notifications
You must be signed in to change notification settings - Fork 0
/
gaze_mapping_example.m
90 lines (71 loc) · 2.85 KB
/
gaze_mapping_example.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
sca; close all; clear all; clc;
try
device = Device();
calibration = device.get_calibration();
gaze_mapper = GazeMapper(calibration);
n_tags = 4;
tag_sz = 64*2;
wborder_sz = 32;
for x = 0:n_tags-1
mpxs = double(MarkerGenerator.generate_marker(x))/255;
mpxs_rs = imresize(mpxs, [tag_sz-wborder_sz*2, tag_sz-wborder_sz*2], 'nearest');
mpxs_fin = ones(tag_sz);
mpxs_fin(wborder_sz:end-(wborder_sz+1), wborder_sz:end-(wborder_sz+1)) = mpxs_rs;
marker_pixels(x+1, :, :) = mpxs_fin;
end
PsychDefaultSetup(2);
Screen('Preference', 'SkipSyncTests', 1);
screens = Screen('Screens');
screenNumber = max(screens);
grey = 0.5;
startXpix = 120;
startYpix = 50;
dimX = 820;
dimY = 820;
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey, [startXpix, startYpix, startXpix+dimX, startYpix+dimY]);
[xCenter, yCenter] = RectCenter(windowRect);
[wszx, wszy] = Screen('WindowSize', window);
screen_size = [wszx, wszy];
marker_verts = [0, 0, tag_sz, tag_sz; ... % Rect for Maker 1
wszx-tag_sz, 0, wszx, tag_sz; ... % Rect for Maker 2
wszx-tag_sz, wszy-tag_sz, wszx, wszy; ... % Rect for Maker 3
0, wszy-tag_sz, tag_sz, wszy]; % Rect for Maker 4
% needs to be the corners of the black square,
% so excluding white border
marker_verts(:, 1) = marker_verts(:, 1) + wborder_sz;
marker_verts(:, 2) = marker_verts(:, 2) + wborder_sz;
marker_verts(:, 3) = marker_verts(:, 3) - wborder_sz;
marker_verts(:, 4) = marker_verts(:, 4) - wborder_sz;
screen_surface = gaze_mapper.add_surface(marker_verts, screen_size);
for x = 1:n_tags
marker_texs(x) = Screen('MakeTexture', window, squeeze(marker_pixels(x, :, :)));
end
baseRect = [0, 0, 100, 100];
result = [];
while true
Screen('FillRect', window, grey);
for x = 1:n_tags
Screen('DrawTexture', window, marker_texs(x), [], marker_verts(x, :));
end
if ~isempty(result)
surface_gazes = result.mapped_gaze{screen_surface.uid};
if ~isempty(surface_gazes)
surface_gaze = surface_gazes{1};
% psychtoolbox coordinates are in pixels
gx = surface_gaze.x * wszx;
gy = (1 - surface_gaze.y) * wszy;
centeredRect = CenterRectOnPointd(baseRect, gx, gy);
Screen('FrameOval', window, [1, 0, 0], centeredRect, 10);
end
end
vbl = Screen('Flip', window);
sc_gz_sample = device.receive_matched_scene_video_frame_and_gaze();
result = gaze_mapper.process_frame(sc_gz_sample);
end
sca;
catch e
disp(['Error: ', e.message]);
sca;
device.close();
clear device;
end