It's celebration time π. We are just done with the fabulous Christmas π and waiting for the new year bell to ring. Hashnode's Christmas Hackathon is also going strong with many of the enthusiasts are building something cool and writing about it.
There shouldn't be any excuse to stay away from it. So, I thought of building something cool(at least, my 7 years old daughter thinks it is π) while learning about a bit of Machine Learning. So what is it about?
I've borrowed all the Disney Princess dolls from my daughter to build a Machine Learning model such that, an application can recognize them with confidence using a webcam. I have given it a name too. The app is called, Princess Finder. In this article, we will learn the technologies behind it along with the possibilities of extending it.
The Princess Finder
The Princess Finder app is built using,
The Teachable Machine: How about an easy and fast way to create machine learning models that you can directly use in your app or site? The Teachable Machine allows you to train a computer with images, sounds, and poses. We have created a model using the Disney princess so that, we can perform an Image Classification by using it in our app.
ml5.js: It is machine learning for the web using your web browser. It uses the web browser's built-in graphics processing unit (GPU) to perform fast calculations. We can use APIs like, imageClassifier(model), classify, etc. to perform the image classification.
React: It is a JavaScript library for building user interfaces. We can use ml5.js in a React application just by installing and importing it as a dependency.
Here is a snap from the app shows, it is 93% confident that the princess is Jasmine. It also marks it with a golden ring.
Whereas, there is no way I look like a Disney Princess(not even a doll). Hence my own image has been classified correctly saying, No Dolls.
Here is a quick demo with lots of excitements,
A Few Terminologies
If you are a newbie to Machine Learning, you may find some of the terminologies a bit overwhelming. It is better to know the meaning of them at a high level to understand the usages.
Field
Type
Model
A representation of what an ML system has learned from the training.
Training
It is a process of building a Machine Learning model. Training is comprised of various examples to help to build the model.
Examples
One row of a dataset helps in training a model. An example consists of the input data and a Label.
Label
In Supervised Learning, the possible result of an example is called, Label.
Supervised Learning
Supervised machine learning is about Training a model using the input data and the respective Label.
Image Classification
It is a process to classify objects, patterns in an image.
Our Princess Finder app uses the Supervised Machine learning where we have trained the model with lots of examples of princesses pictures. Each of the example data also contains a label to identify a particular princess by the name.
Teachable Machine
We can create the ML models with a few simple steps using the Teachable Machine user interfaces. To get started, browse to this link. You can select either an image, sound, or pose project. In our case, it will be an image project.
Next, we need to define the classifications by selecting the examples(the images and labels). We can either use a webcam to take the snaps or can upload the images.
We start the training once the examples are loaded. This is going to create a model for us.
After the training is complete, you can test the model with the live data. Once satisfied, you can export the model to use it in an application.
Finally, we can download the model to use it in our app. You can optionally upload the model to the cloud to consume it using a URL. You can also save the project to google drive.
If you are interested to use or extend the model I have created, you can download and import it into the Teachable Machine interface.
User Interface using ml5.js and React
So we have a model now. We will use the ml5.js library to import the model and classify the images using the live stream. I have used React as I am most familiar with it. You can use any UI library, framework, or vanilla JavaScript for the same. I have used the create-react-app to start the skeleton of my app up and running within a minute.
Install the ml5.js dependency,
# Or, npm install ml5
yarn add ml5
Unzip the model under the public folder of the project. We can create a folder called, model under the public and extract the files.
Use the ml5.js library to load the model. We will use the imageClassifier method to pass the model file. This method call returns a classifier object that we will use to classify the live images in a while. Also note, once the model is loaded successfully, we initialize the webcam device so that we can collect the images from the live stream.
Next, we call the classify() method on the classifier to get the results. The results is an array of all the labels with the confidence factor of a match.
We should use the classify method call in a specified interval. You can use a React hook called, useInterval for the same. The results array may look like this,
Please find the complete code of the App.js file from here. That's all, you can now use this result array to provide any UI representation you would like to. In our case, we have used this results array in two React components,
List out the Princess and Highlight the one with the maximum match
<Princessdata={result}/>
Show a Gauge chart to indicate the matching confidence.
<Chartdata={result[0]}/>
The Princess component loops through the results array and render them along with using some CSS styles to highlight one.
The `princess-finder` is a fun app to use a bit of machine learning in the browser. This app was built as part of the Hashnode's #christmashackathon.
Princess Finder
I've borrowed all the Disney Princess dolls from my daughter to build a Machine Learning model such that, an application can recognize them with confidence using a webcam. I have given it a name too. The app is called, Princess Finder.
Technologies Used
The Princess Finder app is built using,
The Teachable Machine: How about an easy and fast way to create machine learning models that you can directly use in your app or site? The Teachable Machine allows you to train a computer with images, sounds, and poses. We have created a model using the Disney princess so that, we can perform an Image Classification by using it in our app.
ml5.js: It is machine learning for the web using your web browser. It uses the web browser's built-in graphics processing unit (GPU) to perform fast calculations. We can use the API like, imageClassifier(model)β¦
Hope you find the article insightful. Please π like/share so that it reaches others as well. Let's connect. Feel free to DM or follow me on Twitter(@tapasadhikary). Have fun and wish you a very happy 2021 ahead.