Leveraging Google Cloud for building a serverless image tagging service — part 1 — Google Cloud Vision API
Here is the scenario. You work for a startup that develops a platform that allows photographers (both professionals and amateurs) to upload and share their lovely pictures with other photography lovers.
The platform has been online for more than 2 years now and has been very successful. Users have uploaded more than 5 million pictures already.
Your product owner comes to you requesting a killer feature that allows users to search for photos, even those that haven’t been tagged nor received any description yet.
You are requested to come up with a cheap and fast solution to the problem. Now what?
Google Cloud to the rescue!
In this series of articles, we are going to see how to build a solution that leverages Android, Firebase and Google Cloud Vision API for streamlining image tagging in the cloud with a serverless structure.
In this first part, we are going to focus on configuring a Google Cloud project with Google Cloud Vision API enabled. In the parts that follow, we’ll see how to configure Firebase services and how to create a basic Android app for uploading the photos.
Let’s get it started!
Google Cloud Vision API
Google describes this service as follows:
Google Cloud Vision API enables developers to understand the content of an image by encapsulating powerful machine learning models in an easy to use REST API. It quickly classifies images into thousands of categories (e.g., “sailboat”, “lion”, “Eiffel Tower”), detects individual objects and faces within images, and finds and reads printed words contained within images. You can build metadata on your image catalog, moderate offensive content, or enable new marketing scenarios through image sentiment analysis. Analyze images uploaded in the request or integrate with your image storage on Google Cloud Storage.
For our use case, we are going to focus on image classification. We are going to use it to tag our images, so they can be searched by our platform users.
The first thing we need to do in order to use the API is to create a Google Cloud Project. To do so, follow these steps:
- Access Google Cloud console and sign in using your Google account;
- Click in the menu right to the console logo:
- In the dialog that opens, click the + button for creating a new project:
- Give your new project a name and wait for its creation.
- After the project is created, click the menu right to the console logo again and select your new created project as the active one;
Cool! You have just created your Google Cloud project, which is going to be used for managing the APIs used in this series.
Time to enable the Google Cloud Vision API for the project. To do so, follow the steps below:
- Open the top left menu and select API Manager > Library:
- Next, search for the Google Cloud Vision API and select it:
- After clicking ENABLE, the page changes to show a dashboard for the API:
Next and final step for using the API is creating an API Key for authenticating our calls to the service. Let’s get it done:
- Click the top left menu again and select API Manager > Credentials:
- Click Create credentials, then select API key:
- A new dialog displays your key. You can later access and copy this key by accessing API Manager > Credentials at any time.
Testing what we have done
Time to test our precious work! In order to do this, we are going to perform a HTTP request to the API. For this, we are going to use the following JSON template:
Notice we use __IMAGE_URI__ as the value for imageUri field. This placeholder will be replaced with sed when we call the API:
What’s happening here?
- First, we use sed to replace __IMAGE_URI__ with the URL for the image we are analyzing;
- Then, we pipe the sed output to our curl command. This is going to POST our JSON changed by sed to https://vision.googleapis.com/v1/images:annotate?key=__API_KEY__ (make sure you use the API KEY you generated above here).
The result is the following:
It works! From the results, we can see the API is more than 97% sure it’s a mug and more than 94% sure it’s orange. Below is the image we analyzed. As you can see, the API yielded pretty good results:
Finally, if you check the Google Cloud Vision API dashboard again, you should see it now displays updated data:
In this first part, we have seen what is Google Cloud Vision API about, how easy it is to create a Google Cloud project and enable the Vision API for it, how easy it is to use the API and, of course, how impressively accurate the API is.
Stay tuned for the next parts, as we are going to see how to configure Firebase and integrate it with the API, as well as how to create a basic Android.
Hope you liked it!
Thanks for reading! If you found this helpful or interesting please ❤ it. Also, feel free to share your thoughts in the comments below.