Android

Fritz provides an Android API that you can use to transform images or live video into beautiful works of art. You can choose to stylize an image from the photo gallery or transform live video captured by the device’s camera. Follow these simple instructions in order to bring style transfer to your app in no time.

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 FritzVisionStyleModel via Gradle

Assuming that you’ve already included the Core SDK and the fritz repository, you’ll also want to include the style transfer dependency.

In app/build.gradle:

dependencies {
    implementation 'ai.fritz:core:1.3.2'
    implementation 'ai.fritz:vision-style-model:1.3.2'
}

2. Get a style predictor

First, get a predictor class from one of our 11 different styles.

// Define the stylePredictor
FritzVisionStylePredictor stylePredictor;

// Get the starry night predictor
// "this" refers to the calling Context (Application, Activity, etc)
stylePredictor = FritzVisionStyleTransfer.getPredictor(this, ArtisticStyle.STARRY_NIGHT)

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 on FritzVisionImage

Pass your FritzVisionImage into the predictor to transform the style.

FritzVisionImage styledImage = stylePredictor.predict(visionImage);

Draw the styledImage to a Canvas to see the new image. FritzVisionImage contains several convenience methods to help transform the image. To learn more, visit the Reference Docs.

Canvas canvas = new Canvas();
FritzVisionImage.drawOnCanvas(styledImage.getBitmap(), canvas);

// OR
canvas.drawBitmap(styledImage.getBitmap(), new Matrix(), new Paint());

Note

To achieve higher resolution on the stylized image, you can specify FritzVisionStylePredictorOptions. However, note that higher resolution results in longer prediction times.

  • To initialize the options when getting the style predictor.

    FritzVisionStylePredictorOptions options = new FritzVisionStylePredictorOptions.Builder()
            .imageResolution(FritzStyleResolution.HIGH)
            .build();
    
    predictor = FritzVisionStyleTransfer.getPredictor(this, ArtisticStyle.STARRY_NIGHT, options);
    
  • To change the options on an existing predictor.

    FritzVisionStylePredictorOptions options = new FritzVisionStylePredictorOptions.Builder()
            .imageResolution(FritzStyleResolution.HIGH)
            .build();
    predictor.setOptions(options);
    

For a full tutorial of style transfer, take a look at this post on Heartbeat.