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.
Step 2: Creating a new Model
After signing-up, you need to go to “New Model” and click on “Image Classification”
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.
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.
Step 5: Train and Test Model
Now you need to wait until your model has finished training.
Then you can test your model by uploading any image you want and verify its output prediction.
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”.
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 = 'https://app.nanonets.com/api/v2/ImageCategorization/LabelFile/' 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 = requests.post(url, auth= requests.auth.HTTPBasicAuth('<Enter_API_key>', ''), files=data) # Enter your API key print(response.text)
The image I uploaded is this:
Output:
{"message":"Success","result":[{"message":"Success","prediction":[{"label":"apple","probability":0.9991737},{"label":"banana","probability":0.0008263273}],"file":"apple1.jpg"}]}
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:
x=response.json() if x["message"]=="Success": print("Your image has been successfully classified!") print() for i in x["result"][0]["prediction"]: print("Label : {} Probability : {}".format(i["label"],i["probability"])) print("____________________________________________________") print("\nHence, prediction= {}".format(x["result"][0]["prediction"][0]["label"])) else: 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.
Output:
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.
Leave a Reply