[Python] How to Properly Implement Sliding Windows for Real-Time Activity Recognition from...

Discussão em 'Python' iniciado por Stack, Abril 12, 2025.

  1. Stack

    Stack Membro Participativo

    We're developing a real-time fall detection system using a wearable sensor (accelerometer and gyroscope) that sends continuous data to Firebase. Our machine learning model was trained on 20-second data windows collected at 80 Hz (total 1600 samples per window). The main challenge is correctly implementing the sliding window logic to continuously collect sensor data and make real-time predictions.

    Our current approach involves continuously polling Firebase for new batches of sensor data, normalizing them, and buffering these samples in a deque with a maximum size of 1600. Once the buffer reaches 1600 samples, we run a prediction and reset the buffer accordingly.

    However, the predictions are inaccurate, and we suspect our approach to sliding windows or buffering might be flawed.

    Here's a simplified snippet illustrating the relevant part of our implementation:

    BUFFER_SIZE = 1600 # 20 sec window at 80 Hz
    STEP_INTERVAL = 5 # Predict every 5 seconds
    data_buffer = deque(maxlen=BUFFER_SIZE)
    last_prediction_time = time.time()

    while True:
    samples = fetch_from_firebase() # fetches new data batch (80 samples per second)

    for sample in samples:
    # Normalize and append each sensor sample to buffer
    reading = np.array([
    sample['ax'], sample['ay'], sample['az'],
    sample['gx'], sample['gy'], sample['gz']
    ]) / 10000.0
    data_buffer.append(reading)

    # Prediction logic (every STEP_INTERVAL seconds, after buffer fills)
    current_time = time.time()
    if len(data_buffer) == BUFFER_SIZE and (current_time - last_prediction_time) >= STEP_INTERVAL:
    window = np.array(data_buffer).reshape(1, -1)
    window_scaled = scaler.transform(window)
    prediction = model.predict(window_scaled)[0]

    last_prediction_time = current_time
    handle_prediction(prediction)

    • Is this the correct way to implement continuous sliding windows for real-time sensor data?
    • Could our current method of handling the buffer or timing intervals be causing prediction inaccuracies?
    • Are there better practices or alternative strategies for buffering
      real-time sensor data effectively for predictions?

    Continue reading...

Compartilhe esta Página