Skip to content

Instantly share code, notes, and snippets.

@jamesonthecrow
Last active July 26, 2019 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesonthecrow/f47311e994bba3122fe572384bfe8348 to your computer and use it in GitHub Desktop.
Save jamesonthecrow/f47311e994bba3122fe572384bfe8348 to your computer and use it in GitHub Desktop.
Pet Segmentation iOS View Controller with Fritz (www.fritz.ai)
class ViewController: UIViewController, UIImagePickerControllerDelegate,
UINavigationControllerDelegate {
/// The rest of the view controller...
/// Scores output from model greater than this value will be set as 1.
/// Lowering this value will make the mask more intense for lower confidence values.
var clippingScoresAbove: Double { return 0.6 }
/// Values lower than this value will not appear in the mask.
var zeroingScoresBelow: Double { return 0.4 }
private lazy var visionModel = FritzVisionPetSegmentationModel()
/// The rest of the view controller...
@objc func imagePickerController(_ picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismiss(animated: true, completion: { () -> Void in
})
createSticker(image)
}
func createSticker(_ uiImage: UIImage) {
guard let result = try? visionModel.predict(FritzVisionImage(image: uiImage)),
let mask = result.buildSingleClassMask(
forClass: FritzVisionPetClass.pet,
clippingScoresAbove: clippingScoresAbove,
zeroingScoresBelow: zeroingScoresBelow
)
else { return }
let petSticker = fritzImage.masked(withAlphaMask: mask)
DispatchQueue.main.async {
self.imageView.image = petSticker
}
}
}
@hsiaoer
Copy link

hsiaoer commented Jul 26, 2019

Line 31-45 can remove and replace with this

   let petSticker = fritzImage.masked(withAlphaMask: mask)

    DispatchQueue.main.async {
      self.imageView.image = petSticker
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment