element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
NanoRama
  • Challenges & Projects
  • Project14
  • NanoRama
  • More
  • Cancel
NanoRama
Blog TinyML on Arduino Nano 33 BLE Sense - Fruit Identification
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join NanoRama to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: jomoenginer
  • Date Created: 8 Apr 2020 6:29 AM Date Created
  • Views 5493 views
  • Likes 12 likes
  • Comments 8 comments
  • apds9960
  • tinyml
  • nano 33 ble sense
  • nanoramach
  • imu
  • tensorflow lite
  • arduno
Related
Recommended

TinyML on Arduino Nano 33 BLE Sense - Fruit Identification

jomoenginer
jomoenginer
8 Apr 2020
image

NanoRama

Enter Your Project for a chance to win a Nano Grand Prize bundle for the most innovative use of Arduino plus a $400 shopping cart!

Submit an EntrySubmit an Entry  Back to homepage image
Project14 Home
Monthly Themes
Monthly Theme Poll

 

Overview

In the previous example, the Arduino Nano 33 BLE Sense LSM9DS1 IMU motion, vibration and orientation sensor was used to gather movement data and transform that into a TensorFlow Lite model to identify specific movements.  In this post, the on board APDS9960 Gesture sensor will be used to gather object identification data which will be used to create a TensorFlow Lite model that can be use to identify specific objects; fruit in this case. The proximity sensor feature of the board will be used to identify when an object is near the board and the RGB sensor is used to first collect color data of the objects and then to properly id the objects.  This is a simple example but does show the potential of both TinyML running on a small device as well as the power of the sensor rich Arduino Nano 33 BLE Sense.

 

Related Posts:

TinyML on Arduino Nano 33 BLE Sense - Gesture Recognition

TinyML on Arduino Nano 33 BLE Sense - Person Detection with BLE

 

References:

   Fruit Identification - Arduino and TensorFlow

    https://blog.arduino.cc/2019/11/07/fruit-identification-using-arduino-and-tensorflow/

 

   TinyML Classify objects by color Colab project

   https://colab.research.google.com/github/arduino/ArduinoTensorFlowLiteTutorials/blob/master/FruitToEmoji/FruitToEmoji.ipynb#scrollTo=ykccQn7SXrUX

 

   FruitToEmoji Examples

   https://github.com/arduino/ArduinoTensorFlowLiteTutorials/

     - FruitToEmoji-GIT.ipynb

 

Hardware

  •    Arduino Nano 33 BLE Sense

           https://store.arduino.cc/usa/nano-33-ble-sense

 

Fruitware

  •      Apple
  •      Banana
  •      Orange (Using an Orange Bell Pepper instead)

 

Prepare Arduino IDE

 

  • See Gesture Recognition example posted previously for Arduino Libraries to install


Capturing training data

 

  1. Download the object_color_capture.ino file from the following link to capture the fruit data

      https://create.arduino.cc/editor/TensorFlowExamples/ca761558-13ed-4190-baee-89ced06147c3/preview

 

  2. Load the object_color_capture.ino sketch into the Arduino IDE; or just use the Arduino Create IDE.

 

image

 

3. Open the Serial Monitor and select a fruit to collect data from.

      NOTE:  Ensure the board is close to the fruit and the fruit is well lit for better data collection.

                   Move the board over the fruit or the fruit over the board

    Ex: Banana

image

 

  4. Copy the data from the Serial Monitor and place it in a CSV file.

     Ex:

         banana.csv

         NOTE: Ensure the top line of the file includes the following:

           Red,Green,Blue

 

  5. Repeat for the other fruit.

      NOTE: Press the white Reset button at the top of the Nano 33 BLE Sense to reset the output.

      Apple, Orange (Orange Bell Pepper in this case)

 

image

 

 

Training the model

 

  1. Open the link to the FruitToEmoji-GIT.ipynb Jupyter Notebook to Train the Model

       https://colab.research.google.com/github/arduino/ArduinoTensorFlowLiteTutorials/blob/master/FruitToEmoji/FruitToEmoji.ip…

 

image

 

    NOTE: A Warning may pop up.  Just click RUN ANYWAY.

image

 

  2. Click the Arrow button next to Setup Python Environment to config the environment with the appropriate dependencies.

      Note any errors that are seen which may impact the session.

 

  3. Upload the banana, apple, and orange CSV files data to the Upload Data area in the Left Window Pane.

image

 

Train Neural Network

1. Run the Parse and prepare the data cell from the CSV files to a format that can be used to train the connected neural network

image

 

  2. Run the Build & Train the Model cell to build and train a TensorFlow model using the high-level Keras API.

   Output:

Epoch 394/400 34/34 [==============================] - 0s 2ms/step – loss:
 0.0825 - mae: 0.1781 - val_loss: 0.0611 - val_mae: 0.1435 Epoch 395/400 34/34
 [==============================] - 0s 2ms/step - loss: 0.0834 - mae: 0.1794 -
 val_loss: 0.0620 - val_mae: 0.1445 Epoch 396/400 34/34
 [==============================] - 0s 2ms/step - loss: 0.0826 - mae: 0.1779 -
 val_loss: 0.0606 - val_mae: 0.1426 Epoch 397/400 34/34
 [==============================] - 0s 2ms/step - loss: 0.0823 - mae: 0.1780 -
 val_loss: 0.0554 - val_mae: 0.1357 Epoch 398/400 34/34
 [==============================] - 0s 2ms/step - loss: 0.0830 - mae: 0.1786 -
 val_loss: 0.0575 - val_mae: 0.1385 Epoch 399/400 34/34
 [==============================] - 0s 2ms/step - loss: 0.0826 - mae: 0.1780 -
 val_loss: 0.0558 - val_mae: 0.1359 Epoch 400/400 34/34
 [==============================] - 0s 2ms/step - loss: 0.0820 - mae: 0.1763 -
 val_loss: 0.0616 - val_mae: 0.1437

 

  3. Run with Test Data to put the data into the model and plot the predictions

image

 

4. Convert the Trained Model to Tensor Flow Lite and show model size in bytes.

     Output

        Model is 1768 bytes

 

    Refresh files to view the gesture file that is created:

     File Created: gesture_model.tflite

 

image

 

5. Encode the Model in an Arduino Header File

      This cell will create a byte array into the model.h file.

       NOTE: Previously this was not working but looks like it has been corrected.

 

       Refresh files to see the model.h file.

image

 

Run the TensorFlow Lite Model on the Arduino Nano 33 BLE Sense

 

   1. Download and load the object_color_classifier.ino file into the Arduino IDE or run it from Arduino Create

      https://create.arduino.cc/editor/TensorFlowExamples/8508c70f-5155-4e3b-b982-c5f6bd36ea5c/preview

 

   2. Add a model.h tab to the Arduino IDE Project and add the model.h code from the Colab session into the new file.

     image

 

  3. Compile and Upload to Arduino Nano 33 BLE Sense

 

   4. Open the Arduino IDE Serial Monitor and present a well lit fruit to the Nano sensors to observe the identification data

image

 

  Fruit Emoji Version

 

   1. The same object_color_classifier.ino sketch can be used to print fruit emojis via the serial output but requires some code modifications.

 

     Edit 1

      -  At line 50, change the code to the following:

   

// array to map gesture index to a name
const char* CLASSES[] = {
   u8"\U0001F34E", //"Apple", // Apple
   u8"\U0001F34C", //"Banana",  // Banana
   u8"\U0001F34A" //"Orange"  // Orange
};

 

     Edit 2

       - At line 127, add a carriage return "\r" to the print output:

// output results
 for (int i = 0; i < NUM_CLASSES; i++) {
 Serial.print(CLASSES[i]);
 Serial.print(" ");
 Serial.print(int(tflOutputTensor->data.f[i] * 100));
 Serial.print("%\n\r");
 }
 Serial.println();

 

   2. Connect the Nano to either a MacOS or Linux system to view the emoji output.

       Note: I am not sure how to do this on Windows.

                An Ubuntu 18.04 VM was used for this.

 

   3.  Install emoji fonts and cu.

sudo apt install fonts-emojione
sudo apt-get install cu

  4. Connect the USB connection from the Nano to the VM

image

 

   5. Use 'dmesg' from a terminal to get the Nano tty device

image

 

   6.  Use 'cu' to connect to the Nano and open a serial connection.

cu -l ttyACM0

 

  NOTE: To exit 'cu' use '~.'

 

   7. Present a well lit fruit to the Nano sensor to view the data represented with emojis.

 

image

 

That is pretty much it for this example.

 

This was an interesting example of the use of the on board Arduino Nano 33 BLE Sensors to create a TensorFlow (TinyML) model for object identification. It really shows the power of TinyML on a small device.

 

Next:  Either ArduCam connection or Fruit ID Data over BLE

  • Sign in to reply

Top Comments

  • dubbie
    dubbie over 5 years ago +3
    Jon, Another very interesting example of machine learning. I liked the idea of getting emoji's against each category although I'm not sure how easy it would be to extend to other topics. Maybe you could…
  • ankur608
    ankur608 over 5 years ago +2
    Great article. It was fun to see the variation(a bell pepper) in this case.
  • jomoenginer
    jomoenginer over 5 years ago in reply to dubbie +2
    Dubbie, Thanks. Yeah, I suppose you could look through the list of Emojis and see if one suits your candy need. You would just use the Unicode (Code) values in place of the fruit emojis. https://unicode…
Parents
  • hyemi
    hyemi over 5 years ago

    Thank for very useful posting.

    Why tensor_arena size is 8KB which is much more than model size(1768 byte)?

     

    Thank you in advance.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • hyemi
    hyemi over 5 years ago

    Thank for very useful posting.

    Why tensor_arena size is 8KB which is much more than model size(1768 byte)?

     

    Thank you in advance.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube