-
Notifications
You must be signed in to change notification settings - Fork 26
/
peopleTrackerPDAFOnLegTracks.m
387 lines (331 loc) · 16.2 KB
/
peopleTrackerPDAFOnLegTracks.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
function [ legTrackOnPeople, tracker] = peopleTrackerPDAFOnLegTracks( candidates, candidatesTrack, tracker )
%PEOPLETRACKERPDAFONLEGTRACKS People tracker over the candidate tracks obtained
% by candidatesTrackerPDAF.
%
% Inputs:
% candidates = Nx1 vector of candidate structs as obtained by getCandidateLegs
% candidatesTrack = Nx1 vector as obtained by candidatesTrackerPDAF.
% It contains the index of single leg track to which each
% candidate has been associated.
% tracker = tracker object containing the state of the single candidate tracker
% and of this people tracker.
%
% Outputs:
% legTrackOnPeople = Mx1 vector containing for each leg track the index
% of the person to which it as been associated, or 0 if
% no association was possible
% tracker = updated tracker object
%--- PARAMETERS ---
windowSizeSecond = 1; % avgSpeed in the last windowSizeSecond
maxFps = 30;
NUM_FRAMES_NOT_SEEN = 10; % maximum life of a not updated people track
thresholdDistToOtherCentroid = 450; % mm
thresholdDist2LegsNewPeople = 100; % mm
thresholdDist2LegsAssociation = 800; %mm
thresholdDistStraightAssociation = tracker.peopleDistThreshold;%50; %mm
thresholdDistTooClosePeople = 50; % mm
thresholdDistOverlappingCandidates = 50; %mm
probabilityThresholdForAssociation = tracker.legProbabilityThreshold;%0.5;
probabilityDeviationThresholdForAssociation = 0.15;
radiusPDAF = 400; %mm
thresholdStillPerson = 0.05^2; % m/s
%------------------
legTracks = tracker.legTracks;
% exclude from further processing leg tracks with low leg probability
if ~isempty(legTracks)
thresholdFilterLegs = 0.2;
predictions = cat(2,legTracks.prediction);
states = cat(2,predictions.x);
probabilities = (states(5,:)+1)/2;
idxTracksToKill = find(probabilities<thresholdFilterLegs);
idxTracksToSave = find(probabilities>=thresholdFilterLegs);
idxCandidatesToKill = [];
for i=1:length(candidatesTrack)
if ismember(candidatesTrack(i), idxTracksToKill)
idxCandidatesToKill = [idxCandidatesToKill i];
else
candidatesTrack(i) = candidatesTrack(i) - nnz(idxTracksToKill < candidatesTrack(i));
end
end
legTracks(idxTracksToKill) = [];
candidates(idxCandidatesToKill) = [];
candidatesTrack(idxCandidatesToKill) = [];
end
% read parameters from tracker
peopleTracks = tracker.peopleTracks;
freeID = tracker.peopleFreeID;
sigmaZ = tracker.peopleSigmaZ;
sigmaP = tracker.peopleSigmaP;
sigmaAcc = tracker.peopleSigmaAcc;
% compute time elapsed from the previous update
DT = tracker.currentTimestamp - tracker.oldTimestamp;
legTrackOnPeople = zeros(length(legTracks),1);
% update avg speed of each track
windowLength = ceil(maxFps*windowSizeSecond);
shiftVal = min(ceil(DT*maxFps), windowLength);
for i = 1:length(peopleTracks)
peopleTracks(i).vecSpeed(:,shiftVal+1:end) = peopleTracks(i).vecSpeed(:,1:end-shiftVal);
vx = peopleTracks(i).prediction.x(3);
vy = peopleTracks(i).prediction.x(4);
peopleTracks(i).vecSpeed(:,1:shiftVal) = repmat([vx;vy],1,shiftVal);
peopleTracks(i).avgSpeed = mean( peopleTracks(i).vecSpeed,2);
if sum(peopleTracks(i).avgSpeed.^2) > thresholdStillPerson
peopleTracks(i).heading = atan2(peopleTracks(i).avgSpeed(2), peopleTracks(i).avgSpeed(1));
end
end
idxToKill = [];
% predict step of Kalman Filter and delete people tracks not seen for NUM_FRAMES_NOT_SEEN times
for i = 1:length(peopleTracks)
peopleTracks(i).prediction = KFpredict(peopleTracks(i).prediction, sigmaAcc, DT);
peopleTracks(i).lastSeen = peopleTracks(i).lastSeen + 1;
if peopleTracks(i).lastSeen > NUM_FRAMES_NOT_SEEN
% remove track
idxToKill = [idxToKill i];
end
end
peopleTracks(idxToKill) = [];
% check if two people tracks are too close and delete the older one
if ~isempty(peopleTracks)
predictions = cat(1,peopleTracks.prediction);
states = cat(2, predictions.x);
peopleTracksCentroid = states(1:2,:)'.* 1000; % mm
peopleDistances = pdist2(peopleTracksCentroid,peopleTracksCentroid);
peopleDistances = peopleDistances + diag(Inf.*ones(length(peopleTracks),1));
[minVals, minTracks2ID] = min(peopleDistances,[],2);
[minVal, track1] = min(minVals);
while ~isempty(peopleDistances) && minVal < thresholdDistTooClosePeople
track2 = minTracks2ID(track1);
if peopleTracks(track1).lastSeen <= peopleTracks(track2).lastSeen
trackToKill = track2;
else
trackToKill = track1;
end
peopleDistances(trackToKill,:) = [];
peopleDistances(:,trackToKill) = [];
peopleTracks(trackToKill) = [];
peopleTracksCentroid(trackToKill,:) = [];
[minVals, minTracks2ID] = min(peopleDistances,[],2);
[minVal, track1] = min(minVals);
end
end
if isempty(legTracks)
% no leg tracks
legTrackOnPeople = zeros(length(tracker.legTracks),1);
tracker.peopleTracks = peopleTracks;
tracker.peopleFreeID = freeID;
return;
end
predictions = cat(1,legTracks.prediction);
states = cat(2, predictions.x);
variances = cat(2,predictions.P);
legCentroids = states(1:2,:)' .* 1000; % mm
probEstimates = states(5,:)';
normProbEstimates = (probEstimates+1)./2;
probEstimateDeviations = sqrt(variances(5,5:5:end))'./2;
availableLegTracks = 1:length(legTracks);
% compute feasible associatons between leg tracks
if length(legTracks) > 1
possibleLegsAssociation = nchoosek(1:length(legTracks),2);
% remove too far associated leg Tracks
idxToKill = find(sum( (legCentroids(possibleLegsAssociation(:,1),:)-legCentroids(possibleLegsAssociation(:,2),:)).^2 ,2) > thresholdDist2LegsAssociation^2 );
possibleLegsAssociation(idxToKill,:) = [];
% remove more legs association containing already straight associated legs
visitedAssociations = zeros(size(possibleLegsAssociation,1),1);
while ~isempty(visitedAssociations) && nnz(~visitedAssociations) > 0
i = find(visitedAssociations==0,1,'first');
visitedAssociations(i) = 1;
legTrack1 = possibleLegsAssociation(i,1);
legTrack2 = possibleLegsAssociation(i,2);
idCandidate1 = find(candidatesTrack == legTrack1,1,'first');
idCandidate2 = find(candidatesTrack == legTrack2,1,'first');
if ~isempty(idCandidate1) && ~isempty(idCandidate2)
mindist = minDist(candidates(idCandidate1).allPoints, candidates(idCandidate2).allPoints, candidates(idCandidate1).centroid, candidates(idCandidate2).centroid, thresholdDistOverlappingCandidates);
if mindist <= thresholdDistOverlappingCandidates
% straight association of two legs to a person
idxToKill = find(possibleLegsAssociation(:,1)==legTrack1 | possibleLegsAssociation(:,1)==legTrack2 | possibleLegsAssociation(:,2)==legTrack1 | possibleLegsAssociation(:,2)==legTrack2);
idxToKill = setdiff(idxToKill,i);
possibleLegsAssociation(idxToKill,:) = [];
visitedAssociations(idxToKill) = [];
end
end
end
allPossiblePeopleCentroids = (legCentroids(possibleLegsAssociation(:,1),:) + legCentroids(possibleLegsAssociation(:,2),:))./2;
allPossiblePeopleProbEstimates = (probEstimates(possibleLegsAssociation(:,1),:) + probEstimates(possibleLegsAssociation(:,2),:))./2;
else
possibleLegsAssociation = [];
allPossiblePeopleCentroids = [];
allPossiblePeopleProbEstimates = [];
end
if isempty(peopleTracks)
peopleTracksCentroid = [];
end
updatedPeopleTrack = zeros(length(peopleTracks),1);
if ~isempty(peopleTracks) && ~isempty(possibleLegsAssociation)
distanceCentroidToCentroid = pdist2(allPossiblePeopleCentroids, peopleTracksCentroid);
[minVals, minPeopleTrackIDs] = min(distanceCentroidToCentroid,[],2);
[minVal, minLegsAssociationID] = min(minVals);
minPeopleTrackID = minPeopleTrackIDs(minLegsAssociationID);
else
distanceCentroidToCentroid = [];
minVal = Inf;
end
% update straight associated people tracks
while ~isempty(distanceCentroidToCentroid) && minVal < thresholdDistStraightAssociation
if updatedPeopleTrack(minPeopleTrackID) == 1
% people track already updated
associationsToKill = minLegsAssociationID;
else
% people track straight associated
legTrack1 = possibleLegsAssociation(minLegsAssociationID,1);
legTrack2 = possibleLegsAssociation(minLegsAssociationID,2);
availableLegTracks([legTrack1,legTrack2]) = 0;
associationsToKill = find(possibleLegsAssociation(:,1)==legTrack1 | possibleLegsAssociation(:,1)==legTrack2 | possibleLegsAssociation(:,2)==legTrack1 | possibleLegsAssociation(:,2)==legTrack2);
end
% remove used leg tracks
possibleLegsAssociation(associationsToKill,:) = [];
distanceCentroidToCentroid(associationsToKill,:) = [];
allPossiblePeopleCentroids(associationsToKill,:) = [];
allPossiblePeopleProbEstimates(associationsToKill,:) = [];
if updatedPeopleTrack(minPeopleTrackID) == 1
% people track already updated
[minVals, minPeopleTrackIDs] = min(distanceCentroidToCentroid,[],2);
[minVal, minLegsAssociationID] = min(minVals);
minPeopleTrackID = minPeopleTrackIDs(minLegsAssociationID);
continue;
end
% straight update
centroid = (legCentroids(legTrack1,:) + legCentroids(legTrack2,:))./2;
legProb = (probEstimates(legTrack1) + probEstimates(legTrack2))./2;
measures = [centroid legProb];
[peopleTracks(minPeopleTrackID).prediction, inGate, createNewTrack] = PDAFupdate(peopleTracks(minPeopleTrackID).prediction, measures, sigmaZ,sigmaP);
if inGate
peopleTracks(minPeopleTrackID).lastSeen = 0;
end
updatedPeopleTrack(minPeopleTrackID) = 1;
% association leg-people
legTrackOnPeople(legTrack1) = minPeopleTrackID;
legTrackOnPeople(legTrack2) = minPeopleTrackID;
[minVals, minPeopleTrackIDs] = min(distanceCentroidToCentroid,[],2);
[minVal, minLegsAssociationID] = min(minVals);
minPeopleTrackID = minPeopleTrackIDs(minLegsAssociationID);
end
%remove associations with low leg probability
idxLegTracksToKill = find(normProbEstimates<probabilityThresholdForAssociation); %| probEstimateDeviations>probabilityDeviationThresholdForAssociation);
availableLegTracks(idxLegTracksToKill) = 0;
if ~isempty(possibleLegsAssociation)
idxToKill = find(ismember(possibleLegsAssociation(:,1),idxLegTracksToKill) | ismember(possibleLegsAssociation(:,2),idxLegTracksToKill));
possibleLegsAssociation(idxToKill,:) = [];
allPossiblePeopleCentroids(idxToKill,:) = [];
allPossiblePeopleProbEstimates(idxToKill,:) = [];
end
% update all remaining people Tracks, for which a precise association has
% not been found, using also single leg tracks
numRemainingAssociations = size(possibleLegsAssociation,1);
% retrieve robot odometry for computing absolute measures
yaw = tracker.pose(3);
cyaw = cos(yaw);
syaw = sin(yaw);
measures = [allPossiblePeopleCentroids, allPossiblePeopleProbEstimates];
auxPossibileAssociation = possibleLegsAssociation;
for i=1:length(availableLegTracks)
if availableLegTracks(i) ~= 0
idCandidate = find(candidatesTrack == i,1,'first');
if isempty(idCandidate)
measures = [measures; legCentroids(i,:), probEstimates(i)];
else
measure = ([cyaw -syaw; syaw cyaw]*mean(candidates(idCandidate).legPoints(1:2,:),2) + [tracker.pose(1); tracker.pose(2)].*1000)';
measures = [measures; measure, probEstimates(i)];
end
auxPossibileAssociation = [auxPossibileAssociation; availableLegTracks(i) availableLegTracks(i)];
end
end
notUsedIdx = 1:numRemainingAssociations;
% PDAF update of not straight associated people
for i=1:length(peopleTracks)
if ~isempty(updatedPeopleTrack) && ~updatedPeopleTrack(i) && ~isempty(measures)
measuresIdx = find((measures(:,1) - peopleTracks(i).prediction.x(1).*1000).^2 + (measures(:,2) - peopleTracks(i).prediction.x(2).*1000).^2 < radiusPDAF^2);
[peopleTracks(i).prediction, inGate, createNewTrack] = PDAFupdate(peopleTracks(i).prediction, measures(measuresIdx,:), sigmaZ,sigmaP, true);
if nnz(inGate)>0
peopleTracks(i).lastSeen = max(0,peopleTracks(i).lastSeen - 1);
legTrackIdx = unique(auxPossibileAssociation(measuresIdx(inGate==1),:));
legTrackOnPeople(legTrackIdx) = i;
end
updatedPeopleTrack(i) = 1;
notUsedIdx = setdiff(notUsedIdx, measuresIdx(measuresIdx <= numRemainingAssociations));
end
end
if isempty(notUsedIdx)
% no new track to create
legTrackOnPeopleOld = legTrackOnPeople;
legTrackOnPeople = zeros(length(tracker.legTracks),1);
legTrackOnPeople(idxTracksToSave) = legTrackOnPeopleOld;
tracker.peopleTracks = peopleTracks;
tracker.peopleFreeID = freeID;
return;
end
% new People track creation
possibleNewPeopleCentroids = allPossiblePeopleCentroids(notUsedIdx,:);
possibleNewPeopleProbEstimates = allPossiblePeopleProbEstimates(notUsedIdx,:);
possibleNewPeopleLegsAssociation = possibleLegsAssociation(notUsedIdx,:);
possibleNewPeopleLegDistances = zeros(size(possibleNewPeopleLegsAssociation,1),1);
for i=1:size(possibleNewPeopleLegsAssociation,1)
idxCandidate1 = find(possibleNewPeopleLegsAssociation(i,1) == candidatesTrack);
idxCandidate2 = find(possibleNewPeopleLegsAssociation(i,2) == candidatesTrack);
if length(idxCandidate1) ~=1 || length(idxCandidate2) ~=1
possibleNewPeopleLegDistances(i) = Inf;
else
candidate1 = candidates(idxCandidate1);
candidate2 = candidates(idxCandidate2);
possibleNewPeopleLegDistances(i) = minDist(candidate1.allPoints,candidate2.allPoints,candidate1.centroid,candidate2.centroid,thresholdDist2LegsNewPeople);
end
end
% exclude too far apart legs
idxToKill = find(possibleNewPeopleLegDistances > thresholdDist2LegsNewPeople);
possibleNewPeopleCentroids(idxToKill,:) = [];
possibleNewPeopleProbEstimates(idxToKill,:) = [];
possibleNewPeopleLegsAssociation(idxToKill,:) = [];
possibleNewPeopleLegDistances(idxToKill,:) = [];
[possibleNewPeopleLegDistances, sortIdx] = sort(possibleNewPeopleLegDistances);
possibleNewPeopleCentroids = possibleNewPeopleCentroids(sortIdx,:);
possibleNewPeopleProbEstimates = possibleNewPeopleProbEstimates(sortIdx,:);
possibleNewPeopleLegsAssociation = possibleNewPeopleLegsAssociation(sortIdx,:);
while ~isempty(possibleNewPeopleLegsAssociation)
distancesToOtherCentroids = pdist2(possibleNewPeopleCentroids(1,:), possibleNewPeopleCentroids);
distancesToOtherCentroids(1,1) = Inf;
if ~isempty(peopleTracksCentroid)
distancesToOtherPeopleTracks = pdist2(possibleNewPeopleCentroids(1,:), peopleTracksCentroid);
else
distancesToOtherPeopleTracks = Inf;
end
if min(distancesToOtherCentroids) > thresholdDistToOtherCentroid && min(distancesToOtherPeopleTracks) > thresholdDistToOtherCentroid
legTrack1 = possibleNewPeopleLegsAssociation(1,1);
legTrack2 = possibleNewPeopleLegsAssociation(1,2);
% create new track
newTrack.prediction = KFinitialize(possibleNewPeopleCentroids(1,:)',possibleNewPeopleProbEstimates(1));
measures = [possibleNewPeopleCentroids(1,:) ,possibleNewPeopleProbEstimates(1)];
newTrack.prediction = PDAFupdate( newTrack.prediction, measures, sigmaZ,sigmaP);
newTrack.lastSeen = 0;
newTrack.id = freeID;
freeID = freeID + 1;
% average speed
newTrack.vecSpeed = zeros(2, ceil(windowSizeSecond * maxFps));
newTrack.avgSpeed = newTrack.prediction.x(3:4);
newTrack.heading = 3*pi/2;
peopleTracks = [peopleTracks newTrack];
legTrackOnPeople(legTrack1) = length(peopleTracks);
legTrackOnPeople(legTrack2) = length(peopleTracks);
idxToKill = find(possibleNewPeopleLegsAssociation(:,1)==legTrack1 | possibleNewPeopleLegsAssociation(:,1)==legTrack2 | possibleNewPeopleLegsAssociation(:,2)==legTrack1 | possibleNewPeopleLegsAssociation(:,2)==legTrack2);
else
idxToKill = 1;
end
possibleNewPeopleCentroids(idxToKill,:) = [];
possibleNewPeopleProbEstimates(idxToKill,:) = [];
possibleNewPeopleLegsAssociation(idxToKill,:) = [];
possibleNewPeopleLegDistances(idxToKill,:) = [];
end
legTrackOnPeopleOld = legTrackOnPeople;
legTrackOnPeople = zeros(length(tracker.legTracks),1);
legTrackOnPeople(idxTracksToSave) = legTrackOnPeopleOld;
tracker.peopleTracks = peopleTracks;
tracker.peopleFreeID = freeID;
end