Exemplo n.º 1
0
def sequence_truth(
        file_names=['training1', 'training2', 'training3', 'training4']):
    df_out = DataFrame()
    for file_name in file_names:
        df = skeletion_from_archive_cached(file_name)
        df = df[['frame', 'gesture', 'sample_id']].dropna().drop_duplicates()
        df_out = pd.concat((df_out, df))
    return df_out
Exemplo n.º 2
0
def agg_movement_intervals(
        file_name,
        has_labels=False,
        train_on=['training1', 'training2', 'training3', 'training4']):
    from models import movement_interval

    df = skeletion_from_archive_cached(file_name)
    df = preprocessed_skeleton(file_name,
                               demain=True,
                               keep_only_top_40=False,
                               train_id=False,
                               drop_lower_joints=True,
                               dummy_gesture=False)

    df_interval = movement_interval(train_on=train_on)
    df_merge = pd.merge(df,
                        df_interval[['sample_id', 'frame', 'movement']],
                        on=['frame', 'sample_id'],
                        how='left')

    df_merge.sort(['sample_id', 'frame'], inplace=True)
    df_merge.fillna(method='ffill', inplace=True)
    df_merge.fillna(method='bfill', inplace=True)

    df_merge['interval'] = (df_merge.movement.shift(1) !=
                            df_merge.movement).astype(int).cumsum()
    # delete break frames tagged with gesture
    df_merge = df_merge[(df_merge.movement != 0)]

    # unify gestures in interval to most frequent one
    def smooth_gesture(df):
        df['gesture'] = df.gesture.value_counts().index[0]
        return df

    agg_functions = ['median', 'var', 'min', 'max']

    if has_labels:
        df_merge = df_merge.groupby(['interval']).apply(smooth_gesture)
        # drop intervalls without movement or if labelt as break
        df_merge = df_merge[(df_merge['gesture'] != 'break')
                            & (df_merge['movement'] == 1)]
        y_gest = df_merge[['sample_id', 'gesture',
                           'interval']].drop_duplicates()
        y_gest['gesture'] = np.array(
            [gesture_to_id[g] for g in y_gest.gesture])

    df_merge = df_merge.drop(['movement', 'frame'], axis=1)
    df_agg = df_merge.groupby(['sample_id', 'JointType', 'interval'
                               ]).agg(agg_functions).unstack('JointType')
    df_agg['_count'] = df_merge.groupby(['sample_id', 'JointType',
                                         'interval']).agg({
                                             'x_p': 'count'
                                         }).unstack('JointType')[('x_p',
                                                                  'HandRight')]

    if has_labels:
        return df_agg, y_gest
    return df_agg, None
def sequence_truth(file_names=['training1',
                               'training2',
                               'training3',
                               'training4']):
    df_out = DataFrame()
    for file_name in file_names:
        df = skeletion_from_archive_cached(file_name)
        df = df[['frame', 'gesture', 'sample_id']].dropna().drop_duplicates()
        df_out = pd.concat((df_out, df))
    return df_out
def agg_movement_intervals(file_name, has_labels=False,
         train_on=['training1','training2', 'training3', 'training4']):
    from models import movement_interval


    df = skeletion_from_archive_cached(file_name)
    df = preprocessed_skeleton(file_name, demain=True, keep_only_top_40=False,
            train_id=False, drop_lower_joints=True, dummy_gesture=False)


    df_interval = movement_interval(train_on=train_on)
    df_merge = pd.merge(df, df_interval[['sample_id', 'frame', 'movement']], on=['frame', 'sample_id'], how='left')

    df_merge.sort(['sample_id', 'frame'], inplace=True)
    df_merge.fillna(method='ffill', inplace=True)
    df_merge.fillna(method='bfill', inplace=True)

    df_merge['interval'] = (df_merge.movement.shift(1) != df_merge.movement).astype(int).cumsum()
    # delete break frames tagged with gesture
    df_merge = df_merge[(df_merge.movement != 0)]

    # unify gestures in interval to most frequent one
    def smooth_gesture(df):
        df['gesture'] = df.gesture.value_counts().index[0]
        return df

    agg_functions=['median', 'var', 'min', 'max']

    if has_labels:
        df_merge = df_merge.groupby(['interval']).apply(smooth_gesture)
        # drop intervalls without movement or if labelt as break
        df_merge = df_merge[(df_merge['gesture'] != 'break') & (df_merge['movement'] == 1)]
        y_gest = df_merge[['sample_id', 'gesture', 'interval']].drop_duplicates()
        y_gest['gesture'] = np.array([gesture_to_id[g] for g in y_gest.gesture])

    df_merge = df_merge.drop(['movement', 'frame'], axis=1)
    df_agg = df_merge.groupby(['sample_id', 'JointType', 'interval']).agg(agg_functions).unstack('JointType')
    df_agg['_count'] = df_merge.groupby(['sample_id', 'JointType', 'interval']).agg({'x_p': 'count'}).unstack('JointType')[('x_p', 'HandRight')]

    if has_labels:
        return df_agg, y_gest
    return df_agg, None
Exemplo n.º 5
0
def preprocessed_skeleton(file_name,
                          demain=True,
                          keep_only_top_40=True,
                          train_id=True,
                          drop_lower_joints=True,
                          dummy_gesture=False,
                          window_shift=1,
                          window_length=40):

    df = skeletion_from_archive_cached(file_name)

    if demain:

        def demean(arr):
            num_cols = [
                'w_r', 'x_p', 'x_pix', 'x_r', 'y_p', 'y_pix', 'y_r', 'z_p',
                'z_r'
            ]
            arr[num_cols] = arr[num_cols] - arr[num_cols].mean()
            return arr  #- arr.mean()

        df = df.groupby('sample_id').apply(demean)

    if train_id:

        def add_train_id(df):
            df = df.sort('frame')
            gestures = iter(df.gesture)
            last = gestures.next()
            count = 0
            gest_i = []
            gest_i.append(count)
            for gest in gestures:
                if gest == last:
                    gest_i.append(str(count))
                    last = gest

                else:
                    count += 1
                    gest_i.append(str(count))
                    last = gest
            df['gesture_nr'] = np.array(gest_i)
            return df

        df = df.groupby('sample_id').apply(add_train_id)

    if keep_only_top_40:

        def get_top(arr, n=window_length, column='frame'):
            start_frame = arr.frame.min()
            return arr[(arr.frame < start_frame + n) |
                       (arr.gesture == 'break')]

        # make sure each gesture appears only once in each sequence!!!
        df = df.groupby(df.gesture + df.sample_id + df.gesture_nr,
                        group_keys=False).apply(get_top)

    if drop_lower_joints:
        joints_to_drop = [
            'KneeLeft', 'AnkleLeft', 'FootLeft', 'HipRight', 'KneeRight',
            'AnkleRight', 'FootRight'
        ]

        for joint in joints_to_drop:
            df = df[df.JointType != joint]

    if dummy_gesture:

        def add_dummy_gestures(df,
                               window_length=window_length,
                               window_shift=window_shift):
            start_ = df.frame.min()
            end_ = df.frame.max()
            w_start = start_
            w_end = w_start + window_length
            windows = DataFrame()

            while w_end < end_:
                df_w = df[(df.frame >= w_start) & (df.frame <= w_end)]
                df_w['dummy_gesture'] = w_start
                windows = pd.concat([windows, df_w], axis=0)

                w_start += window_shift
                w_end += window_shift

            return windows

        df = df.groupby('sample_id',
                        group_keys=False).apply(add_dummy_gestures)

    return df
def preprocessed_skeleton(file_name, demain=True, keep_only_top_40=True,
        train_id=True, drop_lower_joints=True, dummy_gesture=False,
        window_shift=1, window_length=40):

    df = skeletion_from_archive_cached(file_name)

    if demain:
        def demean(arr):
            num_cols =  ['w_r', 'x_p', 'x_pix', 'x_r', 'y_p', 'y_pix',
                    'y_r', 'z_p', 'z_r']
            arr[num_cols] = arr[num_cols] - arr[num_cols].mean()
            return arr #- arr.mean()

        df = df.groupby('sample_id').apply(demean)

    if train_id:
        def add_train_id(df):
            df = df.sort('frame')
            gestures = iter(df.gesture)
            last = gestures.next()
            count = 0
            gest_i = []
            gest_i.append(count)
            for gest in gestures:
                if gest == last:
                    gest_i.append(str(count))
                    last = gest

                else:
                    count += 1
                    gest_i.append(str(count))
                    last = gest
            df['gesture_nr'] = np.array(gest_i)
            return df
        df = df.groupby('sample_id').apply(add_train_id)

    if keep_only_top_40:
        def get_top(arr, n=window_length, column='frame'):
            start_frame = arr.frame.min()
            return arr[ (arr.frame < start_frame + n) | (arr.gesture == 'break')]

        # make sure each gesture appears only once in each sequence!!!
        df = df.groupby(df.gesture + df.sample_id + df.gesture_nr,
                group_keys=False).apply(get_top)

    if drop_lower_joints:
        joints_to_drop = ['KneeLeft', 'AnkleLeft',
           'FootLeft', 'HipRight', 'KneeRight', 'AnkleRight', 'FootRight']

        for joint in joints_to_drop:
            df = df[df.JointType != joint]

    if dummy_gesture:
        def add_dummy_gestures(df, window_length=window_length,
                window_shift=window_shift):
            start_ = df.frame.min()
            end_ = df.frame.max()
            w_start = start_
            w_end = w_start + window_length
            windows = DataFrame()

            while w_end < end_:
                df_w = df[(df.frame >= w_start) & (df.frame <= w_end)]
                df_w['dummy_gesture'] = w_start
                windows = pd.concat([windows, df_w], axis=0)

                w_start += window_shift
                w_end += window_shift

            return windows
        df= df.groupby('sample_id', group_keys=False).apply(add_dummy_gestures)

    return df