-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetBodyPoses.m
71 lines (60 loc) · 2.86 KB
/
getBodyPoses.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
% INPUT
% heatmaps: matrix of size [heatmapHeight * heatmapWidth * numBodyParts]
% one heatmap for each body part showing probability that particular
% body part is located at a particular location in the image, plus
% one heatmap for the background
% pafs: matrix of size [pafHeight * pafWidth * (2*numPairs)] containing part
% affinity fields for each body part pair. Each paf has x component
% and y component of vector field pointing from one body part to the
% other
% params: struct containing constants
% OUTPUT
% poses: matrix of size [numPeople * 18(aka numBodyParts) * 2(x,y)]
% records locations of each body part for each person in the image,
% the location value is [NaN, NaN] if that body part is not found in
% the image
function [poses] = getBodyPoses(heatmaps,pafs,params)
% Use nonmaximum suppression to extract body part coordinates from heatmaps.
[bodyParts,detectionScores] = detectBodyPartsFromHeatmaps(heatmaps,params);
% Create poses structure to be built
poses = [];
EMPTY_POSE = repmat([NaN NaN],params.NUM_BODY_PARTS,1);
poses(end+1,:,:) = EMPTY_POSE; %add empty pose so ismember works
% Loop through each pair type
for p = 1:size(params.PAIRS,1)
a = params.PAIRS(p,1);
b = params.PAIRS(p,2);
bodyPartsA = bodyParts{a};
bodyPartsB = bodyParts{b};
scoresA = detectionScores{a};
scoresB = detectionScores{b};
% Generate all possible pair candidates.
[candidates,heatmapScores] = getBodyPartPairCandidates(bodyPartsA, ...
bodyPartsB,scoresA,scoresB);
if isempty(candidates)
continue
end
% Get affinity field for this pair
px = params.PAF_INDEX(p,1);
py = params.PAF_INDEX(p,2);
affinityFieldX = pafs(:,:,px);
affinityFieldY = pafs(:,:,py);
affinityField = cat(3,affinityFieldX,affinityFieldY);
% Calculate the scores and sort candidates accordingly
scores = getBodyPartAffinityScores(candidates,affinityField,params);
% Add PAF scores to heatmap scores, discounting the heatmap scores
scores = scores + (params.HEATMAP_SCORE_DISCOUNT*heatmapScores);
candidates = [scores candidates];
candidates = sortrows(candidates,'descend');
% Only keep candidates with scores greater than 0
candidates = candidates(candidates(:,1)>0,:);
candidates = candidates(:,2:5);%remove scores after sorting
pairs = getValidBodyPartPairs(candidates);
% Add valid pairs to the poses
poses = addBodyPartPairsToBodyPoses(pairs,poses,a,b);
end
% Remove the dummy pose
poses = poses(2:end,:,:);
% Filter out invalid poses (poses with < x body parts)
poses = filterBodyPoses(poses, params);
end