-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtrack.m
279 lines (238 loc) · 8.63 KB
/
track.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
function data = track( model , video , params )
% - Enrique Sanchez-Lozano. University of Nottingham
% - Code released as is for research purposes only.
% - Feel free to modify or redistribute.
% - Should you use the code, please cite
% - [1] E.Sanchez-Lozano, G. Tzimiropoulos, B. Martinez, F. De la Torre, M.
% Valstar. A Functional Regression approach to facial landmark tracking.
% IEEE TPAMI. 2017
% - [2] E.Sanchez-Lozano, B.Martinez, G.Tzimiropoulos, M.Valstar. Cascaded
% Continuous Regression for Real-time Incremental Face Tracking. ECCV 2016.
% - Feel free to contact me at [email protected]
% - the video string should be either [] for webcam, or the path to the
% video
% - params should contain the following components
% - params.show = 1 or 0 (show results or not)
% - params.incLearning = 1 or 0 (iCCR or simply CCR)
% - params.lambda = Incremental Learning update factor
% - params.error_fun = handle to function that will return whether the
% tracker is lost or not.
% - params.lost_model = the model that should be used by the error_fun
if nargin < 3
% default params
params.show = 1;
params.incLearning = 1;
params.lambda = 0.1 * [0.1 0.25 0.5 1];
end;
i = 1;
if ~isfield( params , 'error_fun' )
params.error_fun = @detectLost_SVM;
params.lost_model.SVM = model.SVM;
params.lost_model.shModel = model.shModel;
end
if isempty(video)
obj = webcam(1);
obj.Resolution = '640x480';
%obj.Resolution = '2304x1536';
else
obj = VideoReader(video);
end;
global H %- the drawing scenario has been kindly adapted from the xx_track
%%% Initialise the data and the model given the input object
[ model , data ] = ccr_initialise( model , obj , video );
np = size( model.shModel.s0 , 1 );
incLearning = params.incLearning;
pts = [];
keep = 1; first = 1;
while keep
tic
% - grab a frame
if ~isempty(video)
im = ( read( obj , i ) );
else
im = snapshot( obj );
end;
if isfield(params,'transform')
im = imrotate(im, params.transform.pm);
im = imresize( im , params.transform.sc );
if params.transform.flip
im = im(:,end:-1:1,:);
end
end
% im = im(:,end:-1:1,:);
imcpy = im;
% - track points
% - usage: [ pts , img ] = ccr_track( im , pts , model );
box = zeros(np,1);
if isempty( pts )
% - detect
pts = detect_pts_SDM( im , model );
%shape66( [ 61,65 ], :, : ) = [];
if np == 68 && size( pts , 1 ) == 66
pts = [pts(1:60,:); zeros(1,2); pts(61:63,:); zeros(1,2); pts(64:66,:)];
end
if ~isempty( pts )
% - apply a tracking step
[ imcrop , ptscrop , box ] = cropImage( im , pts );
if size( imcrop , 1 ) * size( imcrop , 2 ) > 0
im = imcrop; pts = ptscrop;
else
box = [0 0];
end
[ pts , img ] = ccr_track( im , pts , model );
end
else
% - track the points
[ imcrop , ptscrop , box ] = cropImage( im , pts );
if size( imcrop , 1 ) * size( imcrop , 2 ) > 0
im = imcrop; pts = ptscrop;
else
box = [0 0];
end
[ pts , img ] = ccr_track( im , pts , model );
% - detect whether the result is OK or not
% - here we use the same distance both to detect a lost and to estimate the
% suitability of a frame to update the model's tracker
data.distance(i) = detect_lost( img , pts , params );
threshold = ccr_obtain_threshold( params , pts , model );
if data.distance(i) < threshold
% - lost frame, try to detect the face again
if ispc
pts = detect_pts_SDM( im , model );
else
pts = detect_points_PRL_Linux( im , model.cbSDM_model );
end
if np == 68 && size( pts , 1 ) == 66
pts = [pts(1:60,:); zeros(1,2); pts(61:63,:); zeros(1,2); pts(64:66,:)];
end
if ~isempty( pts )
[ pts , img ] = ccr_track( im , pts , model );
end
fprintf( 'Tracker Lost\n');
end
end
%%%%% FURTHER PROCESSING HERE
% if ~isempty( pts )
% [ rec, imgout ] = registerandwarp(im2double(im), double(pts), model.shModel , [256 256] , 0 );
% end
% - update the data with the statistics obtained through the given
% frames
if ~isempty( pts )
[ data , model ] = ccr_update_data( pts + ones(np,1)*box , model , data , i );
else
data.lost(i) = 1;
if i == 1
data.tracked_pts(:,:,i) = -1 * ones( np , 2 );
else
data.tracked_pts(:,:,i) = data.tracked_pts(:,:,i-1);
end
end
if incLearning
% - check suitability
[suitable,data.counter] = policy_svm( data.distance , i , data.counter );
if suitable
[ model , data ] = iccr_update( img , pts , model , data , i, params.lambda );
end
end
te = toc;
if isempty(pts);
output.pred = [];
else
output.pred = 1 * data.tracked_pts(:,:,i);
end;
if params.show && first
first = false;
frame_w = size(imcpy,2);
draw_figure( size(imcpy,2) , size( imcpy, 1 ) );
set(S.im_h,'cdata',imcpy);
% drawow;
end
if params.show
set(S.im_h,'cdata',imcpy);
if isempty(output.pred) % if lost/no face, delete all drawings
if drawed, delete_handlers(); end
else % else update all drawings
update_GUI();
end
end
drawnow;
if ~isempty( pts )
pts = pts + ones(np,1)*box;
data.tracked_pts(:,:,i) = pts;
end
i = i + 1;
if ~isempty(video)
if i > obj.NumberOfFrames
keep = 0;
end;
end
if params.show
if ~ishandle( S.fh )
data.tracked_pts = data.tracked_pts(:,:,1:i-1);
data.lost = data.lost(1:i-1);
data.distance = data.distance(1:i-1);
return;
end;
end
end;
if params.show
close;
end
function draw_figure( frame_w , frame_h )
% 500 -50
S.fh = figure('units','pixels',...
'position',[500 50 frame_w frame_h],...
'menubar','none',...
'name','CONTINUOUS REGRESSION',...
'numbertitle','off',...
'resize','off',...
'renderer','painters');
% create axes
S.ax = axes('units','pixels',...
'position',[1 1 frame_w frame_h],...
'drawmode','fast');
drawed = false; % nor draw anything yet
output.pred = []; % prediction set to null enabling detection
S.im_h = imshow(zeros(1*frame_h,1*frame_w,3,'uint8'));
hold on;
if incLearning
H = uicontrol('Style', 'PushButton', ...
'String', 'Deactivate IL', ...
'Callback', @(src,evnt)change_String, ...
'Position',[ 10 10 100 20 ]);
else
H = uicontrol('Style', 'PushButton', ...
'String', 'Activate IL', ...
'Callback', @(src,evnt)change_String, ...
'Position',[ 10 10 100 20 ]);
end;
end
function delete_handlers()
delete(S.pts_h); delete(S.time_h);
%delete(S.hh{1}); delete(S.hh{2}); delete(S.hh{3});
drawed = false;
end
function update_GUI()
if drawed % faster to update than to creat new drawings
set(S.pts_h, 'xdata', output.pred(:,1), 'ydata',output.pred(:,2));
% update frame/second
set(S.time_h, 'string', sprintf('%d FPS',uint8(1/te)));
else
% create tracked points drawing
% S.pts_h = plot(output.pred(:,1), output.pred(:,2), 'g*', 'markersize',2);
S.pts_h = plot(output.pred(:,1), output.pred(:,2), 'r.', 'markersize',16);
% create frame/second drawing
S.time_h = text(frame_w-100,50,sprintf('%d FPS',uint8(1/te)),'fontsize',20,'color','m');
drawed = true;
end
end
function change_String()
StrVal = get(H,'String');
if strcmp( StrVal, 'Activate IL')
set(H,'String','Deactivate IL');
else
set(H,'String','Activate IL');
end;
incLearning = not(incLearning);
end
end