DEV Community

Cover image for How to load a Teachable Machine image model in a Node.Js project
Vesi Staneva for SashiDo.io

Posted on • Edited on • Originally published at blog.sashido.io

How to load a Teachable Machine image model in a Node.Js project

It’s no secret Node.js is one of the most widespread frameworks for writing apps nowadays. Though with the rise of Cloud-based Machine learning popularity, finding ways to stay on top of the trends by adding machine learning-based functionalities is becoming vital for Node.js developers and the success of their apps.

If you’re one of those app creators banging their heads about how to introduce machine learning to your Node.js project, let me give you a break by saying that the opportunity to train Machine Learning models is not a reserved space only for experienced Data Scientist and ML engineers anymore. Google’s Teachable Machine tool empowers you to create and export your own model in the browser without any prior machine learning experience needed.

In the following lines, I’ll show you how to train and load an Image classification model to your Node.js application in less than 10 minutes.

Content:
1. Machine learning for dummies - It's that simple!
2. Train a Machine Learning Model with Teachable Machine
3. Export to Tensorflow.js
4. Install NPM @sashido/teachablemachine-node
5. Usage examples
5.1 Plain Node.Js
5.2 Express
8. Conclusion

Machine learning for dummies - It's that simple!

To be absolutely sure this approach is fit for machine learning beginners, I’ve personally put the simplicity of Google's Teachable Machine to the ultimate test with my mom who has no idea what machine learning is. It was not only fun, but she didn’t have any issue building a powerful image classification model that recognizes her cat amongst a bunch of other photos with very little data. 🐈 So if you have any doubts about the complexity of training a model, have no fear - Teachable Machine makes this complex task a piece of cake!

So far, so good. Now you probably wonder how to load your model into your project and make it work for you. Well, our team decided to jump in and offer an easy and open-source solution for serving machine learning models in a live environment. Et, voila - @sashido/teachablemachine-node is here to save the day and help you utilize a Teachable Machine image model into a Node.Js project out of the box. Simple, right?

Train a Machine Learning Model with Teachable Machine

Currently, the teachablemachine-node recognizes only image models. After a thorough analysis of the field, we figured that the greatest demand is for training image models and decided that’s the place to start. Moreover, image models serve well in many cases, such as object detection(a trendy machine learning example in real life is this fun Mask detection project), object recognition, image moderation and so many more.

Gathering samples is with no doubt the fundamental first step from training a model. You can use some open datasets, free photo platforms, own data, or simply use your PC's camera to collect data. Teachable Machine allows both ways - you can load preselected images or directly use your camera to collect data.

ezgif.com-gif-maker--1-

Split the photos into as many classes, as you need. Once done, hit the ‘Train Model’ button and leave it to the Teachable Machine. Training might take a while, depending on the dataset volume. Have patience and do not switch the tab. 😊

train_model_tm

For further insight on your model’s performance click the Advanced option

Export to Tensorflow.js

Once your model is trained and you’re satisfied with its accuracy, it’s time to export it. Make sure that you select Tensorflow.js format when exporting. That way your model will be uploaded (for free) and you will receive an access URL.

export_tendorflow.js

Next, you’ll use this shareable link to load the model into your Node.js project.

Install @sashido/teachablemachine-node

Simple as it can be, just install using npm or yarn:

Install using npm

npm install @sashido/teachablemachine-node

Install using yarn

yarn add @sashido/teachablemachine-node

Usage examples

Our @sashido/teachablemachine-node makes loading an image model trained with Teachable Machine in your Node.Js project a child’s play.

Plain Node.Js

Just with a few lines of code, the Teachable Machine model is loaded into your project. Simply invoke classify and handle the predictions.

const TeachableMachine = require("@sashido/teachablemachine-node");

const model = new TeachableMachine({
  modelUrl: "https://teachablemachine.withgoogle.com/models/r6BBk-hiN/"
});

model.classify({
  imageUrl: "https://media-blog.sashido.io/content/images/2020/09/SashiDo_Dog.jpg",
}).then((predictions) => {
  console.log("Predictions:", predictions);
}).catch((e) => {
  console.log("ERROR", e);
});
Enter fullscreen mode Exit fullscreen mode

Express

As easy as the example above, you can also load your model in an Express app.

const express = require("express");
const TeachableMachine = require("@sashido/teachablemachine-node");

const model = new TeachableMachine({
  modelUrl: "https://teachablemachine.withgoogle.com/models/r6BBk-hiN/"
});

const app = express();
const port = 3000;

app.get("/image/classify", async (req, res) => {
  const { url } = req.query;

  return model.classify({
    imageUrl: url,
  }).then((predictions) => {
    console.log(predictions);
    return res.json(predictions);
  }).catch((e) => {
    console.error(e);
    res.status(500).send("Something went wrong!")
  });
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});
Enter fullscreen mode Exit fullscreen mode

Conclusion

In the last decade, Machine Learning has matured from a research field to applied business solutions. Luckily there’s no need to wait for another decade to upgrade your project with some cool machine learning-based features - Teachable Machine gives you an opportunity to flatten the learning curve and easily train Machine Learning models of your own.

And now with the @sashido/teachablemachine-node you also have a fast and simple way to load any image Teachable Machine model to your Node.js application. Yes, it is that simple!

Check the Awesome Teachable Machine list, full of fantastic learning and inspiration resources, detailed tutorials, and useful tools, that will help you bring your creative ideas to life!

In the long run, our RoadMap includes support for Pose Models, Audio Models, Gifs, and Videos, so you can train and load all kinds of Teachable Machine Models.

Don’t be shy and let us know which's the one you want to use first and we’ll take care of the rest. We’d love to chat, just write to us at hello@sashido.io.

Happy Coding!

Deploy ML models into production in 59 seconds & serve a million predictions for FREE with TeachableHub. Get Early Access !

Top comments (0)