27_cleaning.m 12.1 KB
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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412

% 맨처음 방향 (frame1) 으로 일괄 orientation normalize
% => 1번프레임이 아니라 프레임들의 중앙값
% 키로 모든방향 normalize
% 노멀라이즈시 최소값 빼는게 아니라 joint1-> 0.5/0.5/0.5로 가도록 
% 실험할때 전방향 노멀라이즈도 해봐야겠다..
path_name ='/home/hyuna/Documents/actionGAN_work/27/27_all_skeleton/';
dinfo=dir('/home/hyuna/Documents/actionGAN_work/27/27_all_skeleton/*.skeleton');
%txt=fullfile(dir_to_search, '*.skeleton');
%dinfo=dir(txt);
count=[0,0,0];

for d = 1: length(dinfo)
    label=-1;
    file_name = dinfo(d).name;
    
    
    name = strcat(path_name,file_name(1:20),'.skeleton');
    [token,remainder] = strtok(file_name,'A');
 
    class = str2num(remainder(2:4));
    %class=remainder(2:4);
    
    if class == 27
        bodyinfo = read_skeleton_file(name);
        frame_num = size(bodyinfo,2);   
    
    else
        continue;
    end
   
    if isempty(bodyinfo) || isempty(bodyinfo(1).bodies())
        disp("empty body");
        newdir='/home/hyuna/Documents/actionGAN_work/27/27_emptybody.txt';
      
        txtfile=fopen(newdir,'a');
        fprintf(txtfile, file_name(1:20));
        fprintf(txtfile, '\n');
        fclose(txtfile);
        count(1)=count(1)+1;
        continue;
    end
    
     
    %initialize
    cur_subject_x = zeros(frame_num, 25);
    cur_subject_y = zeros(frame_num, 25);
    cur_subject_z = zeros(frame_num, 25);

    tot_x = zeros(frame_num,25);
    tot_y = zeros(frame_num,25);
    tot_z = zeros(frame_num,25);

    joint_5 = zeros(1,3);
    joint_9 = zeros(1,3);
    joint_1 = zeros(1,3);
    joint_3 = zeros(1,3);

   
    
    %get total joints information
    for FN = 1:frame_num  
       
        cur_body = bodyinfo(FN).bodies(1);
        joints = cur_body.joints;

        for JN = 1:25
            tot_x(FN,JN) = joints(JN).x; 
            tot_y(FN,JN) = joints(JN).y; 
            tot_z(FN,JN) = joints(JN).z; 
        end
    end

    %Orientation normalization 1 : in space
    %get median values
    M_x = median(tot_x);
    M_y = median(tot_y);
    M_z = median(tot_z);

    %set 3 points for make plane
    joint_5 = [M_x(5) M_y(5) M_z(5)];
    joint_9 = [M_x(9) M_y(9) M_z(9)];
    joint_1 = [M_x(1) M_y(1) M_z(1)];
    joint_3 = [M_x(3) M_y(3) M_z(3)];

    %find RIGID TRNASFORMATION matrix
    d1 = joint_1 - joint_5;
    d2 = joint_1 - joint_9;
    n1 = cross(d1,d2); % because we will parallel transform, don't need to find belly
    u1 = n1/norm(n1);
    u2 = [0 0 1];
    cs1 = dot(u1,u2)/norm(u1)*norm(u2);
    ss1 = sqrt(1-cs1.^2);
    v1 = cross(u1,u2)/norm(cross(u1,u2));

    R1 = [v1(1)*v1(1)*(1-cs1)+cs1 v1(1)*v1(2)*(1-cs1)-v1(3)*ss1 v1(1)*v1(3)*(1-cs1)+v1(2)*ss1];
    R1(2,:) = [v1(1)*v1(2)*(1-cs1)+v1(3)*ss1 v1(2)*v1(2)*(1-cs1)+cs1 v1(2)*v1(3)*(1-cs1)-v1(1)*ss1];
    R1(3,:) = [v1(1)*v1(3)*(1-cs1)-v1(2)*ss1 v1(2)*v1(3)*(1-cs1)+v1(1)*ss1 v1(3)*v1(3)*(1-cs1)+cs1];

    %1-3 number tolls to parallel x axis. Rigid transformation on plane surface
    %Z axis coords oyler angle transform

    t = joint_3 - joint_1;
    d3 = R1(1,:) * t.'; 
    d3(1,2) = R1(2,:) * t.'; 
    d3(1,3) = R1(3,:) * t.'; 

    u3 = d3(1:2)/norm(d3(1:2));
    v3 = [u3(1) -u3(2)];
    v3(2,:) = [u3(2) u3(1)];
    u4 = [1 0].';

    csss = v3\u4;
    cs2 = csss(1);
    ss2 = csss(2);

    R2 = [cs2 -ss2 0];
    R2(2,:) = [ss2 cs2 0];
    R2(3,:) = [0 0 1];


    %apply rigid transformation
    for FN = 1:frame_num
        cur_body = bodyinfo(FN).bodies(1);
        joints = cur_body.joints;

        for JN = 1:25
            a = R1(1,:) * [joints(JN).x joints(JN).y joints(JN).z].'; 
            b = R1(2,:) * [joints(JN).x joints(JN).y joints(JN).z].'; 
            c = R1(3,:) * [joints(JN).x joints(JN).y joints(JN).z].'; 

            cur_subject_x(FN,JN) = R2(1,:) * [a b c].'; 
            cur_subject_y(FN,JN) = R2(2,:) * [a b c].'; 
            cur_subject_z(FN,JN) = R2(3,:) * [a b c].'; 

        end
    end

    %orientation normalize 2 (with plane surface)
    if cur_subject_x(1,4) < cur_subject_x(1,1) 
        cur_subject_x = 0 - cur_subject_x;
    end

    if cur_subject_y(1,9) > cur_subject_y(1,5)
        cur_subject_y = 0 - cur_subject_y;
    end

    % for save origin subjects before data augment
    clear_subject_x = cur_subject_x;
    clear_subject_y = cur_subject_y;
    clear_subject_z = cur_subject_z;

    % patch_num*25
    red=[4,3,21,2,1];
    green=[5,6,7,8,23,22];
    blue=[9,10,11,12,25,24];
    yellow=[17,18,19,20];
    skyblue=[13,14,15,16];
 
  
       
    target = {skyblue,yellow,green,blue};
    [r,c]=size(target);
    
   %celldisp(target);
   %disp(c);
   %disp(target{1});
   
    % joint-to-joint   
    for tar_index = 1:c    
        diff_ave = comp_aver(clear_subject_x,clear_subject_y,clear_subject_z,frame_num,target{tar_index});

        if label~=0
           label=set_label(diff_ave,0.13); 
        end    

        %{
        if label==0 %bad
            disp(file_name(1:20));
            disp("difference");
            disp(tar_index);
            disp(target(tar_index));
            disp(diff_ave);
        end
        %}
        
    end
    
    
    % reds(head-spine) shoud be always parallel to z
    if label~=0
        label=set_label(stand_straight(clear_subject_x,clear_subject_y,clear_subject_z, 1,frame_num,red),1.8);
    end
    
   % should check straightness for both legs in case 27 only for start and
   % end of execution
   %{
   if label~=0
       disp(name);
       disp(stand_straight(clear_subject_x,clear_subject_y,clear_subject_z, 1,3, skyblue));
       disp(stand_straight(clear_subject_x,clear_subject_y,clear_subject_z, frame_num-3,frame_num, skyblue));
   end
   %}
 
   for tar_index = 1:c
       label= set_label(stand_straight(clear_subject_x,clear_subject_y,clear_subject_z, 1,3, target{tar_index}),2.0);
       if label==0
           break;
       end
       
       label= set_label(stand_straight(clear_subject_x,clear_subject_y,clear_subject_z, frame_num-3,frame_num, target{tar_index}),2.0);
       if label==0
           break;
       end
   end
   
    % green, blue(arms) should be located in almost the same position at the
    % beginning and end of motion.
    
    %print a distance between starting and ending coords
    g_distance=loc_end_to_end(clear_subject_x,clear_subject_y,clear_subject_z,frame_num,skyblue);
    b_distance=loc_end_to_end(clear_subject_x,clear_subject_y,clear_subject_z,frame_num,yellow);
    
    
    % only use just 2 joints at the tip of arms(hands)
    g_end=g_distance(end-1:end);
    b_end=b_distance(end-1:end);
    
    if label~=0
    label=set_label(g_end,0.7);
    end
 
    % check if label is still 'good'
    if label~=0
    label=set_label(b_end,0.7);
    end
    
    ju1=0;
    ju2=0;
    if label~=0   
        legs={skyblue, yellow};
           
        % count how many times it jumped for each leg
        ju1=num_jumped(clear_subject_z,legs{1}(2),frame_num);
        ju2=num_jumped(clear_subject_z,legs{2}(2),frame_num);
        
        if ju1 ~= ju2
            label = 0;
        end
    end

    % save good and bad examples seperately 
    newdir='';
    if label==0 % bad
        newdir='/home/hyuna/Documents/actionGAN_work/27/27_bad.txt';
        count(2)=count(2)+1;
    else % good
        newdir='/home/hyuna/Documents/actionGAN_work/27/27_good.txt';
        count(3)=count(3)+1;
        copyfile(name,'/home/hyuna/Documents/actionGAN_work/27/27_good_skeleton');
        
        % The condition below is used for a class with subclasses of right/left major limb.
        %{ 
        major =major_limb(clear_subject_z, skyblue, yellow);
        if(major==skyblue)
            s_dir='/home/hyuna/Documents/actionGAN_work/24/24_good_right.txt';
            copyfile(name,'/home/hyuna/Documents/actionGAN_work/24/24_right_skeleton');
        else
           s_dir='/home/hyuna/Documents/actionGAN_work/24/24_good_left.txt';
           copyfile(name,'/home/hyuna/Documents/actionGAN_work/24/24_left_skeleton');
        end
        %}
        
        % The condition below is used for a class 27 to classify how many
        % times it jumped.
        
        if ju1 == 1 %jumped once
            s_dir='/home/hyuna/Documents/actionGAN_work/27/27_once.txt';
            copyfile(name,'/home/hyuna/Documents/actionGAN_work/27/27_once');
        elseif ju1 == 2 %jumped twice
            s_dir='/home/hyuna/Documents/actionGAN_work/27/27_twice.txt';
            copyfile(name,'/home/hyuna/Documents/actionGAN_work/27/27_twice');
        else
            s_dir='/home/hyuna/Documents/actionGAN_work/27/27_unknown.txt';
            copyfile(name,'/home/hyuna/Documents/actionGAN_work/27/27_unknown');
            
        end
        split=fopen(s_dir,'a');
        fprintf(split, file_name(1:20));
        fprintf(split, '\n');
        fclose(split);
        
    end
    
    txtfile=fopen(newdir,'a');
    fprintf(txtfile, file_name(1:20));
    fprintf(txtfile, '\n');
    fclose(txtfile);
    
end

% number of [emptybody, bad, good]
disp(count);



function stand=stand_straight(clear_subject_x,clear_subject_y,clear_subject_z, start,end_ind,target)
max_t=[];

for j = 1: length(target)-1
    theta_per_patch=[];
    for patch = start:end_ind % usually 1:frame_num
        u = [clear_subject_x(patch, target(j+1))-clear_subject_x(patch, target(j)),clear_subject_y(patch, target(j+1))-clear_subject_y(patch, target(j)),clear_subject_z(patch, target(j+1))-clear_subject_z(patch, target(j))];
        % v = axis z
        v = [0,0,1];
        % angle between [vector joint to joint] and [axis z]
        theta = atan2(norm(cross(u,v)),dot(u,v));
        %if theta > 90
        %theta = theta - 90;
        %end
        
        %print all the theta and max of it.
        %disp(theta);
        
        theta_per_patch=[theta_per_patch, theta];
    end
    % maximum theta for each bones through all the patches
    % max_t[4]
    max_t = [max_t,max(theta_per_patch)];
    clear theta_per_patch;
    
end
%disp("max");
%disp(max_t);
% usually 1.6<max angle<=1.8
stand=max_t;
end


function setlabel=set_label(target,value)
if target(target>value) %bad
    label=0;
else
    label=1;
end
setlabel=label;
end


function loc_end=loc_end_to_end(clear_subject_x,clear_subject_y,clear_subject_z,frame_num,target)
echo off;
distance=[];   

for j = 1: length(target)
        x_=clear_subject_x(1, target(j))-clear_subject_x(frame_num, target(j));
        y_=clear_subject_y(1, target(j))-clear_subject_y(frame_num, target(j));
        z_=clear_subject_z(1, target(j))-clear_subject_z(frame_num, target(j));
        d=sqrt(x_*x_+y_*y_+z_*z_);
        distance=[distance,d];
     end
loc_end =distance;
% disp(distance);
end

function comp_ave =comp_aver(clear_subject_x,clear_subject_y,clear_subject_z,frame_num,target)
for j = 1: length(target)    
         % distance between a particular joint and average coordinate per patch
        dist_ave=[];
        for patch = 2:frame_num-1
            ave_x=(clear_subject_x(patch-1,target(j))+clear_subject_x(patch+1,target(j)))/2;
            ave_y=(clear_subject_y(patch-1,target(j))+clear_subject_y(patch+1,target(j)))/2;
            ave_z=(clear_subject_z(patch-1,target(j))+clear_subject_z(patch+1,target(j)))/2;
                       
            % distance between joint and average 
            jnt_ave = sqrt((abs(clear_subject_x(patch, target(j))-ave_x)).^2+ (abs(clear_subject_y(patch,  target(j))-ave_y)).^2+ (abs(clear_subject_z(patch,  target(j))-ave_z)).^2);
            dist_ave=[dist_ave,jnt_ave];
                       
        end
        
end
    comp_ave=dist_ave;
end
function limb= major_limb(clear_subject_z, target1, target2)
    z1=max(clear_subject_z(:,target1(end))) - clear_subject_z(1, target1(end));
    z2=max(clear_subject_z(:,target2(end))) - clear_subject_z(1, target2(end));

    if z1>z2 % target1 raised higher than t2
        limb=target1;
    else
        limb=target2;
    end 
     
end

function num = num_jumped(clear_subject_z, target,frame_num)
    state=0; %
    for i = 1: frame_num-1
        % check if it had descended
        if state==1 && clear_subject_z(i,target) < clear_subject_z(i+1,target)
            state=2;

        elseif clear_subject_z(i,target)>clear_subject_z(i+1,target)
            state=1; % first descending
        else
            state=-1;
        end
    end
    num=state;
end