forked from fabiocarrara/meye
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.m
211 lines (157 loc) · 6.54 KB
/
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
%% Download all the example material
%
% 1 - Download the latest MEYE model in ONNX format
% -------------------------------------------------------------------------
% Download the .onnx file from the assets here:
% https://github.com/fabiocarrara/meye/releases
% EXAMPLE data can be found in this folder:
% https://drive.google.com/drive/folders/1BG6O5BEkwXkNKC_1XuB3H9wbx3DeNWwF?usp=sharing
%
% 2 - Download an example image of a simple mouse eye from:
% https://drive.google.com/file/d/1hcWcC1cAmzY4r-SIWDIgUY0-gpbmetUL/view?usp=sharing
%
% 3 - Download an example of a large image here:
% https://drive.google.com/file/d/16QixvUMtojqfrcy4WXlYJ7CP3K8vrz_C/view?usp=sharing
%
% 4 - Download an example pupillometry video here:
% https://drive.google.com/file/d/1TYj80dzIR1ZjpEvfefH_akhbUjwpvJta/view?usp=sharing
%% EXAMPLE 1
% -------------------------------------------------------------------------
% Predict the pupil from a simple image of an eye
% Clean up the workspace
clearvars, clc
% Change these values according to the filenames of the MEYE model and the
% simple pupil image
MODEL_NAME = 'meye_20220124.onnx';
IMAGE_NAME = 'pupilImage_simple.png';
% Initialize a MEYE object
meye = Meye(MODEL_NAME);
% Load the simple image
img = imread(IMAGE_NAME);
% Predict a single image
[pupilMask, eyeProb, blinkProb] = meye.predictImage(img);
% Plot the results of the prediction
subplot(1,3,1)
imshow(img)
title('Original Image')
subplot(1,3,2)
imagesc(pupilMask)
title(sprintf('Prediction (Eye:%.2f%% - Blink:%.2f%%)',eyeProb*100,blinkProb*100))
axis off, axis image
subplot(1,3,3)
imshowpair(img, pupilMask)
title('Merge')
%% EXAMPLE 2
% -------------------------------------------------------------------------
% Binarize the pupil prediction and get the pupil size in pixels
% Clean up the workspace
clearvars, close all, clc
% Change these values according to the filenames of the MEYE model and the
% simple pupil image
MODEL_NAME = 'meye_20220124.onnx';
IMAGE_NAME = 'pupilImage_simple.png';
% Initialize a MEYE object
meye = Meye(MODEL_NAME);
% Load the simple image
img = imread(IMAGE_NAME);
% Predict a single image
% You can automatically binarize the prediction by passing the "threshold"
% optional argument. This number can be between 0 and 1. If omitted, the
% function returns a raw probability map instead of a binarized image
pupilBinaryMask = meye.predictImage(img, 'threshold', 0.4);
imshowpair(img, pupilBinaryMask)
title(sprintf('Pupil Size: %u px', sum(pupilBinaryMask,'all')))
%% EXAMPLE 3
% -------------------------------------------------------------------------
% Predict the pupil on a large image where the eye is a small portion of
% the image
% Clean up the workspace
clearvars, close all, clc
% Change these values according to the filenames of the MEYE model and the
% simple pupil image
MODEL_NAME = 'meye_20220124.onnx';
IMAGE_NAME = 'pupilImage_large.png';
% Initialize a MEYE object
meye = Meye(MODEL_NAME);
% Load the simple image
img = imread(IMAGE_NAME);
% Predict the image
pupilMask = meye.predictImage(img);
% As you can see from this image, the prediction is not perfect. This is
% because MEYE was trained on images that tightly contained the eye.
subplot(1,2,1)
imshowpair(img, pupilMask)
title('Tomal Image prediction (low-quality)')
% In order to solve this issue it is possible to restrict the prediction to
% a rectangular Region of Interest (ROI) in the image. This is done simply
% by passing the optional argument "roiPos" to the predictImage function.
% The roiPos is a 4-elements vector containing X,Y, width, height of a
% rectangular shape. Note that X and Y are the coordinates of the top left
% corner of the ROI
ROI = [90,90,200,200];
pupilMask = meye.predictImage(img, 'roiPos', ROI);
% Plot the results with the ROI and see the difference between the 2 methods
subplot(1,2,2)
imshowpair(img, pupilMask)
rectangle('Position',ROI, 'LineStyle','-.','EdgeColor',[1,0,0])
title('ROI prediction (high quality)')
linkaxes
set(gcf,'Position',[300,600,1000,320])
%% EXAMPLE 4
% -------------------------------------------------------------------------
% Show a preview of the prediction of an entire pupillometry video.
%
% As you saw you can adjust a few parameters for the prediction.
% If you want to get a quick preview of how your pre-recorded video will be
% processed, you can use the method predictMovie_Preview.
% Here you can play around with different ROI positions and threshold
% values and see what are the results before analyzing the whole video.
% Clean up the workspace
clearvars, close all, clc
% Change these values according to the filenames of the MEYE model and the
% simple pupil image
MODEL_NAME = 'meye_20220124.onnx';
VIDEO_NAME = 'mouse_example.mp4';
% Initialize a MEYE object
meye = Meye(MODEL_NAME);
% Try to play around moving or resizing the ROI to see how the performances change
ROI = [70, 60, 200, 200];
% Change the threshold value to binarize the pupil prediction.
% Use [] to see the raw probability map. Use a number in the range [0:1] to binarize it
threshold = 0.4;
meye.predictMovie_Preview(VIDEO_NAME,"roiPos", ROI,"threshold",threshold);
%% EXAMPLE 5
% Predict the entire video and get the results table
% Clean up the workspace
clearvars, close all, clc
% Change these values according to the filenames of the MEYE model and the
% simple pupil image
MODEL_NAME = 'meye_20220124.onnx';
VIDEO_NAME = 'mouse_example.mp4';
% Initialize a MEYE object
meye = Meye(MODEL_NAME);
% Try to play around moving or resizing the ROI to see how the performances change
ROI = [70, 60, 200, 200];
% Change the threshold value to binarize the pupil prediction.
% Use [] to see the raw probability map. Use a number in the range [0:1] to binarize it
threshold = 0.4;
% Predict the whole movie and save results in a table
T = meye.predictMovie(VIDEO_NAME, "roiPos", ROI, "threshold", threshold);
% Show some of the values in the table
disp(head(T))
% Plot some of the results
subplot 311
plot(T.frameTime,T.isEye, 'LineWidth', 2)
title('Eye Probability')
ylabel('Probability'),
xlim([T.frameTime(1) T.frameTime(end)])
subplot 312
plot(T.frameTime,T.isBlink, 'LineWidth', 2)
title('Blink Probability')
ylabel('Probability')
xlim([T.frameTime(1) T.frameTime(end)])
subplot 313
plot(T.frameTime,T.pupilArea, 'LineWidth', 2)
title('Pupil Size')
xlabel('Time (s)'), ylabel('Pupil Area (px)')
xlim([T.frameTime(1) T.frameTime(end)])