OpenPose训练过程解析(3)
2018-08-25 本文已影响0人
LaLa_2539
genJson.m
运行需要输入参数:genJSON('COCO')
1 function genJSON(dataset)
2 addpath('../testing/util');
3 addpath('../testing/util/jsonlab/');
4
5 if(strcmp(dataset, 'COCO')) %字符串比较,一致返回1
6 mkdir('dataset/COCO/json')
7 count = 1;
8 makeFigure = 0;
9 validationCount = 0;
10 isValidation = 0; %是否为验证集标志位
11
12 load('dataset/COCO/mat/coco_kpt.mat');
13 load('dataset/COCO/mat/coco_val.mat');
14
15 for mode = 0:1
16 if mode == 0
17 RELEASE = coco_kpt;
18 else
19 RELEASE = coco_val;
20 end
21
22 trainIdx = 1:1:size(RELEASE,2); %trainIdx = coco_kpt的列数,即所有图片的数量
23
24 % In COCO:(1-'nose' 2-'left_eye' 3-'right_eye' 4-'left_ear' 5-'right_ear'
25 % 6-'left_shoulder' 7-'right_shoulder' 8-'left_elbow' 9-'right_elbow' 10-'left_wrist'
26 % 11-'right_wrist' 12-'left_hip' 13-'right_hip' 14-'left_knee' 15-'right_knee'
27 % 16-'left_ankle' 17-'right_ankle' )
28
29 for i = trainIdx
30 numPeople = length(RELEASE(i).annorect); %一张图片中的人数
31 fprintf('prepareJoint: %d/%d (numPeople: %d)\n', i, trainIdx(end), numPeople);
32 %allPeopleAnno = RELEASE.annolist(i).annorect;
33 prev_center = [];
34
35 if mode == 1 %val(验证集)
36 if i < 2645
37 validationCount = validationCount + 1;
38 fprintf('My validation! %d, %d\n', i, validationCount); %自己的验证集
39 isValidation = 1;
40 else
41 isValidation = 0;
42 end
43 else
44 isValidation = 0;
45 end
46
47 h = RELEASE(i).annorect.img_height;
48 w = RELEASE(i).annorect.img_width;
49
50 for p = 1:numPeople %一张图片中的人数 end : Line187
51
52 % skip this person if parts number is too low or if
53 % segmentation area is too small
54 if RELEASE(i).annorect(p).num_keypoints < 5 || RELEASE(i).annorect(p).area < 32*32
55 continue; %少于5个关节点以及面积小于32×32,跳过此人
56 end
57 % skip this person if the distance to exiting person is too small
58 person_center = [RELEASE(i).annorect(p).bbox(1)+RELEASE(i).annorect(p).bbox(3)/2, RELEASE(i).annorect(p).bbox(2)+RELEASE(i).annorect(p).bbox(4)/2]; %person检测框的中心
59 flag = 0;
60 for k = 1:size(prev_center,1)
61 dist = prev_center(k,1:2) - person_center; %prev_center是上个人的中心,见Line184
62 if norm(dist) < prev_center(k,3)*0.3 %删除两个距离过近的人中的一个
63 flag = 1;
64 continue;
65 end
66 end
67 if flag ==1
68 continue;
69 end
70 %fprintf('%d/%d/ image%d:', p,numPeople,i);
71 if mode == 0 %coco_kpt
72 joint_all(count).dataset = 'COCO';
73 else
74 joint_all(count).dataset = 'COCO_val';
75 end
76 joint_all(count).isValidation = isValidation; %是否是验证集
77 anno = RELEASE(i).annorect(p).keypoints; %一张图片第p个人的keypoints
78
79 % set image path
80 if mode == 0
81 joint_all(count).img_paths = sprintf('train2014/COCO_train2014_%012d.jpg', RELEASE(i).image_id);
82 else
83 joint_all(count).img_paths = sprintf('val2014/COCO_val2014_%012d.jpg', RELEASE(i).image_id);
84 end
85 %joint_all(count).img_paths = RELEASE(i).image_id;
86 %[h,w,~] = size(imread(['../dataset/COCO/images/', joint_all(count).img_paths]));
87 joint_all(count).img_width = w; %joint_all主要包含单个人的框中心坐标,bbox,area,num_keypoints,joint_self(每个人17个keypoints的坐标)
88 joint_all(count).img_height = h;
89 joint_all(count).objpos = person_center;
90 joint_all(count).image_id = RELEASE(i).image_id;
91 joint_all(count).bbox = RELEASE(i).annorect(p).bbox;
92 joint_all(count).segment_area = RELEASE(i).annorect(p).area;
93 joint_all(count).num_keypoints = RELEASE(i).annorect(p).num_keypoints;
94
95 % set part label: joint_all is (np-3-nTrain)
96 % for this very center person
97 for part = 1:17
98 joint_all(count).joint_self(part, 1) = anno(part*3-2); %part的x坐标
99 joint_all(count).joint_self(part, 2) = anno(part*3-1); %part的y坐标
100
101 if(anno(part*3) == 2)
102 joint_all(count).joint_self(part, 3) = 1; %存在关节点标记
103 elseif(anno(part*3) == 1)
104 joint_all(count).joint_self(part, 3) = 0;
105 else
106 joint_all(count).joint_self(part, 3) = 2; %不存在关节点标记
107 end
108 end
109
110 % pad it into 17x3
111 dim_1 = size(joint_all(count).joint_self, 1); %返回行数
112 dim_3 = size(joint_all(count).joint_self, 3); %若为彩色图像,返回3;若为灰度图像,返回1;这里返回1
113 pad_dim = 17 - dim_1; %这里可能是为了防止丢失某些keypoints而做的填充
114 joint_all(count).joint_self = [joint_all(count).joint_self; zeros(pad_dim, 3, dim_3)]; %(pad_dim, 3, dim_3)维的零元素
115 % set scale
116 joint_all(count).scale_provided = RELEASE(i).annorect(p).bbox(4)/368; %"bbox" : [x,y,width,height],
117 %joint_all(count).scale_provided = RELEASE(i).annorect(p).area;
118
119 % for other person on the same image
120 count_other = 1;
121 joint_all(count).joint_others = cell(0,0); %创建一个空的cell数组
122 for op = 1:numPeople %Line30
123 if op == p || RELEASE(i).annorect(op).num_keypoints == 0
124 continue; %if为真,跳过当次循环
125 end
126 anno = RELEASE(i).annorect(op).keypoints;
127
128 joint_all(count).scale_provided_other(count_other) = RELEASE(i).annorect(op).bbox(4)/368; %同框中他人的joint_all信息
129 %joint_all(count).scale_provided_other(count_other) = RELEASE(i).annorect(op).area;
130 joint_all(count).objpos_other{count_other} = [RELEASE(i).annorect(op).bbox(1)+RELEASE(i).annorect(op).bbox(3)/2, RELEASE(i).annorect(op).bbox(2)+RELEASE(i).annorect(op).bbox(4)/2];
131 joint_all(count).bbox_other{count_other} = RELEASE(i).annorect(op).bbox;
132 joint_all(count).segment_area_other(count_other) = RELEASE(i).annorect(op).area;
133 joint_all(count).num_keypoints_other(count_other) = RELEASE(i).annorect(op).num_keypoints;
134
135 % other people
136 joint_others{count_other} = zeros(17,3);
137 for part = 1:17
138 joint_all(count).joint_others{count_other}(part, 1) = anno(part*3-2);
139 joint_all(count).joint_others{count_other}(part, 2) = anno(part*3-1);
140
141 if(anno(part*3) == 2)
142 joint_all(count).joint_others{count_other}(part, 3) = 1;
143 elseif(anno(part*3) == 1)
144 joint_all(count).joint_others{count_other}(part, 3) = 0;
145 else
146 joint_all(count).joint_others{count_other}(part, 3) = 2;
147 end
148
149 end
150 count_other = count_other + 1;
151 end
152 joint_all(count).annolist_index = i;
153 joint_all(count).people_index = p;
154 joint_all(count).numOtherPeople = length(joint_all(count).joint_others); %其他人的个数
155
156 if(makeFigure) % visualizing to debug
157 imshow(['dataset/COCO/images/', joint_all(count).img_paths]);
158 xlim([-joint_all(count).img_width*0.6 joint_all(count).img_width*1.6]) %用于设定x轴上下限
159 ylim([-joint_all(count).img_height*0.6 joint_all(count).img_height*1.6]) %用于设定y轴上下限
160 hold on;
161 visiblePart = joint_all(count).joint_self(:,3) == 1; %17个keypoints中存在的点返回1,不存在的返回0(标注了,但是为0)
162 invisiblePart = joint_all(count).joint_self(:,3) == 0; %17个keypoints中未标注的点
163 plot(joint_all(count).joint_self(visiblePart, 1), joint_all(count).joint_self(visiblePart,2), 'gx'); %g--绿色;x--叉号符
164 plot(joint_all(count).joint_self(invisiblePart,1), joint_all(count).joint_self(invisiblePart,2), 'rx'); %r--红色;x--叉号符
165 plot(joint_all(count).objpos(1), joint_all(count).objpos(2), 'cs'); %人物框中心点 c--青绿色;s--正方形
166 if(~isempty(joint_all(count).joint_others)) %若joint_others不为空,画点
167 for op = 1:size(joint_all(count).joint_others,2)
168 visiblePart = joint_all(count).joint_others{op}(:,3) == 1;
169 invisiblePart = joint_all(count).joint_others{op}(:,3) == 0;
170 plot(joint_all(count).joint_others{op}(visiblePart,1), joint_all(count).joint_others{op}(visiblePart,2), 'mx');
171 plot(joint_all(count).joint_others{op}(invisiblePart,1), joint_all(count).joint_others{op}(invisiblePart,2), 'cx');
172 plot(joint_all(count).objpos_other{op}(1), joint_all(count).objpos_other{op}(2), 'cs');
173 end
174 end
175 %rect_size = 368*joint_all(count).scale_provided/ 1.2;
176 rect_size = 2.1*sqrt(joint_all(count).scale_provided)/ 1.2;
177 max(RELEASE(i).annorect(p).bbox(3), RELEASE(i).annorect(p).bbox(4))
178 sqrt(joint_all(count).scale_provided)
179 rectangle('Position',[joint_all(count).objpos(1)-rect_size, joint_all(count).objpos(2)-rect_size, rect_size*2, rect_size*2], 'EdgeColor','b') %rectangle('Position', pos)在二维坐标中创建一个矩形,将pos指定为[x y w h]形式的四元素向量,x y为矩形左下角位置;EdgeColor--轮廓颜色; 参考:https://ww2.mathworks.cn/help/matlab/ref/rectangle.html#busopj9-2
180 pause;
181 close all;
182 end
183 %prev_center = [prev_center; joint_all(count).objpos joint_all(count).scale_provided*368];
184 prev_center = [prev_center; joint_all(count).objpos max(RELEASE(i).annorect(p).bbox(3), RELEASE(i).annorect(p).bbox(4))];
185 count = count + 1;
186 %if(count==10), break; end %scale_provided
187 end
188 %if(count==10), break; end
189 end
190 end
191
192 opt.FileName = 'dataset/COCO/json/COCO.json';
193 opt.FloatFormat = '%.3f';
194 savejson('root', joint_all, opt);
195 end
-
Line158、159 x、ylim
x,ylim函数.png -
Line163、164、165
joint标注.png -
Line179 : rectangle函数
after rectangle.png