Image classification using Nanonets API in Python

In this tutorial, we will show you how to do image classification using Nanonets API in Python.

If you have difficulties using Keras & TensorFlow or if you are a beginner and don’t know where to start, then, the Nanonets API is made just for you.
Nanonets API is one of the easiest and best tools for Image classification. Along with this, it also provides functionalities like object detection, image tagging, optical character recognition, and image segmentation.
One of the best things about this API is, you can use any language you want, as all its calls are HTTP only! In this post, I’ll be using Python.

You may also learn: Image Classification in Python using CNN and Prepare your own data set for image classification in Machine learning Python

Today we’ll learn how to use Nanonets API for Image Classification.

So, Let’s start!

Step 1: Sign-up on Nanonets

Firstly, you need to visit the Nanonets API page
Click on “Get started” and then Sign-up to get your API key and access the Features of Nanonet API.
Image classification using Nanonets API in Python

Step 2: Creating a new Model

After signing-up, you need to go to “New Model” and click on “Image Classification”
create a new model in nanonets

Step 3: Defining categories for Images

Now, you need to add the names of all the categories you want your image to be classified into. I want to build a simple model where an image of fruit is classified into an apple or a banana. So I just added two categories: apple and banana. Then click proceed.
Defining categories for Images

Step 4: Upload images

The next step is to upload images of your respective categories. This is done to train the model using known datasets. The easiest way to do this is to let Nanonet itself choose the images to upload from the web. Or else, you can upload your own images.
Upload images in nanonets

Step 5: Train and Test Model

Now you need to wait until your model has finished training.
Train and Test Model for image classification

Then you can test your model by uploading any image you want and verify its output prediction.
Train and Test Model

Step 6: Integrate with program

Nanonets API provides its readymade code. So you don’t have to worry about integrating your application with API.
Click on “Get code”, choose your language of choice, click on “Copy code”, and paste it into your editor. There are two choices in code, I have used “Code for file” in which you have to upload a file from your PC, the other one is “Code for url”.
Integrate with program

Program: Image classification using Nanonets API in Python

Given below is the code that I got. You can use this one with your own API key added in place of <Enter_API_key>. Or, I recommend using the code that you get after your data model is trained. That way, it automatically enters your API key in the right place.

import requests
url = ''
data = {'file': open('C:\\Users\\snigd\\CodeSpeedy\\apple.jpg', 'rb'), 'modelId': ('', '5032a5fc-26b7-4863-8ec9-d4c557213ddd')}   # give the path of the image in the file key
response =, auth= requests.auth.HTTPBasicAuth('<Enter_API_key>', ''), files=data)              # Enter your API key

The image I uploaded is this:
Program: Image classification using Nanonets API in Python



Step 7: Some additions for image classification

Now that we have gotten our output in the json format, we can use this to make a console program by adding the following code:

if x["message"]=="Success":
    print("Your image has been successfully classified!")
    for i in x["result"][0]["prediction"]:
        print("Label : {}         Probability : {}".format(i["label"],i["probability"]))
    print("\nHence, prediction= {}".format(x["result"][0]["prediction"][0]["label"]))
    print("Sorry! Your image could not be classified.")

The above program navigates through the main dictionary and nested lists to obtain the necessary labels and probability.


Your image has been successfully classified!

Label : apple         Probability : 0.9991737
Label : banana         Probability : 0.0008263273

Hence, prediction= apple

You can also try to make a UI out of this using a library like Tkinter.

Object Detection in PHP with 15 lines of code

Leave a Reply

Your email address will not be published. Required fields are marked *