Android

Note

If you haven’t set up the SDK yet, make sure to go through those directions first. You’ll need to add the Core library to the app before using the specific feature or custom model libraries.

1. Add FritzVisionLabelModel via Gradle

Add our repository in order to download Fritz libraries:

repositories {
    maven { url "https://raw.github.com/fritzlabs/fritz-repository/master" }
}

Add the dependencies in app/build.gradle:

dependencies {
    implementation 'ai.fritz:core:2.0.0'
    implementation 'ai.fritz:vision-label:2.0.0'
}

2. Define FritzVisionLabelPredictor

First, create a FritzVisionLabelPredictor instance:

FritzVisionLabelPredictor visionPredictor = new FritzVisionLabelPredictor();

3. Create a FritzVisionImage from an image or a video stream

To create a FritzVisionImage from a Bitmap:

FritzVisionImage visionImage = FritzVisionImage.fromBitmap(bitmap);

To create a FritzVisionImage from a media.Image object when capturing the result from a camera, first determine the orientation of the image. This will rotate the image to account for device rotation and the orientation of the camera sensor.

// Get the system service for the camera manager
final CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

// Gets the first camera id
String cameraId = manager.getCameraIdList().get(0);

// Determine the rotation on the FritzVisionImage from the camera orientaion and the device rotation.
// "this" refers to the calling Context (Application, Activity, etc)
int imageRotationFromCamera = FritzVisionOrientation.getImageRotationFromCamera(this, cameraId);

Finally, create the FritzVisionImage object with the rotation

FritzVisionImage visionImage = FritzVisionImage.fromMediaImage(image, imageRotationFromCamera);

4. Run prediction - Label your image

Next, pass the FritzVisionImage into the predictor in order to evaluate the image labels:

FritzVisionLabelResult labelResult = visionPredictor.predict(visionImage);

visionPredictor.predict() returns a FritzVisionLabelResult which contains several methods to access the labels.

Method Description
FritzVisionImage getOriginalImage() Get the original image passed into the predict method.
List<FritzVisionLabel> getVisionLabels() Gets a list of FritzVisionLabel objects. Each label object has getText() and getConfidence() methods.
String getResultString() Gets a string of all labels in the format “<label name>:<confidence score>”
void drawVisionImage(Canvas canvas) Draws the original image passed in to the predict method.
void drawVisionImage(Canvas canvas, Size canvasSize) Draws the original image scaled up to the target canvas size. E.g Your original image was 512x512 but you’d like to stretch it to fit a canvas size of 1024x1024.
void logResult() Uses Log.d to print out the label predictions

Note

You may also pass options into FritzVisionLabelPredictor in order to set a confidence threshold for the results. For example, if you only want to return labels that have a confidence score of 0.7 or higher, you may specify it with the following:

// Create predictor options
FritzVisionLabelPredictorOptions options = new FritzVisionLabelPredictorOptions.Builder()
    .confidenceThreshold(0.7f)
    .build();

// Pass in the options when initializing the predictor
FritzVisionLabelPredictor visionPredictor = new FritzVisionLabelPredictor(options);

// Pass in options on an existing predictor instance
visionPredictor.setOptions(options);