-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathloadAllSubjectsVectors.m
169 lines (140 loc) · 5.58 KB
/
loadAllSubjectsVectors.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
% loadAllSubjectsVectors.m
%
% This script loads subject data into same-sized vectors, with a row for
% each sample. Variables will be placed into your workspace and prefixed
% with "gc." This format is not particularly compact (compared to the
% struct format in loadAllSubjects.m) since many values will be repeated
% for a subject, but you may find this convenient, particularly for
% compatibility with some of the provided scripts. This requires the MATLAB
% gason wrapper to read JSON files. You can get it from
% https://github.com/pdollar/coco/tree/master/MatlabAPI.
%% Don't overwrite workspace variables.
if exist('baseDirectory', 'var') || exist('currSubject', 'var') || ...
exist('input', 'var') || exist('subjectDir', 'var') || ...
exist('subjectDirs', 'var')
error(['A workspace variable in this script will overwrite ' ...
'existing variables.']);
end
%% Configuration.
baseDirectory = '/path/to/data';
%% Initialize variables.
% Subject and frame number are sufficient to reconstruct filenames, which
% we avoid loading into memory.
gcSbjNum = [];
gcFrmNum = [];
% True if face and eyes were detected. Only true samples were used to train
% iTracker.
gcAppleValid = [];
% On-device face and eye detections (using Apple's detectors).
gcAppleFaceX = [];
gcAppleFaceY = [];
gcAppleFaceW = [];
gcAppleFaceH = [];
gcAppleLeftEyeX = [];
gcAppleLeftEyeY = [];
gcAppleLeftEyeW = [];
gcAppleLeftEyeH = [];
gcAppleRightEyeX = [];
gcAppleRightEyeY = [];
gcAppleRightEyeW = [];
gcAppleRightEyeH = [];
% Parameterized face grid as [X Y W H].
gcFaceGridParams = [];
% String describing the device type.
gcDeviceName = {};
% "Active screen area" in points.
gcScreenW = [];
gcScreenH = [];
gcDotNum = [];
gcDotXPts = [];
gcDotYPts = [];
gcDotXCam = [];
gcDotYCam = [];
gcDotStartTime = [];
% 1 = portrait; 2 = portrait upside down; 3 = landscape with home button on
% the right; 4 = landscape with home button on the left.
gcOrientation = [];
% Dataset.
gcTrain = [];
gcVal = [];
gcTest = [];
%% Load from JSON files.
if ~exist(baseDirectory, 'dir')
error(['The specified base directory does not exist. Please edit ' ...
'the script to specify the root of the numbered subject ' ...
'directories.']);
end
subjectDirs = dir(baseDirectory);
for currSubject = subjectDirs'
% Valid subject directories have five-digit numbers.
if ~currSubject.isdir || length(currSubject.name) ~= 5 || ...
~all(isstrprop(currSubject.name, 'digit'))
continue;
end
disp(['Processing subject ' currSubject.name '...'])
subjectDir = fullfile(baseDirectory, currSubject.name);
% Apple Face Detections
input = gason(fileread(fullfile(subjectDir, 'appleFace.json')));
input.X(~input.IsValid) = NaN;
input.Y(~input.IsValid) = NaN;
input.W(~input.IsValid) = NaN;
input.H(~input.IsValid) = NaN;
gcAppleFaceX = [gcAppleFaceX; input.X'];
gcAppleFaceY = [gcAppleFaceY; input.Y'];
gcAppleFaceW = [gcAppleFaceW; input.W'];
gcAppleFaceH = [gcAppleFaceH; input.H'];
% Apple Left Eye Detections
input = gason(fileread(fullfile(subjectDir, 'appleLeftEye.json')));
input.X(~input.IsValid) = NaN;
input.Y(~input.IsValid) = NaN;
input.W(~input.IsValid) = NaN;
input.H(~input.IsValid) = NaN;
gcAppleLeftEyeX = [gcAppleLeftEyeX; input.X'];
gcAppleLeftEyeY = [gcAppleLeftEyeY; input.Y'];
gcAppleLeftEyeW = [gcAppleLeftEyeW; input.W'];
gcAppleLeftEyeH = [gcAppleLeftEyeH; input.H'];
% Apple Right Eye Detections
input = gason(fileread(fullfile(subjectDir, 'appleRightEye.json')));
input.X(~input.IsValid) = NaN;
input.Y(~input.IsValid) = NaN;
input.W(~input.IsValid) = NaN;
input.H(~input.IsValid) = NaN;
gcAppleRightEyeX = [gcAppleRightEyeX; input.X'];
gcAppleRightEyeY = [gcAppleRightEyeY; input.Y'];
gcAppleRightEyeW = [gcAppleRightEyeW; input.W'];
gcAppleRightEyeH = [gcAppleRightEyeH; input.H'];
% Dot Information
input = gason(fileread(fullfile(subjectDir, 'dotInfo.json')));
gcDotNum = [gcDotNum; input.DotNum'];
gcDotXPts = [gcDotXPts; input.XPts'];
gcDotYPts = [gcDotYPts; input.YPts'];
gcDotXCam = [gcDotXCam; input.XCam'];
gcDotYCam = [gcDotYCam; input.YCam'];
gcDotStartTime = [gcDotStartTime; input.Time'];
% Face Grid
input = gason(fileread(fullfile(subjectDir, 'faceGrid.json')));
input.X(~input.IsValid) = NaN;
input.Y(~input.IsValid) = NaN;
input.W(~input.IsValid) = NaN;
input.H(~input.IsValid) = NaN;
gcFaceGridParams = [gcFaceGridParams; ...
[input.X' input.Y' input.W' input.H']];
% Frames
input = gason(fileread(fullfile(subjectDir, 'frames.json')));
gcFrmNum = [gcFrmNum; cellfun(@(x) str2num(x(1:5)), input)'];
% Info
input = gason(fileread(fullfile(subjectDir, 'info.json')));
gcTrain = [gcTrain; repmat(strcmp(input.Dataset, 'train'), input.TotalFrames, 1)];
gcVal = [gcVal; repmat(strcmp(input.Dataset, 'val'), input.TotalFrames, 1)];
gcTest = [gcTest; repmat(strcmp(input.Dataset, 'test'), input.TotalFrames, 1)];
gcDeviceName = [gcDeviceName; repmat({input.DeviceName}, input.TotalFrames, 1)];
gcSbjNum = [gcSbjNum; repmat(str2double(currSubject.name), input.TotalFrames, 1)];
% Motion data omitted. Add it if you need it!
% Screen
input = gason(fileread(fullfile(subjectDir, 'screen.json')));
gcScreenW = [gcScreenW; input.W'];
gcScreenH = [gcScreenH; input.H'];
gcOrientation = [gcOrientation; input.Orientation'];
end
gcAppleValid = ~isnan(gcAppleFaceX) & ~isnan(gcAppleLeftEyeX);
clear baseDirectory currSubject input subjectDir subjectDirs