Background Removal for Images
API Interface of the iOS Framework
Instantiate Background Removal model with preferred confidence
threshold
value (might be changed in runtime).Lower threshold values keep more pixels that possibly correspond to the background. Higher threshold values remove more pixels that possibly correspond to the foreground. The effective range for this parameter is [0., 1.].
private var backgroundRemover = BackgroundRemover(threshold: 0.5)
Load the model with
backgroundRemover.loadModel
methodExample:
backgroundRemover.loadModel { error in
self.dismissPresentedViewController(animated: true) {
if error != nil {
showError(on: self, message: "Failed to initialize BG Removal model: \(error!.localizedDescription)")
}
}
}
Schedule the task with
backgroundRemover.removeBackground
method when the model is loadedExample:
let result = self.backgroundRemover.removeBackground(fromImage: image) { mask, error in
if error != nil {
showAlert(on: self, message: "BG Model runtime failure: \(error!.localizedDescription)")
return
}
DispatchQueue.main.async {
(self.loadingViewController as! LoadingViewController).update(text: "Applying Mask...")
DispatchQueue.global().async {
self.applyMask(mask!, to: image)
}
}
}
The mask is returned as a UIImage object with non-zero pixels corresponding to the foreground with the defined confidence threshold.
Last modified 4mo ago