Creating a text recognition app using Firebase on-cloud APIs

In this section, we are going to convert the on-device app to a cloud app. The difference is that on-device apps download the model and store it on the device. This allows for a lower inference time, allowing the app to make quick predictions.

By contrast, cloud-based apps upload the image to the Google server, meaning inference will happen there. It won't work if you are not connected to the internet.

In this case, why use a cloud-based model? Because on-device, the model has limited space and processing hardware, whereas Google's servers are scalable. The Google on-cloud text recognizer model is also able to decode multiple languages.

To get started, you need a Google Cloud subscription. Follow these steps:

  • Go to your Firebase project console
  • In the menu on the left, you will see that you are currently on the Spark Plan (the free tier)
  • Click Upgrade, and follow the instructions to upgrade to the Blaze Plan, which is pay-as-you-go
  • You need to provide credit card or payment details for verification purposes—these will not be charged automatically
  • Once you subscribe, you will receive 1,000 Cloud Vision API requests free each month
This program can be tried only if you have a upgraded Blaze Plan and not a free tier account. The steps are given to create a upgraded account and please follow steps to get the account to try the program given.

By default, Cloud Vision is not enabled for your project. To do so, you need to go to the following link: https://console.cloud.google.com/apis/library/vision.googleapis.com/?authuser=0. In the top menu dropdown, select the Firebase project containing the Android app you added in the previous section.

Click Enable to enable this feature for your app. The page will look like the following screenshot:

Now return to your code, and make the following changes.

You can find the application code in our Packt Github repository at: https://github.com/PacktPublishing/Machine-Learning-for-Mobile/tree/master/Testrecognizationoncloud.

All the other files, except the main activity, have no changes.

The changes are as follows:

import com.google.firebase.FirebaseApp;
import com.google.firebase.ml.vision.FirebaseVision;
import com.google.firebase.ml.vision.common.FirebaseVisionImage;
import com.google.firebase.ml.vision.document.FirebaseVisionDocumentText;
import com.google.firebase.ml.vision.document.FirebaseVisionDocumentTextRecognizer;

Now, we need to import the preceding packages as dependencies.

 private FirebaseVisionDocumentTextRecognizer textRecognizer;

The preceding code will declare the document text recognizer.

textRecognizer = FirebaseVision.getInstance().getCloudDocumentTextRecognizer();

The preceding code instantiates and assigns the cloud text recognizer.

       takePictureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takePicture();
//In this function we are having the code to decode the characters in the picture
}
});
}

The preceding code registers the on-click-event listener for the take-picture button.

Bitmap bmp = BitmapFactory.decodeByteArray(bytes,0,bytes.length);

The preceding line creates a bitmap from the byte array.

FirebaseVisionImage firebase_image = FirebaseVisionImage.fromBitmap(bmp);

The preceding line creates a firebase image object to pass through the recognizer.

 textRecognizer.processImage(firebase_image)

The preceding line passes the created image object to the recognizer for processing.

.addOnSuccessListener(new OnSuccessListener<FirebaseVisionDocumentText<() {
@Override
public void onSuccess(FirebaseVisionDocumentText result) {
Toast.makeText(getApplicationContext(),result.getText(),Toast.LENGTH_LONG).show();
}
})

The preceding code block will add the on-success listener. It will receive a FirebaseVision document text object, which is in turn displayed to the user in the form of a Toast message.

.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e)
{
Toast.makeText(getApplicationContext(),"Unable to read the text",Toast.LENGTH_LONG).show();
}
});

The preceding code block will add the on-failure listener. It will receive an exception object, which is in turn a display error message to the user in the form of a Toast message.

Once you run the code with the internet-connected device , you will get the same output as before, but from the cloud.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset