Coding the application

The code for this application can be found in your GitHub repository at https://github.com/PacktPublishing/Machine-Learning-for-Mobile/tree/master/Fritz/imagelabelling/imagelabelling.

Once you have downloaded the code open it in Android studio here you can find the code in the MainActivity.java.

To explain the whole code, it may deal more with android code. Here, you can find the explanation of the important code blocks:

Fritz.configure(this.getApplicationContext());

The preceding line in the oncreate life cycle method will initialize the Fritz framework:

options = new FritzVisionLabelPredictorOptions.Builder()
.confidenceThreshold(0.3f)
.build();

The preceding line will create the configuration options for the label predictor:

visionPredictor = FritzVisionLabelPredictor.getInstance(this.getApplicationContext(), options);

Creating the instance of the predictor:

Bitmap bmp = BitmapFactory.decodeFile(file.getPath());

Getting the image saved to the file and converting this as a bitmap:

FritzVisionImage img = FritzVisionImage.fromBitmap(bmp);
List<FritzVisionLabel> labels = visionPredictor.predict(img);

Converting the bitmap image to fritz vision image and supplying that image object to the predictor's predit method, which, in turn, returns the predicted labels as the list:

String output="";

for(FritzVisionLabel lab: labels)
{
output = output + lab.getText()+" Confidence: "+ lab.getConfidence();
}

if(output.trim().length()==0)
{
output = "Unable to predict.";
}
Toast.makeText(MainActivity.this, output, Toast.LENGTH_LONG).show();

As the predictor returned a list of Fritzvisionlabel objects, we need to decode that and show it to the user. The preceding code shows the content and the confidence percentage to the user in a Toast message.

Once you run the app, the image frames captured from the camera will be shown in the texture view that we have created in our layout.

Once you click the start labelling button, it will save the image to the disk and input the same image to the Fritzvisionlabel predictor. Once you revive the prediction results, you will be interpreting it and showing it to the user in the form of a Toast message.

To make the preceding app work, we need to add this app to your Fritz project.

To do so, click Project Settings in the left-hand menu of your project in the Fritz web console.

Then, click on Add android app to your project and it will open a dialog, as follows:

In this, you need to give a name to your app, for identification purposes. Then you need to get the package name from your android manifest file and enter it in the Package ID text field.

This can be obtained from the manifest tag of your manifest file as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.avinaas.imagelabelling">

Once you register the app, you can run and see the result by connecting an Android device to your PC with the USB-debugging option enabled.

Make sure you disable the Instant run option in your android studio. This can be done from the settings option in the file menu.

Once you successfully run the app, the results will look like this:

..................Content has been hidden....................

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