Tutorial

How to do Custom Image Classification (AutoML) Using JavaScript

This guide explains how to use Eden AI's API in JavaScript for custom image classification. It covers five stages: creating a project, uploading data, training a model, running a classification job, and retrieving results, with concise code examples using Axios.

How to do Custom Image Classification (AutoML) Using JavaScript
TABLE OF CONTENTS

AutoML image classification enables you to create custom machine learning models for categorizing images without requiring deep technical knowledge.

With Eden AI, you can easily leverage its user-friendly API to build a custom image classifier tailored to your needs.

This guide will walk you through the process of implementing AutoML image classification using JavaScript.

What is Custom Image Classification?

Custom Image Classification with AutoML allows you to automatically create and train a machine learning model to classify images into your own specific categories.

Using an AutoML platform, you upload labeled images, and the tool automatically selects and tunes the best model to recognize and categorize new images based on your custom labels.

This process simplifies machine learning, making it accessible even to those without deep expertise in AI.

Implementing Custom Image Classification in Python

Get Access to Eden AI API

1. Create an Account: Visit Eden AI and sign up for an account. Once registered, navigate to the API section to obtain your API key. This key will give you access to a wide range of AI services including face comparison.

2. Navigate to Image Technologies – After logging in, go to the image section of the platform.

3. Select Custom Image Classification – Choose the Custom Image Classification feature or explore advanced options based on your needs.

Overview of the Process

The process is split into five stages to allow for clear, logical steps that ensure all parts of the project (creation, data uploading, training, prediction, and result retrieval) are managed separately.

This also helps avoid potential issues related to data size or processing time, as the operations are asynchronous.

  1. Creating a Project: This sets up the environment to store your data and model.
  2. Uploading Data: You need to upload and label images for model training.
  3. Training the Model: This is the phase where the model learns from the data.
  4. Launching a Classification Job: Once the model is trained, you can use it to classify new images.
  5. Getting Results: Finally, you fetch the predictions made by the trained model.

Getting Started: Installing JavaScript's Request Module

To interact with Eden AI's API, we'll be using Axios in JavaScript for sending HTTP requests. If you're working in a Node.js environment, you can install Axios using the following command:


npm install axios

Step 1: Creating a Project

The first step is to create a project, which will serve as a container for your images and classification settings.


import requests

headers = {"Authorization": "Bearer 🔑 Your_API_Key"}

url = "https://api.edenai.run/v2/image/automl_classification/create_project/"

payload = {
    "providers": [["nyckel"]],  # Specify the AI provider
    "name": "Your_project_name"  # Name your project
}

response = requests.post(url, headers=headers, json=payload)
result = response.json()
print(result)

What this does:

  • providers: Specifies which AI provider to use (in this case, “nyckel”).
  • name: The name of the project you're creating.
  • The response will return a project_id, which you will use later in the other stages.

Step 2: Uploading Your Data

Next, you need to upload your image data to Eden AI. This step involves specifying the label for the image, the project ID, and the image file itself.


const axios = require("axios").default;
const fs = require("fs");
const FormData = require("form-data");

const form = new FormData();
form.append("providers", "nyckel");
form.append(
    "file",
    fs.createReadStream("🖼️ path/to/your/image.png")
);
form.append("type_of_data", "TEST");
form.append("label", "🏷️ Label of your image");
form.append("project_id", "🆔 ID of your project");

const options = {
    method: "POST",
    url: "https://api.edenai.run/v2/image/automl_classification/upload_data_async",
    headers: {
        Authorization: "Bearer 🔑 Your_API_Key",
        "Content-Type": "multipart/form-data; boundary=" + form.getBoundary(),
    },
    data: form,
};

axios
.request(options)
    .then((response) => {
        console.log(response.data);
    })
    .catch((error) => {
        console.error(error);
    });

What this does:

  • type_of_data: Specifies whether the data is for training or testing. You can use “TRAIN” for training data and “TEST” for test data.
  • label: This is the label for the image, such as “dog” or “cat”.
  • file: The image you’re uploading for classification. Make sure to provide the correct path to the image.

Step 3: Training the Model

Once your data is uploaded, you need to train the model using the uploaded data. This step uses the project_id to start the training process.


const axios = require("axios").default;
const options = {
    method: "POST",
    url: "https://api.edenai.run/v2/image/automl_classification/train_async",
    headers: {
        authorization: "Bearer 🔑 Your_API_Key",
    },
    data: {
        providers: "nyckel",
        project_id: "🆔 ID of your project",
    },
};
axios
    .request(options)
    .then((response) => {
        console.log(response.data);
    })
    .catch((error) => {
        console.error(error);
    });

What this does:

  • train_async: This sends the request to train the model using the uploaded data.
  • The project_id links the request to your project.

Step 4: Launching a Classification Job

After training, you can use the model to classify new images. This step sends the image to your trained model for classification.


const axios = require("axios").default;
const fs = require("fs");
const FormData = require("form-data");

const form = new FormData();
form.append("providers", "nyckel");
form.append(
    "file",
    fs.createReadStream("🖼️ path/to/your/image.png")
);
form.append("project_id", "🆔 ID of your project");

const options = {
    method: "POST",
    url: "https://api.edenai.run/v2/image/automl_classification/predict_async",
    headers: {
        Authorization: "Bearer 🔑 Your_API_Key",
        "Content-Type": "multipart/form-data; boundary=" + form.getBoundary(),
    },
    data: form,
};

axios
.request(options)
    .then((response) => {
        console.log(response.data);
    })
    .catch((error) => {
        console.error(error);
    });

What this does:

  • predict_async: This endpoint sends the image to the trained model for classification.
  • file: The image file to classify is sent in this request.

Step 5: Getting the Results

Finally, once the classification job has been processed, you can retrieve the results using the public_id from the previous classification job response.


import requests

headers = {"Authorization": "Bearer 🔑 Your_API_Key"}

url = "https://api.edenai.run/v2/image/automl_classification/predict_async/<public_id>"

response = requests.get(url, headers=headers)
result = response.json()
print(result)

What this does:

  • public_id: This is the unique ID returned from the previous classification request.
  • get request: Retrieves the results of the classification, such as predicted labels and confidence scores.

Interpreting the Results

Here’s an example of what you might see when you retrieve the results:


{
  "predictions": [
    {
      "label": "Cat",
      "confidence": 0.92
    }
  ]
}

Explanation of Output Fields:

  • label: The predicted category for the image (e.g., "Cat").
  • confidence: The model’s confidence in the prediction (e.g., 92%).

Going Further

To further customize and better manage your AutoML classification tasks, Eden AI offers additional endpoints that enable you to fine-tune your workflows.

Whether you need to list, start, or retrieve results for tasks like training, prediction, or data upload, these endpoints provide enhanced flexibility and control.

You can explore all available options and detailed instructions in our comprehensive documentation here.

Why Eden AI is the Best Tool for Custom Image Classification

Eden AI offers several advantages for Custom Image Classification.

Access to multiple providers

With Eden AI, you can choose from a variety of providers, giving you great flexibility.

Ease of use

Eden AI’s API is designed to be simple and intuitive, making it easy for developers to integrate many AI services into their applications with minimal effort.

Scalability

Whether you’re working on small projects or large-scale applications, Eden AI is built to scale with your needs, making it suitable for a wide range of use cases.

Conclusion:

Using Eden AI’s API in JavaScript, you can create custom image classification models in just a few steps.

By following these five stages (create project, upload data, train model, classify images, and retrieve results), you can easily integrate image classification into your web applications.

With Axios handling the HTTP requests, the process is straightforward, allowing you to focus on what matters most—building your application.

Start Your AI Journey Today

  • Access 100+ AI APIs in a single platform.
  • Compare and deploy AI models effortlessly.
  • Pay-as-you-go with no upfront fees.
Start building FREE

Related Posts

Try Eden AI for free.

You can directly start building now. If you have any questions, feel free to chat with us!

Get startedContact sales
X

Start Your AI Journey Today

Sign up now with free credits to explore 100+ AI APIs.
Get my FREE credits now