Maker.io main logo

Real-time Food Quality Prediction

2022-08-25 | By Alex Miller

License: Attribution

Things used in this project:


Hardware components
Arduino Mega 2560 ×1
Arm Cortex M0 ×1

Software apps and online services
Neuton Tiny ML

Story

 

 

Copy Code
static NeuralNet neuralNet = { 0 };
extern const unsigned char model_bin[];
extern const unsigned int model_bin_len;
uint8_t app_init()
{
return (ERR_NO_ERROR != CalculatorInit(&neuralNet, NULL));
}
inline Err CalculatorOnInit(NeuralNet* neuralNet)
{
memUsage = sizeof(*neuralNet);
app_reset();
timer_init();
return CalculatorLoadFromMemory(neuralNet, model_bin, model_bin_len, 0);
}

Here, create an object NeuralNet and call a function for loading the model located in the file model.c

 

Copy Code
CalculatorLoadFromMemory(neuralNet, model_bin, model_bin_len, 0);
Copy Code
inline float* app_run_inference(float* sample, uint32_t size_in, uint32_t *size_out)
{
if (!sample || !size_out)
return NULL;
if (size_in / sizeof(float) != app_inputs_size())
return NULL;
*size_out = sizeof(float) * neuralNet.outputsDim;
if (app.reverseByteOrder)
Reverse4BytesValuesBuffer(sample, app_inputs_size());
return CalculatorRunInference(&neuralNet, sample);
}
Copy Code
inline void CalculatorOnInferenceResult(NeuralNet* neuralNet, float* result)
{
if (neuralNet->taskType == TASK_BINARY_CLASSIFICATION && neuralNet->outputsDim >= 2)
{
float* value = result[0] >= result[1] ? &result[0] : &result[1];
if (*value > 0.5)
{
if (value == &result[0])
{
led_green(1);
led_red(0);
}
else
{
led_green(0);
led_red(1);
}
}
else
{

 

 

 

 

 

 
Here are the results for another row of data. In this case, we see that the model predicted that the food is spoiled, as the predicted class is one, which indicates “spoiled food”. The prediction was also made very fast, in 3848 microseconds, with the same 199 kB of Flash memory usage and 136 kB of RAM usage. In this case, you can see the red LED, indicating that the food is spoiled.
 
Conclusion:
 
 
 
制造商零件编号 A000067
ARDUINO MEGA2560 ATMEGA2560
Arduino
¥334.88
Details
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum