-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpii_vnect_demo_video.m
246 lines (217 loc) · 8.65 KB
/
mpii_vnect_demo_video.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
CROP_TRACKING_MODE = false;
IS_FIRST_FRAME = true;
%% Global Params
CROP_SIZE = 368; %384;
CROP_RECT = [0, 0, CROP_SIZE, CROP_SIZE];
hm_factor = 8;
VISUALIZE_HEATMAPS = true;
%Point to your matcaffe
addpath('/home/makkunda/caffe-rc5/matlab')
addpath('./util');
img_base_path = './data';
%Get the joint parents and the labels. The extended set also contains hand
%and feet tips which may not be as stable. Use the first 17 in that case.
[~,o1,~,relevant_labels] = mpii_vnect_get_joints('extended');
%% Variables
CROP_SCALE = 1.0;
pad_offset(1:2) = 0;
% f1 = OneEuroFilter(30.0,1.7,0.3);
% f2 = OneEuroFilter(30.0,0.8,0.4);
% f3 = OneEuroFilter(30.0,20,0.4);
for i11 = 1:1:2
for j11 = 1:1:21
f1(i11,j11) = OneEuroFilter(60.0,1.7,0.3);
end
end
for i11 = 1:1:3
for j11 = 1:1:21
f2(i11,j11) = OneEuroFilter(60.0,0.8,0.4);
end
end
for i11 = 1:1:3
for j11 = 1:1:21
f3(i11,j11) = OneEuroFilter(60.0,20,0.4);
end
end
%%
caffe.set_mode_gpu()
caffe.set_device(0)
caffe.reset_all();
net = caffe.Net('./models/vnect_net.prototxt', './models/vnect_model.caffemodel', 'test');
video_path = fullfile(img_base_path, sprintf('IMG_3881.MOV'));
v=VideoReader(video_path);
v1=VideoWriter('sid3.avi');
open(v1);
i12=130;
while (hasFrame(v))
video=readFrame(v);
%video=permute(video,[2,1,3]);
% fprintf('#####')
i12=i12+1;
% if mod(i12-1,10)~=0
% continue
% end
img = permute(double(video)/255.0 - 0.4, [2,1,3]);
img = img(:,:,[3 2 1]);
img = imresize(img, [848, 448]);
if(~CROP_TRACKING_MODE)
scales = 1:-0.2:0.6;
box_size = [size(img,1),size(img,2)];
CROP_TRACKING_MODE = true;
else
scales = [1.0,0.7];
box_size = [CROP_SIZE, CROP_SIZE];
crop_offset = int32(floor(40.0 / CROP_SCALE));
%Get 2D locations in the previous frame
min_crop = (int32(CROP_RECT(1:2) + min(pred_2d, [], 2)'/CROP_SCALE) - int32(pad_offset/CROP_SCALE)) - crop_offset;
max_crop = (int32(CROP_RECT(1:2) + max(pred_2d, [], 2)'/CROP_SCALE) - int32(pad_offset/CROP_SCALE)) + crop_offset;
old_crop = CROP_RECT;
CROP_RECT(1:2) = max(min_crop, int32([1, 1]));
CROP_RECT(3:4) = min(max_crop, int32([size(img, 1), size(img,2)]));
CROP_RECT(3:4) = CROP_RECT(3:4) - CROP_RECT(1:2);
%Temporal smoothing of the crops
if(~IS_FIRST_FRAME)
mu = 0.8;
CROP_RECT = (1-mu)* CROP_RECT + mu * old_crop;
IS_FIRST_FRAME = false;
end
img = imcrop(img, CROP_RECT([2,1,4,3]));
CROP_SCALE = (CROP_SIZE-2) / max(size(img, 1), size(img, 2));
img = imresize(img, CROP_SCALE);
if(size(img,1) > size(img,2))
pad_offset(1) = 0;
pad_offset(2) = (CROP_SIZE-size(img,2)) / 2;
else
pad_offset(2) = 0;
pad_offset(1) = (CROP_SIZE-size(img,1)) / 2;
end
img = mpii_vnect_pad_image(img, box_size);
end
%Once we know the crop, get the image ready to be fed into the network
data = zeros(box_size(1), box_size(2), 3, length(scales));
for si = 1:length(scales)
data(:,:,:,si) = mpii_vnect_pad_image(imresize(img, scales(si)), box_size);
end
net.blobs('data').reshape([size(data,1) size(data,2) size(data,3) size(data,4)]);
net.forward({data(:,:,:,:)});
%Get the heatmaps and the location maps from the network
output_heatmap = net.blobs('heatmap').get_data();
output_xmap = net.blobs('x_heatmap').get_data();
output_ymap = net.blobs('y_heatmap').get_data();
output_zmap = net.blobs('z_heatmap').get_data();
%Housekeeping for the next step
hm_size = box_size/hm_factor; %or size(output_heatmap); 1 and 2
heatmap = zeros(hm_size(1), hm_size(2), size(output_heatmap,3));
xmap = zeros(hm_size(1), hm_size(2), size(output_zmap,3));
ymap = zeros(hm_size(1), hm_size(2), size(output_zmap,3));
zmap = zeros(hm_size(1), hm_size(2), size(output_zmap,3));
%Since the predicted heatmaps and location maps are at different
%scales,they need to be rescaled and averaged
for si = 1:length(scales)
s_hm = imresize(output_heatmap(:,:,:,si), 1.0/scales(si), 'bilinear');
s_xhm = imresize(output_xmap(:,:,:,si), 1.0/scales(si), 'bilinear');
s_yhm = imresize(output_ymap(:,:,:,si), 1.0/scales(si), 'bilinear');
s_zhm = imresize(output_zmap(:,:,:,si), 1.0/scales(si), 'bilinear');
mid_pt = size(s_hm)/2;
heatmap = heatmap + s_hm( (mid_pt(1) -floor(hm_size(1)/2)+1): (mid_pt(1) +ceil(hm_size(1)/2)), (mid_pt(2) -floor(hm_size(2)/2)+1): (mid_pt(2) +ceil(hm_size(2)/2)),:);
xmap = xmap + s_xhm( (mid_pt(1) -floor(hm_size(1)/2)+1): (mid_pt(1) +ceil(hm_size(1)/2)), (mid_pt(2) -floor(hm_size(2)/2)+1): (mid_pt(2) +ceil(hm_size(2)/2)),:);
ymap = ymap + s_yhm( (mid_pt(1) -floor(hm_size(1)/2)+1): (mid_pt(1) +ceil(hm_size(1)/2)), (mid_pt(2) -floor(hm_size(2)/2)+1): (mid_pt(2) +ceil(hm_size(2)/2)),:);
zmap = zmap + s_zhm( (mid_pt(1) -floor(hm_size(1)/2)+1): (mid_pt(1) +ceil(hm_size(1)/2)), (mid_pt(2) -floor(hm_size(2)/2)+1): (mid_pt(2) +ceil(hm_size(2)/2)),:);
end
%Final heatmaps and location maps, from which we can infer the 2D and
%3D pose
heatmap = heatmap/length(scales);
xmap = xmap/length(scales);
ymap = ymap/length(scales);
zmap = zmap/length(scales);
if(i12>132)
old_vel=pred_p-old_p;
end
if(i12>131)
old_p=pred_p;
end
pred_p = zeros(3,size(heatmap,3));
pred_2d = zeros(2, size(pred_p,2));
hm = zeros(box_size(2), box_size(1), size(heatmap,3));
% Take the maximas in the heatmaps as the 2D predictions. You can
% substitue this with a function guided by the distance from the
% root joint. Use the maxima locations to get the 3D joint locations
for k = 1:size(heatmap,3)
hm(:,:,k) = imresize(permute(heatmap(:,:,k),[2,1,3]), hm_factor);
[~,max_idx] = max(reshape(hm(:,:,k),1,[]));
[y,x] = ind2sub(size(hm(:,:,k)), max_idx(1));
pred_2d(1:2,k) = [x,y];
end
for i11 = 1:1:2
for j11 = 1:1:21
[f1(i11,j11),pred_2d(i11,j11)] = f1(i11,j11).filter2(pred_2d(i11,j11),i12-130);
end
end
%[f1,pred_2d] = f1.filter2(pred_2d,i-130);
for k = 1:size(heatmap,3)
x = pred_2d(1,k);
y = pred_2d(2,k);
pred_p(1,k) = 100* xmap(max(floor(x/hm_factor),1), max(floor(y/hm_factor),1) ,k);
pred_p(2,k) = 100* ymap(max(floor(x/hm_factor),1), max(floor(y/hm_factor),1) ,k);
pred_p(3,k) = 100* zmap(max(floor(x/hm_factor),1), max(floor(y/hm_factor),1) ,k);
end
%Subtract the root location just to be safe.
pred_p = bsxfun(@minus, pred_p, pred_p(:,15));
for i11 = 1:1:3
for j11 = 1:1:21
[f2(i11,j11),pred_p(i11,j11)] = f2(i11,j11).filter2(pred_p(i11,j11),i12-130);
end
end
if(i12>132)
fun=@(x)TotalError(x,pred_p,pred_2d,old_p,old_vel);
x0(:,1:21)=pred_p;
if(i12==133)
x0(:,22)=0;
else
x0(:,22)=d;
end
options.Algorithm='levenberg-marquardt';
x=lsqnonlin(fun,x0,[],[],options);
pred_p=x(:,1:21);
d=x(:,22);
end %Plot the predicted Pose
for i11 = 1:1:3
for j11 = 1:1:21
[f3(i11,j11),pred_p(i11,j11)] = f3(i11,j11).filter2(pred_p(i11,j11),i12-130);
end
end
if(i12>133)
video2=DrawCircle(permute(img(:,:,[3,2,1]), [2,1,3])+0.4,pred_2d);
video2=DrawLine1(video2,pred_2d,1,17);
video2=DrawLine1(video2,pred_2d,17,2);
video2=DrawLine1(video2,pred_2d,2,6);
video2=DrawLine1(video2,pred_2d,2,3);
video2=DrawLine1(video2,pred_2d,3,4);
video2=DrawLine1(video2,pred_2d,4,5);
video2=DrawLine1(video2,pred_2d,5,18);
video2=DrawLine1(video2,pred_2d,6,7);
video2=DrawLine1(video2,pred_2d,7,8);
video2=DrawLine1(video2,pred_2d,2,16);
video2=DrawLine1(video2,pred_2d,16,15);
video2=DrawLine1(video2,pred_2d,15,12);
video2=DrawLine1(video2,pred_2d,12,13);
video2=DrawLine1(video2,pred_2d,13,14);
video2=DrawLine1(video2,pred_2d,14,21);
video2=DrawLine1(video2,pred_2d,15,9);
video2=DrawLine1(video2,pred_2d,9,10);
video2=DrawLine1(video2,pred_2d,10,11);
video2=DrawLine1(video2,pred_2d,11,20);
video1 = imresize(video2,[size(video,1),size(video,2)]);
% %video1=uint8(video2);
% fprintf('############zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz')
imshow(video1)
% writeVideo(v1,video1);
%F=getframe(gcf);
%F=imresize(F,[size(video,1),size(video,2)])
%imshow(F);
writeVideo(v1,mat2gray(video1));
F.Visible='off';
end
end
close(v1)
%close(v)