Last active
July 26, 2019 14:47
-
-
Save jamesonthecrow/f47311e994bba3122fe572384bfe8348 to your computer and use it in GitHub Desktop.
Pet Segmentation iOS View Controller with Fritz (www.fritz.ai)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 31-45 can remove and replace with this