Advertisement
pleasedontcode

ML Code

Aug 14th, 2024
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.58 KB | Source Code | 0 0
  1. #include <Arduino_LSM9DS1.h>
  2. #include <TensorFlowLite.h>
  3. #include <tensorflow/lite/micro/kernels/micro_ops.h>
  4. #include <tensorflow/lite/micro/model.h>
  5. #include <tensorflow/lite/micro/error_reporter.h>
  6. #include <tensorflow/lite/micro/micro_interpreter.h>
  7.  
  8. // Include the TensorFlow Lite model
  9. #include "gesture_model.h"
  10.  
  11. // TensorFlow Lite setup
  12. tflite::MicroErrorReporter micro_error_reporter;
  13. tflite::MicroInterpreter* interpreter;
  14. TfLiteTensor* input;
  15. TfLiteTensor* output;
  16.  
  17. void setup() {
  18.   Serial.begin(115200);
  19.  
  20.   // Initialize the LSM9DS1 sensor
  21.   if (!IMU.begin()) {
  22.     Serial.println("Failed to initialize IMU!");
  23.     while (1);
  24.   }
  25.  
  26.   // Load the model
  27.   static tflite::MicroOpResolver<6> micro_op_resolver;
  28.   micro_op_resolver.AddFullyConnected();
  29.   micro_op_resolver.AddSoftmax();
  30.  
  31.   static tflite::MicroInterpreter static_interpreter(
  32.       gesture_model, micro_op_resolver, tensor_arena, kTensorArenaSize, &micro_error_reporter);
  33.   interpreter = &static_interpreter;
  34.  
  35.   interpreter->AllocateTensors();
  36.   input = interpreter->input(0);
  37.   output = interpreter->output(0);
  38. }
  39.  
  40. void loop() {
  41.   float ax, ay, az;
  42.   if (IMU.accelerationAvailable()) {
  43.     IMU.readAcceleration(ax, ay, az);
  44.    
  45.     // Prepare the input data
  46.     input->data.f[0] = ax;
  47.     input->data.f[1] = ay;
  48.     input->data.f[2] = az;
  49.    
  50.     // Run inference
  51.     interpreter->Invoke();
  52.    
  53.     // Read the result
  54.     float gesture = output->data.f[0];
  55.     Serial.print("Gesture Probability: ");
  56.     Serial.println(gesture);
  57.   }
  58.  
  59.   delay(1000); // Adjust delay as needed
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement