mp_cleaning_27.m 8.08 KB


path_name ='C:\\Users\\User\\Documents\\gan_work\\25\\';
dinfo=dir('C:\\Users\\User\\Documents\\gan_work\\25\\*.png');
%txt=fullfile(dir_to_search, '*.png');
%dinfo=dir(txt);



%disp(cur_subject_x);


count=[0,0,0];
for d = 1: length(dinfo)
    
    label=-1;
    file_name = dinfo(d).name;
     
    motionpatch=imread(fullfile(path_name, file_name));
    cur_subject_x=double(motionpatch(:,:,1))./255;
    cur_subject_y=double(motionpatch(:,:,2))./255;
    cur_subject_z=double(motionpatch(:,:,3))./255;

    frame_num = size(cur_subject_x,1);   
    
    name = strcat(path_name,file_name(1:length(file_name)),'.png');
    
    % 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='C:\\Users\\User\\Documents\\gan_work\\datas\\mp_bad.txt';
        count(2)=count(2)+1;
        %copyfile(name,'C:\\Users\\User\\Documents\\gan_work\\datas\\mp_bad');
    else % good
        newdir='C:\\Users\\User\\Documents\\gan_work\\datas\\mp_good.txt';
        count(3)=count(3)+1;
        %copyfile(name,'C:\\Users\\User\\Documents\\gan_work\\datas\\mp_good');
        
        % 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.
        
     
    end
    
    txtfile=fopen(newdir,'a');
    fprintf(txtfile,file_name(1:length(file_name)));
    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)
value=value*1.1;
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
            % disp(patch);
            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