在 python 中使用 tflite 模型从 arduino nano 33 ble 读取加速度计和陀螺仪数据并预测所做的手势

问题描述 投票:0回答:0

我已经使用加速度计训练了 5 个手势,在下载转换后的 tflite 模型供使用后,我遇到了无法调整输入数据大小以传递给模型的错误消息。

import serial
import numpy as np
import tensorflow.lite as tflite

# Open the serial port to read accelerometer data from the Arduino
ser = serial.Serial('COM3', 9600)

# Load the TensorFlow Lite model
interpreter = tflite.Interpreter(model_path="gesture_model.tflite")
interpreter.allocate_tensors()

# Get the input and output tensors
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Create a buffer to store the accelerometer data
buffer = []

while True:
    # Read accelerometer data from the Arduino
    line = ser.readline().decode().strip()
    accel_data = list(map(float, line.split(',')))

    # Filter out readings below 2.5g
    if np.linalg.norm(accel_data) < 2.5:
        continue

    # Add the accelerometer data to the buffer
    buffer.append(accel_data)
    print(line)
    # If the buffer is full, process the data
    if len(buffer) == 100:
        # Reshape the data to (1, 100)
        accel_data = np.array(buffer).reshape(1, 100)

        # Preprocess the accelerometer data
        accel_data = (accel_data - np.mean(accel_data)) / np.std(accel_data)

        # Set the input tensor with the preprocessed accelerometer data
        interpreter.set_tensor(input_details[0]['index'], accel_data.astype(np.float32))

        # Run inference and get the output tensor
        interpreter.invoke()
        output_data = interpreter.get_tensor(output_details[0]['index'])

        # Get the predicted gesture from the output tensor
        predicted_gesture = np.argmax(output_data)

        # Print the predicted gesture
        print("Predicted gesture: ", predicted_gesture)

        # Clear the buffer
        buffer = []

数据格式为aX,aY,aZ,gX,gY,gZ。我试图将它存储在缓冲区中并将其调整为大小为 1:100 的数组,但它不起作用。

arduino accelerometer gesture tensorflow-lite tflite
© www.soinside.com 2019 - 2024. All rights reserved.