Python Forum
Human Activity recognition in real time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Human Activity recognition in real time
#1
Hi everyone, i have followed a youtube tutorial to build a model for human activity recognition using accelerometer.
In my model i used both accelerometer and gyroscope, so i have 6 features.
I'm trying to use the model for real time activity recognition. How? I have a loop with a kafka consumer which fills a dictionary, and for each client connected when the number of measurements reach a specific amount (80, 4 seconds) a make_prediction function gets called, (this function calls model.predict_classes).
My problem is: if i collect a dataset ad i use the model for prediction i get a very good accuracy, about 90-95%, when i try to predict activity in real time my model doesn't work.
This is the relevant code:
Code I used to train the model:
X = balanced_data[['acc_x', 'acc_y', 'acc_z', 'gy_x', 'gy_y', 'gy_z']]
y = balanced_data['label']
scaler = StandardScaler()
scaler.fit(X)
X=scaler.transform(X)
scaled_X = pd.DataFrame(data = X, columns = ['acc_x', 'acc_y', 'acc_z', 'gy_x', 'gy_y','gy_z'])
scaled_X['label'] = y.values
scaled_X.head()

Fs = 20
frame_size = Fs*4 # 80
hop_size = Fs*2 # 40

def get_frames(df, frame_size, hop_size):

    N_FEATURES = 6

    frames = []
    labels = []
    for i in range(0, len(df) - frame_size, hop_size):
        acc_x = df['acc_x'].values[i: i + frame_size]
        acc_y = df['acc_y'].values[i: i + frame_size]
        acc_z = df['acc_z'].values[i: i + frame_size]
        gy_x = df['gy_x'].values[i: i + frame_size]
        gy_y = df['gy_y'].values[i: i + frame_size]
        gy_z = df['gy_z'].values[i: i + frame_size]
        
        # Retrieve the most often used label in this segment
        label = stats.mode(df['label'][i: i + frame_size])[0][0]
        frames.append([acc_x, acc_y, acc_z, gy_z, gy_y, gy_z])
        labels.append(label)

    # Bring the segments into a better shape
    frames = np.asarray(frames).reshape(-1, frame_size, N_FEATURES)
    labels = np.asarray(labels)

    return frames, labels

X, y = get_frames(scaled_X, frame_size, hop_size)

X.shape, y.shape
X, y = shuffle(X, y, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0, stratify = y)
x_train_bf = X_train
X_train = X_train.reshape(len(X_train), 80, 6, 1)
X_test = X_test.reshape(len(X_test), 80, 6, 1)
I get a val_accuracy of 96-98%.
Now this is how i predict the activity in real time:

       if len(prediction_sockets[key])>=prediction_sockets_index[key] + 80:
                window = prediction_sockets[key][prediction_sockets_index[key]:prediction_sockets_index[key]+80]
                result = await make_prediction(window)
                print(str(result)   +str(key)+ '\n\n\n\n\n')
                prediction_sockets_index[key]+=40

#make_prediction function 
def make_prediction(window):

  
    frames=[]
    for line in window:
        frames.append([float(line[1]),float(line[2]),float(line[3]),float(line[5]),float(line[6]),float(line[7])])
    #lines:['user', 'acc_timestamp','acc_x','acc_y','acc_z','gy_timestamp','gy_x','gy_y','gy_z']
    cols=['acc_x', 'acc_y', 'acc_z', 'gy_x', 'gy_y', 'gy_z']
    data = pd.DataFrame(data = frames, columns = cols)
    scaler_2 = ML.scaler #ML is the file where i trained the model, i'm using the same scaler.
    window_scaled=scaler_2.transform(data)
    scaled_data = pd.DataFrame(data = window_scaled, columns = cols)
    frame_pp=get_frames_2(scaled_data,80,40)
    re_frame = frame_pp.reshape(len(frame_pp),80,6,1)
    return model.predict_classes(re_frame)

def get_frames_2(df, frame_size, hop_size):

    N_FEATURES = 6

    frames = []
    for i in range(0, len(df) - frame_size, hop_size):
        acc_x = df['acc_x'].values[i: i + frame_size]
        acc_y = df['acc_y'].values[i: i + frame_size]
        acc_z = df['acc_z'].values[i: i + frame_size]
        gy_x = df['gy_x'].values[i: i + frame_size]
        gy_y = df['gy_y'].values[i: i + frame_size]
        gy_z = df['gy_z'].values[i: i + frame_size]
        frames.append([acc_x, acc_y, acc_z, gy_x,gy_y,gy_z])
    # Bring the segments into a better shape
    frames = np.asarray(frames).reshape(-1, frame_size, N_FEATURES)
As you can see i'm pre-processing the data exactly the same way when i train my model and when i use for real-time recogniton.
Even if i collect a dataset and i try to make prediction on it, using the function get_frames_2 to process the data, the model works.
But when i use i for real time prediction, i don't get the correct result, i get Walking label every time.
Can someone help me please? Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can draw a real-time marker on map using folium/leaflet Jupyter notebook C3PO 0 2,412 Dec-22-2020, 07:04 PM
Last Post: C3PO
  Real-time plot from serial port Nochill_Senpai 1 5,451 Feb-12-2020, 03:04 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020