Digital Developer Conference on Data and AI: Essential data science, machine learning, and AI skills and certification Register for free

Developing an image classifier using Watson Visual Recognition on Watson Studio

In recent years, one of the biggest applications of machine learning has been in image classification — the ability of a computer to intelligently recognize an object. Such work is being done at prominent technology companies, and it’s since created this unwarranted notion of exclusivity. We’ve crafted a narrative that to get your hands dirty with machine learning and artificial intelligence, you must be some sort of certifiable genius, or at least an engineering graduate just to break in. So, I’m going to show you how in less than 45 minutes, you can build a fully functioning image classifier that can recognize any (yes, any) object with insanely high probabilities.

Learning objectives

After completing this tutorial, you should be able to:

  • Use Watson Studio to build an image classification model using image data from any source
  • Build an Android application that classifies images using the model


  • An IBM Cloud account.

  • Enough space on IBM Cloud for three services. If you don’t have enough, delete some services to make room.

  • Android Studio. Download the latest version Android Studio 3.1.3.

  • A Java JDK SE 8 installation.

  • A data set that you want to model. If you don’t have your own data set, then you can download this sample data set.

Estimated time

It should take approximately 45 minutes to complete this tutorial.


Setting up Watson Studio for first time

Watson Studio is a cloud-based development and deployment environment for machine learning, deep learning, data governance, and data exploration. It’s a platform built for business analysts, data engineers, data scientists, and developers to simplify their tasks with an intuitive UI and vast computing power. It’s a platform where insights can be traced back to models, projects, notebooks, and data sources, and where models can evolve and automatically update themselves.

  • Navigate to


  • Sign in to IBM Cloud.

  • Click Get Started, and you are redirected to the Watson Studio dashboard. An interactive menu explains key tasks.

Create a project

  • Click New Project, select Watson Tools, and click OK.


Two services, Object Storage and Watson Visual Recognition, are created in your IBM Cloud dashboard.

  • Enter a name for your project along with the details (optional), choosing Restrict who can be a collaborator (optional). Notice that Object Storage and Watson Visual Recognition services are selected.


If there’s an error and the services are not created, you can add them from the Services menu.

  • Click Create. The project dashboard is shown, which has tabs such as Overview, Assets, Bookmarks, Access Control, and Settings.

  • Select the Assets tab.


Add data asset

  • Click New data asset. A side navigation drawer opens with the Load tab selected.


  • Click Browse or simply drag the data assets of your project. (Note: When using a Watson Visual Recognition project, have your data assets in a .zip file.)


Create a Model

  • Click New Visual Recognition model.


A new dashboard opens with a Default Custom Model model name. The dashboard only shows two tabs, My Classes and All Images.

  • In My Classes, click Create a class and create a class that you want to classify. (Note the class of an image because it relates to the object classification task).



  • Click the created class and add the data asset to it by selecting the data asset and clicking Add to model.


After the data assets have been added to the respective classes, click Train Model.


The process takes approximately 10 – 15 minutes to train the model for the respective classes.


Test Model

  • After the training is complete, click here in the message.


A new dashboard containing three tabs opens.

  • Overview
  • Test
  • Implementation

  • Click the Test tab.


  • Take a test image from any source and click Browse or simply drag the image to the dashboard. After loading the image into the image classifier, the image classification algorithm runs, which returns the accuracy within the range from 0 – 1 that identifies your class or object.


Building the client-side application (Android app)

  • Start Android Studio, and create a New project.


  • Go back to your model in Watson Studio and click the Implementation tab.
  • Under the Implementation tab, look for Java in the left menu and select it. You’ll find the code that is required to communicate with the model.


  • In Android Studio, under Gradle Scripts/build.gradle (Module:app), add implementation '' in the dependencies block, and click Sync Now.

  • After the sync process is complete, go to MainActivity under java/package_name.project_name/, and add two global objects:

    • VisualRecognition
    • CameraHelper
VisualRecognition mVisualRecognition;
CameraHelper mCameraHelper;
  • Under the onCreate method in MainActivity, initialize the objects.
mVisualRecognition = new VisualRecognition("{version}");

mCameraHelper = new CameraHelper(this);
  • Update the setApiKey method with the api key generated in the Watson Visual Recoginition service on IBM Cloud.
  • Under the onActivityResult method, create a background thread for making the network call, parsing the result, and displaying the result to the UI.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CameraHelper.REQUEST_IMAGE_CAPTURE) {
        final Bitmap photo = mCameraHelper.getBitmap(resultCode);
        final File photoFile = mCameraHelper.getFile(resultCode);
        ImageView preview = findViewById(;

        AsyncTask.execute(new Runnable() {
            public void run() {
                InputStream imagesStream = null;
                try {
                    imagesStream = new FileInputStream(photoFile);
                } catch (FileNotFoundException e) {
                ClassifyOptions classifyOptions = new ClassifyOptions.Builder()
                        .threshold((float) 0.6)
                ClassifiedImages result = mVisualRecognition.classify(classifyOptions).execute();
                Gson gson = new Gson();
                String json = gson.toJson(result);
                String name = null;
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    JSONArray jsonArray = jsonObject.getJSONArray("images");
                    JSONObject jsonObject1 = jsonArray.getJSONObject(0);
                    JSONArray jsonArray1 = jsonObject1.getJSONArray("classifiers");
                    JSONObject jsonObject2 = jsonArray1.getJSONObject(0);
                    JSONArray jsonArray2 = jsonObject2.getJSONArray("classes");
                    JSONObject jsonObject3 = jsonArray2.getJSONObject(0);
                    name = jsonObject3.getString("class");
                } catch (JSONException e) {
                final String finalName = name;
                runOnUiThread(new Runnable() {
                    public void run() {
                        TextView detectedObjects = findViewById(;

Congratulations! Now you have an Android application communicating with the model built and deployed on Watson Studio.

To see more, you can look at the GitHub repo that contains the Android code.


In this tutorial, you learned how to build an image classification model using a Watson Tool project on Watson Studio and then use the model to build an Android application.