Google Cloud Vision API is a machine learning tool that can classify details from an image provided as an input into thousands of different categories with pre-trained API models. It offers these pre-trained models through an API and the categories are detected as individual objects within the image.

In this tutorial, you are going to learn how to integrate Google Cloud Vision API in a React Native application and make use of real-time APIs. You can find the complete code inside this GitHub repo.

Installing Expo

If you are not familiar with Expo, this tutorial can be a good start. Basically, Expo provides a set of tools to create and publish React Native applications with minimal effort. Earlier, React Native had something called create-react-native-app which is now merged with Expo-Cli and is an official way to build a React Native app.

To create your React Native app, you need to install Expo as a global npm module.

npm install -g expo-cli

Once the command line interface for Expo is installed in your local development environment, you must run the following command in order to generate a project.

expo-cli init google-vision-rn-demo

It will ask you for which template to use; choose the option blank template rather than tabs template. We only need a single screen in our application for the demonstration purposes. In the last step, you will be prompted to write the name of the project — simply type it and hit enter. Then, it will start installing dependencies.

Once the project is created, traverse into the project directory. If you need any help with this setup, refer to the Expo documentation.

Setting Up Firebase

In this section, we are going to set up a new Firebase project. It will provide us the database and backend service and we do not have to write our own backend for this tutorial, hence saving time and focusing on what we need to learn. For simplicity, we are going to make the Firebase project data public for demonstration purposes.

Visit Firebase and sign-in with your Google ID. Once signed-in, click on a new project and enter a name. Lastly, hit the Create Project button.

The next step is to make sure we set up Firebase database rules to allow us to upload image files through the app. From the left-hand side menu in the Firebase console, open Database tab and then choose Rules and modify them as follows.

The Xs are values of each key you have to fill in. Ignore the value for Key GOOGLECLOUDVISIONAPI_KEY right now as we will get back to it in the next section. Other values for their corresponding keys can be attained at the Firebase console. You can get these values by visiting the Firebase console and then click the gear icon next to Project Overview in the left-hand side menu bar and lastly go to Project settings section. There are ways in Expo where you do not have to publish your secret keys when deploying the app or uploading the codebase on a site like GitHub. The initial step we recommend is to add this file inside .gitignore.

Then create another file called firebase.js inside the config directory. We will be using this file in the main application to send requests to upload an image to the Firebase storage. Also, note that we are importing environment.js in it to access Firebase keys.

Getting Google Cloud Vision API Key

To use a Google Cloud Platform service, you need a Gmail account. Once you are signed-in from your Gmail ID, you can visit the Google Cloud Console. The next step is to create a new project.

Click select a project from the drop-down menu and then click new project. Enter the name of your project and then click Create. Once you’ve created the project, we are placed back into the main console page again and then need to select our newly created project.

The next step in this process is to get your API key. This you can get by clicking on the console and moving over to Dashboard section and under that choose Enable APIs and Services.

Then type vision in the search as shown below and then click Vision API.

Lastly, click Enable like below

In order to complete this process of enabling Vision API services, you are required to add billing information (if you haven't done already) to your Google Cloud Platform account.

Your URL in the dashboard will look like this: https://console.cloud.google.com/apis/dashboard?project=FIREBASE-PROJECT-ID&folder&organizationId. Once you are at the below screen, click on the Credentials section from the left-hand side menu and create a new API key if there isn't any by clicking on the button Create Credentials and then API Key.

Once you have created your API key, it is time to add it in the file environment.js for the key GOOGLECLOUDVISIONAPI_KEY.

That's it. Setting up the APIs is complete. We can now move on to work on the app itself.

Building The App

To get started, we need to install an npm package called uuid to create a unique blob for the image that is going to upload on the Firebase storage service. Run the command npm install --save uuid. Next, open App.js and paste the code you can find here.

Note that most of the source code for accessing and uploading to Firebase is taken from an example of using Expo with Firebase. We are going to explain below the bits that are essential to connect and run Firebase. First, let us start by understanding what uploadImageAsync is doing.

As shown in the above snippet, the uploadImageAsync function uploads the image by creating a unique image ID or blob with the help of uuid. It also uses xhr to send a request to the Firebase storage to upload the image. We are also defining a default state in the App component and asking for User Permissions for both using the camera roll or gallery or take a photo from the device's camera as shown in the code snippet below.

The Vision API uses HTTP Post request as a REST API endpoint to perform data analysis on images you send in the request. This is done via the URL https://vision.googleapis.com/v1/images:annotate. To authenticate each request, we need the API key. The body of this POST request is in JSON format. For example: