Image Recognition on Andorid with Google Cloud Vision API


Andorid Image Recognition with Google Cloud Vision API

Google introduced a new API called the Google Cloud Vision API. This API has full potential to understand the contents of an image by using the Google’s machine learning platform and TensorFlow. It allows the API to process individual pieces of an image separately and return the results really fast in a unified format. Therefore in simple terms now you can submit an image to the Google Cloud Vision API and find out what’s in the image. What’s more; is that, when making a request to process an image; Google gives us the capability to specify the types of analysis that should be performed on this image. For example, a simple object identification, landmark detection, facial detection, sentimental analysis and many more such analysis could be performed on an image. But what’s even more great is that, this API can also be integrated directly into the Android apps. Making Android image recognition very simple. Also this is another way, Google lets us perform image recognition on Android, powered by Google cloud platform. Imagine the power an end user can have through this API.

 

The new Google Cloud Vision API is a multi-platform solution for image recognition, weather its an Android app, iOS app or cloud storage, this API is available for image analysis. As this Cloud Vision API has SDK support for Java, Go Lang, Node.js, Python, and more importantly JSON format. But here in this article we would mainly discuss on how to do image recognition on android. Therefore continuing on Java/Android, you might be aware that, there are many ways to perform android image recognition like OpenCV, OCR reading libraries and facial recognition APIs. But none of them are as accurate and light weight as this new Android Cloud Vision API as they require a huge amount of data to be present on the app beforehand to perform object matching. Which in turn increases the APK size and still result in in-accurate data. Also to use this new Android Cloud Vision API and perform all sorts of image analysis; no heavy gradle or project files are required to be included in your project. Just the basic google-api-client-androidgoogle-http-client-gson and google-api-services-vision dependencies are required in your gradle for models. Read more about it in the next section:

 

Enabling yourself to use Google Cloud Vision API on Android

To perform Android image recognition using the Google Cloud Vision API, we must first enable it from the Google Cloud Developer Console, please follow the steps:

  1. Create a project in Google Cloud Console or use an existing one.
  2. Enable Billing for the project. if you have a new or un-used account you can start a free trial (It might ask your credit card info to validate your identity but not charge you).
  3. Enable the Google Cloud Vision API using this link. OR
    • Navigate to “API Manager” section from the hamburger menu.
    • Search and select “Google Cloud Vision API”.
    • Enable it.
  4. Then go to the credentials section from the side menu.
  5. Click on credentials drop down menu and select OAuth Client ID.
    • Select Application Type as Android.
    • Add a suitable name like Android client for Cloud Vision API
    • Enter your SHA1 fingerprint in the desired format. Using the mentioned command on screen or use this SHA1 fingerprint tutorial to get your fingerprint.
    • Enter the package name for your app, can be located in the defaultConfig block of your gradle.
    • Click on create.
  6. That’s it, you’re done.

If you are planning to access Google Cloud Vision API through some other platform you may need to create the credentials in a different manner. As when accessing this API on Android to perform image analysis, we need the end user’s consent that we are accessing their pictures. Although if you are planning to perform image recognition in background, like a server to server interaction. Then things should be handled differently. We may not need to generate an OAuth ID, instead a Service Account key may be required. But lets not discuss that here; as here in this tutorial our main focus is to perform; Android image recognition. Therefore before moving on to next section lets add the following dependencies in your app gradle, although at the end of this example full source code is available:

 

Supported Image Analysis Techniques in Cloud Vision API on Android

In general Google Cloud Vision API support many types of image analysis techniques. Be it optical character recognition, landmark detection or a simple logo detection. Cloud vision API does it very accurately. Also the best part about this API is that; its cross platform and is available via API access, which results in very light weight applications. Also since this technology is comparatively new, there is a lot of scope for improvement. And while that’s happening behind the scenes we don’t need to worry about the changing code-base as basically its just an API call. Moving on lets have a look at the supported Android image recognition techniques.

1. LABEL_DETECTION

One of the most basic types of techniques available in Android Cloud Vision API is the label detection. This allows the API to analyse image content and list out all the items contained in it individually. Have a look at the image I uploaded:

Android Cloud Vision API - 1

Labels: statue, monument, sculpture, etc. Landmarks: Albert Einstein Memorial

 

2. TEXT_DETECTION

Another interesting type of image analysis technique in Google Cloud Vision API is this text detection feature. It allows us to extract out the text from an image. My personal opinion is that, this feature alone could turn out to be the torch bearer for whole Cloud Vision API suite.

Android Cloud Vision API - 2

Texts: Federal Deposit Insurance Corporation, along with locale and formatting.

 

3. LANDMARK_DETECTION

Google has gathered so much of data that now they can even identify a landmark, by just scanning through its picture. In a way it might just sound a little scary, but this image recognition technique is equally powerful and useful. Imagine how useful it could be in adding caption on your vacation images automatically.

Android Image Recognition

Landmark: Duquesne Incline

 

4. LOGO_DETECTION

By using the new Cloud Vision API you can also identify some of the popular logos. Although when I tried this API with some logos, it wasn’t able to identify most of them. But, maybe in future this will get improved.

 

5. FACE_DETECTION

This is also one the very powerful features of the Google Cloud Vision API. This allows us to mark the number of faces in a picture. It also helps in identifying the placement of individual facial features in an image. By using this image recognition technique on Android we can highlight a face with a polygon very easily.

 

6. SAFE_SEARCH_DETECTION

This technique allows us to detect inappropriate images. This could majorly help in moderation of images when performing a server to server integration. Currently it supports four types of annotations adult, spoof, medical and violence.

 

7. IMAGE_PROPERTIES

Android Cloud Vision API also provides a way to identify the dominant colors in an image. In a way this feature would not be very much used on Android as a great alternative in form of Palette library is already available, through which dominant colors can be identified.

Android Image Recognition - 2

A random picture of a hotel room.

 

Is Google Cloud Vision API Free ?

The answer is NO. To perform image recognition on android via this API, I assume an infrastructure cost is involved. Therefore just like most of the Google products, Cloud Vision API is FREE for initial use on all platforms but when usage increases, they will start charging you. Please find the detailed pricing plan here.

 

Android Image Recognition using Google’s Cloud Vision API

Behind the scenes Google is harnessing the power of TensorFlow and Machine Learning platforms to perform this powerful image analysis on Android. As I mentioned earlier through this Android image recognition technique, we can categorize our images in to thousands of tags. This will definitely help us in organizing our data in a better way; leading us to build better location aware apps. To build an Android image recognition app using the Cloud Vision API, hope you have enabled the API from the Cloud Console using the steps mentioned in the first section of this tutorial and included all the dependencies in your build.gradle file. Next lets move on to defining a layout where all the results would be displayed. (Although full source code is available at the end of the tutorial.)

Next lets define the GET_ACCOUNTS and INTERNET permission in the manifest. As to perform an OAuth request with Android to Could Vision API we need to access the account information on the android device.

 

In this example to make a Google Cloud Vision API request on Android, we will be using Google API Client library for java. And as you might know, to perform a Google API Client OAuth request on Android we first need to get an auth token from google via a client call. Therefore lets first define a class to get an OAuth token.

Please Note: Don’t forget to generate a ClientID for your OAuth token in the Cloud Console, using the steps mentioned in first section.

 

Moving on lets define the MainActivity where the Android image recognition would take place. In this example we would perform LABEL_DETECTIONTEXT_DETECTION, and LANDMARK_DETECTION. Although if you wish to perform more analysis on single image. You can add more feature requests on the same image upload. But since this Google Could Vision API is in very nascent stage, therefore as and when more features are added, it gets slower.

To perform a successful Android image recognition, using the new Google Cloud Vision API, we first need to authenticate the our app and generate an OAuth token for making an API call. But before generating an OAuth token we may first need to pick an account through which the OAuth token can be generated. But to pick an account we first need to get a permission called GET_ACCOUNTS. Once we have the permission to get accounts, we can call the method getAuthToken() mentioned above to pick an account and get its OAuth token. This method internally takes consent from the user, using the GetTokenTask; after they select an account using the pickUserAccount() method. Now once we have this API token we can call the launchImagePicker() method to pick an image from gallery and pass it on to the callCloudVision() method which uploads the image to Google Cloud Vision API and applies all the Android image analysis techniques on it. This method internally encodes the bitmap into a jpeg, creates and sends a request to the google cloud vision API. Further we parse the result and display it on screen. View the full source code here:

 Full Source Code

In the above Android image recognition example, we simply picked an account authenticated it, selected and uploaded an image to perform image analysis on it. Although we applied very basic set of image recognition techniques, like LABEL_DETECTIONTEXT_DETECTION, and LANDMARK_DETECTION. Which gave deep insights on how google looks at an image. Maybe in future Google would enhance this API and launch something like this for videos as well. But as of now even the simple Google Cloud Vision API for images is doing wonders. Let me know what you guys are planning to build with these awesome set of APIs. Connect with us on Facebook, Google+ and Twitter for more updates.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.


Leave a comment

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

One thought on “Image Recognition on Andorid with Google Cloud Vision API