Skip to content

Instantly share code, notes, and snippets.

/// Remove background of input image based on an alpha mask.
///
/// - Parameters:
/// - image: Image to mask
/// - mask: Input mask. Reduces pixel opacity by mask alpha value. For instance
/// an alpha value of 255 will be completely opaque, 0 will be completely transparent
/// and a value of 125 will be partially transparent.
/// - Returns: Image mask with background removed.
func createMask(of image: UIImage, fromMask mask: UIImage, withBackground background: UIImage? = nil) -> UIImage? {
guard let imageCG = image.cgImage, let maskCG = mask.cgImage else { return nil }
@hsiaoer
hsiaoer / completePrediction.java
Last active April 8, 2022 16:01
ImageSegmentationTutorial-completePrediction
// Create the on device model for People Segmentation
// PeopleSegmentationOnDeviceModel is a subclass of FritzOnDeviceModel
PeopleSegmentOnDeviceModelFast onDeviceModel = new PeopleSegmentOnDeviceModelFast();
// Get the predictor with the People Segmentation Model
// FritzVisionSegmentPredictor is a subclass of FritzVisionPredictor
FritzVisionSegmentPredictor predictor = FritzVision.ImageSegmentation.getPredictor(onDeviceModel);
// Create a a visionImage
FritzVisionImage visionImage = FritzVisionImage.fromBitmap(inputBitmap);
@hsiaoer
hsiaoer / createFVImage.java
Last active September 11, 2019 14:47
ImageSegmentationTutorial-createFVImage
// ---------------------------------
// Handle Bitmap (ARG_8888)
// ---------------------------------
// Standard FritzVisionImage creation
FritzVisionImage visionImage = FritzVisionImage.fromBitmap(bitmap);
// Applying a rotation to the bitmap (how to get the rotation value)
// https://stackoverflow.com/questions/7286714/android-get-orientation-of-a-camera-bitmap-and-rotate-back-90-degrees
FritzVisionImage visionImage = FritzVisionImage.fromBitmap(bitmap, rotation);
@hsiaoer
hsiaoer / setupPredictor.java
Last active September 11, 2019 14:47
ImageSegmentationTutorial-setupPredictor
@Override
protected void onCreate(final Bundle savedInstanceState) {
// ...
// PeopleSegmentOnDeviceModel is a subclass of FritzOnDeviceModel
PeopleSegmentOnDeviceModel onDeviceModel = new PeopleSegmentOnDeviceModelFast();
// Get the predictor with the People Segmentation Model
// FritzVisionSegmentPredictor is a subclass of FritzVisionPredictor
FritzVisionSegmentPredictor predictor = FritzVision.ImageSegmentation.getPredictor(onDeviceModel);
}
@hsiaoer
hsiaoer / HairSeg-createImage.java
Last active September 11, 2019 14:35
Hair Seg - Create Image
// Determine how to rotate the image from the camera used.
ImageRotation rotation = FritzVisionOrientation.getImageRotationFromCamera(this, cameraId);
// Create a FritzVisionImage object from android.media.Image
FritzVisionImage visionImage = FritzVisionImage.fromMediaImage(image, rotation);
@hsiaoer
hsiaoer / HairSeg-BlendModes.java
Last active September 11, 2019 14:32
Hair Seg - Blend Modes
// Soft Light Blend
BlendMode blendMode = BlendMode.SOFT_LIGHT;
// Color Blend
BlendMode blendMode = BlendMode.COLOR;
// Hue Blend
BlendMode blendMode = BlendMode.HUE;
@hsiaoer
hsiaoer / HairSeg-CreatePredictor.java
Last active September 11, 2019 14:31
Hair Seg - Create Predictor
// Initialize the model included with the app
SegmentOnDeviceModel onDeviceModel = new HairSegmentationOnDeviceModelFast();
// Create the predictor with the Hair Segmentation model.
FritzVisionSegmentPredictor predictor = FritzVision.ImageSegmentation.getPredictor(onDeviceModel);
@hsiaoer
hsiaoer / projectBuild.gradle
Last active September 11, 2019 14:23
Sample project build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven {
url "https://fritz.mycloudrepo.io/public/repositories/android"
}
@hsiaoer
hsiaoer / backgroundReplace.java
Last active July 26, 2019 14:29
ImageSegmentationTutorial-backgroundReplace
snapshotOverlay.setCallback(new OverlayView.DrawCallback() {
@Override
public void drawCallback(final Canvas canvas) {
// 1. Resize the background image
Bitmap scaledBackgroundBitmap = BitmapUtils.resize(backgroundBitmap, cameraSize.getWidth(), cameraSize.getHeight());
// 2. Draw the background to the canvas
canvas.drawBitmap(scaledBackgroundBitmap, new Matrix(), new Paint());
@hsiaoer
hsiaoer / displayResult.java
Last active July 26, 2019 14:21
ImageSegmentationTutorial-showPredictionResult
snapshotOverlay.setCallback(new OverlayView.DrawCallback() {
@Override
public void drawCallback(final Canvas canvas) {
// If the prediction has not run
if(segmentResult == null) {
return;
}
// STEP 6: Show the people segmentation result when the background hasn't been chosen.