I want to upload a pre-trained model for audio classification on an Arduino Nano 33 BLE.
I want to receive audio from its built-in microphone, put it in a tensor, let the model process it and have the output of its inference.
I am beginning to work on it based on this two codes: https://docs.arduino.cc/tutorials/nano-33-ble-sense/microphone-sensor and https://docs.arduino.cc/tutorials/nano-33-ble-sense/get-started-with-machine-learning. I have the model saved in model.h format.
When I run the following code, that does nothing but it is just the first step for have the communication with the microphone set and the ML variables defined, if #include <model.h> is uncommented, it returns me the following error, otherwise not. Why!?
#include <PDM.h>
#include <TensorFlowLite.h>
#include <tensorflow/lite/micro/all_ops_resolver.h>
#include <tensorflow/lite/micro/micro_error_reporter.h>
#include <tensorflow/lite/micro/micro_interpreter.h>
#include <tensorflow/lite/schema/schema_generated.h>
#include <tensorflow/lite/version.h>
#include <model.h>
// buffer to read samples into, each sample is 16-bits
short sampleBuffer[256];
// number of samples read
volatile int samplesRead;
// global variables used for TensorFlow Lite (Micro)
tflite::MicroErrorReporter tflErrorReporter;
// pull in all the TFLM ops, you can remove this line and
// only pull in the TFLM ops you need, if would like to reduce
// the compiled size of the sketch.
tflite::AllOpsResolver tflOpsResolver;
const tflite::Model* tflModel = nullptr;
tflite::MicroInterpreter* tflInterpreter = nullptr;
TfLiteTensor* tflInputTensor = nullptr;
TfLiteTensor* tflOutputTensor = nullptr;
// Create a static memory buffer for TFLM, the size may need to
// be adjusted based on the model you are using
constexpr int tensorArenaSize = 8 * 1024;
byte tensorArena[tensorArenaSize] __attribute__((aligned(16)));
// array to map gesture index to a name
const char* GESTURES[] = {
"punch",
"flex"
};
void setup() {
Serial.begin(9600);
while (!Serial);
// configure the data receive callback
PDM.onReceive(onPDMdata);
// optionally set the gain, defaults to 20
// PDM.setGain(30);
// initialize PDM with:
// - one channel (mono mode)
// - a 16 kHz sample rate
if (!PDM.begin(1, 8000)) {
Serial.println("Failed to start PDM!");
while (1);
}
// get the TFL representation of the model byte array
//tflModel = tflite::GetModel(bees_model);
//if (tflModel->version() != TFLITE_SCHEMA_VERSION) {
// Serial.println("Model schema mismatch!");
// while (1);
//}
// Create an interpreter to run the model
//tflInterpreter = new tflite::MicroInterpreter(tflModel, tflOpsResolver, tensorArena, tensorArenaSize, &tflErrorReporter);
// Allocate memory for the model's input and output tensors
//tflInterpreter->AllocateTensors();
// Get pointers for the model's input and output tensors
//tflInputTensor = tflInterpreter->input(0);
//tflOutputTensor = tflInterpreter->output(0);
}
void loop() {
// wait for samples to be read
if (samplesRead) {
// print samples to the serial monitor or plotter
for (int i = 0; i < samplesRead; i++) {
Serial.println(sampleBuffer[i]);
// check if the sound value is higher than 500
if (sampleBuffer[i]>=500){
digitalWrite(LEDR,LOW);
digitalWrite(LEDG,HIGH);
digitalWrite(LEDB,HIGH);
}
// check if the sound value is higher than 250 and lower than 500
if (sampleBuffer[i]>=250 && sampleBuffer[i] < 500){
digitalWrite(LEDB,LOW);
digitalWrite(LEDR,HIGH);
digitalWrite(LEDG,HIGH);
}
//check if the sound value is higher than 0 and lower than 250
if (sampleBuffer[i]>=0 && sampleBuffer[i] < 250){
digitalWrite(LEDG,LOW);
digitalWrite(LEDR,HIGH);
digitalWrite(LEDB,HIGH);
}
}
// clear the read count
samplesRead = 0;
}
}
void onPDMdata(void) {
// query the number of bytes available
int bytesAvailable = PDM.available();
// read into the sample buffer
PDM.read(sampleBuffer, bytesAvailable);
// 16-bit, 2 bytes per sample
samplesRead = bytesAvailable / 2;
}
The error is:
ML_arduino_bees.ino: In function 'void setup()':
ML_arduino_bees.ino:52:17: error: 'onPDMdata' was not declared in this scope
PDM.onReceive(onPDMdata);
^~~~~~~~~
ML_arduino_bees.ino:52:17: note: suggested alternative: 'model_data'
PDM.onReceive(onPDMdata);
^~~~~~~~~
model_data
exit status 1
Compilation error: 'onPDMdata' was not declared in this scope