How I attempted Image Classification in the browser using ml5.js and React

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
Search for a command to run...

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
No comments yet. Be the first to comment.
Traditional SSR is great, but it has a massive buffering problem. Let's look into the SSR Streaming and learn how it could be a game changer.

Learn how to build maintainable, reusable forms in React using design patterns like custom hooks, compound components, and context for scalable CRUD

Learn how JavaScript variables work, including let vs const, primitive and reference types, pass by value, and how JavaScript stores data in memory.

A beginner-friendly introduction to JavaScript covering what it is, how it works, and how to set up your JavaScript environment

Learn how the promises are handled internally using event loop. This concept is essentials to debug and work with async programming.

GreenRoots Blog - A Blog by Tapas Adhikary
186 posts
Educator | YouTuber | Building ReactPlay | Let's Connect
Since my Machine Learning Journey had started, I am very much lost into the crowd of Python , TensorFlow, Understanding Deep Learning and Neural Network.
While all these are pure fun and intense learning, I was missing something similar to run in the browser. Additionally, there is a huge side of Machine Learning(ML) use-case where an individual doesn't have to learn all the underlying facts of ML rather, can use all the hard work done by others to do something cool. Here is one such we will be seeing in next 3-4 minutes.
Relax, as a reader of this story you do not need any prior experience in Machine Learning or anything related to it. Keep Reading, you are going to like it.
Image Classification is a machine learning problem where we define a set of targets(objects like, Car, Cat, Dog, Boat etc. to identify in the images ), train a model to recognized them using labeled example(sample) photos.
To understand this more easier way,
Training.Labels.Labeled Examples.Machine Learning Algorithm.Model.
Image Classification of Car, Cat, Boat, Dog objects
With the knowledge gained so far, let us dive into the problem in hand. We will be building a web app to,

Also, we will least worry about how the model gets created, what algorithm runs at the back to achieve it etc. These are important but not so much for the use-case we are focusing on here.
All excited about it? Let's see it in action: ml-greenroots-info
There are plenty of options out there to try JavaScript based ML library or tool-kit to do Machine Learning in the browser. Some are to solve basic problems and few are advanced like, TensorFlow.JS.
Here are some key steps that will help to set up ml5.js with react and get started with it:
ml5.js by installing it using npm npm i ml5
ml5.js into your React component import * as ml5 from "ml5";
ml5.js, you can make use of various open source Model providers like, MobileNet, YOLO etc. without worrying too much about the underlying facts. For this project I am using, MobileNet. // Initialize the Image Classifier method with MobileNet
const classifier = ml5.imageClassifier('MobileNet', modelLoaded);
// When the model is loaded
function modelLoaded() {
console.log('Model Loaded!');
}
As you see above, we get a classifier and also handle a callback when the model is successfully loaded from MobileNet.Once we have the classifier, next action is to Classify the Image and perform Prediction. This is going to be super easy! Have a look into this code:
// Make a prediction with a selected image
classifier.predict(image, 3, function(err, results) {
if(err) {
console.log(err);
}
return results;
}).then((results) => {
// set the prediction in state and off the loader
this.setLoader(false);
this.setPredictions(results);
});
Neat, right? We call the predict method by passing the image, number of predictions required(3, in this case) and a call-back to handle once the Prediction is done.
Once the Prediction is done, we are just setting some state in React component and using it for rendering. That's all about the Machine Learning part of the code. Rest of it is, just the react component related methods and styles.
You can check out the code in my GitHub repository :
This project is based on GatsbyJS and got a build-deploy-workflow hook established with Netlify. Please feel free to fork and send pull requests, if you have any great modifications or suggestions to the code.
You can also add your own components to do execute ML use-cases in the browser. Thank you!
This blog post will not be complete without mentioning some of the Caveats:
Machine Learning in the Browser is picking up. However the performance of the ML Algorithms in the browser will not be that great. One should decide to use it based on the use-case in hand and the target persona in mind.
With the availability of the tools and libraries supporting it, we are all encouraged to try it out. To me, a project like this is an entry point to learn Machine Learning deeper fold-by-fold, layer-by-layer.
If you access the github repo mentioned above, you will find a file called ML5ImageClassification.js which is the React component doing all that magic.
However this might not be the best code you would see and, that is intentional. Because I am planning to re-write the code using react hooks to showcase, how an existing react project can be modified with hook easily and effectively✨✨✨. Stay Tuned!
If you have read through and reached so far, Great! Thank You very much. Hope you liked the post. Please comment and share.