Distinguishing Between Flower Species Using Image Recognition, SashiDo's Services, and Flutter App Development

Tags: sashido, flutter, tensorflow, python
Published: 2021-06-30 06:02:44

Step 1: Downloading the dataset
As with any AI project, the first step will be to download and organize the data, which in this case, is images of the following flower species:
-Daisy
-Dandelion
-Rose
-Sunflower
-Tulip
Luckily, there’s a Kaggle notebook with all of these species and all you have to do is download it...here’s how:
a. Visit https://www.kaggle.com/alxmamaev/flowers-recognition 
b. Click on the “Download” button (create an account if needed)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/14ctyyhncp4g01fhcuk9.png)

Unzip the download and make sure you have these folders
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1t2rt5652k5amiko62j2.png)
GREAT JOB! You have successfully downloaded the dataset. Let’s move onto step 2!

Step 2: Setup your Teachable Machine Image Model
Visit https://teachablemachine.withgoogle.com/

Click “Get Started”
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gaoupd8sllt3wzt3ewr9.png)

Click “Image Project” (since we are classifying images)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2m14rhjd41f5lonko3t2.png)

And make sure you see this screen:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gyh3lagqfizfrvfkrhul.png)
Just as a recap, we are classifying between the following 5 different species of flowers: Daisy, Dandelion, Rose, Sunflower, and Tulip. Each class will represent one of these 5 species...so...you guessed it!

Let’s begin by creating 5 classes by clicking the “Add a Class” button 3 times
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vr3nhcl5aa3qur8ijuan.png)

And you should end up with 5 classes
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0hboma9rpn56wqpa88pk.png)

Let’s name each of the classes “Daisy, Dandelion, Rose, Sunflower, and Tulip” to stay organized
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vkylnww6mbdaqwxve8sp.png)

Now drag and drop each of the folders we download earlier in Step 1 into their corresponding classes on Teachable ML
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qg7lou9b3q8wp7ybn21z.png)

This is what you should end up with:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ok9ckuwbjl50aiot7c8.png)
Hit the “Train Model” button to train the model...or…

If you would like to play with the various parameters, click “Advanced” and change the number of epochs, batch size, etc.

Epochs: the number of passes of the entire training dataset the machine learning algorithm has completed
Batch Size: the number of training examples utilized in one iteration
Learning Rate: a tuning parameter in an optimization algorithm that determines the step size at each iteration while moving toward a minimum of a loss function

For example purposes, I will not change any of the parameters and will press train model
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bb2vpxbqjoske4x5al9v.png)
Click Export Model
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5jxsilo30j1gatfr1hko.png)
Click “Tensorflow” and then Download Model
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5ols3as8s1f1x4x1ilm.png)

Step 3: Setting Up The Mobile Application Using The Flutter Framework
For this project, we’re going to use Flutter to develop a mobile application that is compatible with both IOS and Android

Flutter → Flutter SDK is Google's UI toolkit for crafting beautiful, natively compiled applications for mobile, web, and desktop from a single codebase. If you would like to learn more about Flutter, check out the documentation.

There are many YouTube tutorials online on how to make your very own app, but since this tutorial is regarding the integration of SashiDo.io’s amazing services, we will skip that bit and leave it for you!

Here’s a YouTube tutorial by freeCodeCamp.org: https://www.youtube.com/watch?v=pTJJsmejUOQ&ab_channel=freeCodeCamp.org

Begin by creating a Flutter project and setting up the application. For our example, we have set up a login page and this is ultimately what we will be integrating SashiDo.io’s services with using their Parsing functions to allow the user to easily login and out of their accounts.

Ensure that you have an emulator of a chosen device and you are able to run the project on that emulator using the ‘flutter run’ command in terminal. Drag and drop the Tensorflow model’s file you downloaded from step 2 into the project’s directory where it can be referred to.

Step 4: Integrating SashiDo into your project

Download and import the flutter parse server sdk dependency to your project. To connect the application frontend with the SashiDo backend, which can be done why finding the keyApplicationId, keyClientKey, and the parse server url to initialize the connection with the parse server.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9w78smzdid5dano0mgzc.png)
This function can now be added inside the initState function at the beginning of the screen class.

To login the user, this parse server can be initialized with the username and password using the parse server library function.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zyxbkgbrp1cmld63jj79.png)
Simply pass this function when the login button in your application is pressed.

To register the user, the same process can be used by creating a new parse sever user using the username, password, and email as parameters and the signUp function from the parse server library. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/esjtpbrvs0z0136hz178.png)
 
The screen routing functions can be added using the Navigator.push()function on login/register success.
[Optional] To use a Google account for logging in, import the google sign in library [https://pub.dev/packages/google_sign_in] by first installing it in your pubspec.yaml file. Then, go ahead and create a new async function where the Google signIn function can be used to authenticate the user using their valid Google email.
Use the parse server's loginWith function to use google sign in with the scopes as shown below:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9nnlon1fct4ql2gue1a0.png)
The authentication process is now complete!

 
Step 5: Start creating the interface for your application: 

For every flutter project, you want to always use certain basic components such as scaffolds, buttons, containers, etc. In doing so, you must add certain properties such as margins, width & height parameters, and others which you will learn along the way - see below for some screenshots of what we’re talking about!
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/howmbwxqx157m4ckocaj.png)

Let’s dissect the components in this button components!

First, we wrap the component in a container, which allows it act as an individual component on the screen which can be manipulated by margins, since we can’t directly manipulate the RaisedButton with these same properties. The EdgeInsets.only allows us to specify if we only want margins from the top, left, right, or bottom, whereas EdgeInsets.fromLTRB stands for EdgeInsets from left, top, right, and bottom correspondingly. Thereafter, we add a child components such as the RaisedButton.icon as you see in this example. For the button, we add an onPressed() functionality so that an action executes once the user has interacted with the button. Additionally, we have different attributes such as a label which is the text that shows up on the button, an icon that represents what shows up right before the button text, and also a shape, which allows us to control the curvature on the button - here is the finished product!
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9f1otza7qqm65rb9npk.png)

Many of the flutter components follow a similar format, try figuring out this one on your own! 

Step 6: Use a Machine Learning model with application

Add the downloaded machine learning model folder to the project repository inside your assets folder.

Download the TF Lite [https://pub.dev/packages/tflite] flutter package and import it inside the working file. 
Download and import the image_picker [https://pub.dev/packages/image_picker] flutter package in the same working file.

Use the TF Lite package to load the model along with the labels as shown below:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ygfuka3lqb8lc50mvewr.png)

To let the user pick an image from their smartphones, create a pickImage function that uses the image picker package and lets the user select their desired image. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/prs51frtrebi27mdah0p.png)

To run the TF Lite model on the selected image, simply use the Tensorflow runModelOnImage function to output the plant type as shown below: 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vpgs8b4l10l6wl7alwrp.png)

This function can now be called inside the pickImage and passing the directory of picked image as a parameter, just like how it’s done below:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qtjo6n1ybf20jto9fxxp.png)

Finally, load the model inside the initState function of the class and pass in the pickImage function when the button is pressed in your frontend of the application to connect everything together.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xny8w4sbutcrewilzn0v.png)
 

THE FINISHED PRODUCT:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6q2hgeoxisl3ekig8qhs.png)