Skip to content

Instantly share code, notes, and snippets.

class ViewController: UIViewController {
let background = UIImage(named: "clouds.png")
var animationDuration = 12.0
override func viewDidLoad() {
///...
backgroundView = initialBackground()
backgroundViewDelayed = initialBackground()
view.addSubview(backgroundView)
/// 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 }
private lazy var visionModel = FritzVisionSkySegmentationModel()
/// ...
func createSticker(_ image: UIImage) {
/// Use the result to build a mask
guard let result = try? visionModel.predict(fritzImage),
let mask = result.buildSingleClassMask(
forClass: FritzVisionSkyClass.none,
clippingScoresAbove: clippingScoresAbove,
@hsiaoer
hsiaoer / ModelOutputParams.swift
Created June 24, 2019 14:55
Model Output Params
class ViewController: UIViewController {
@IBOutlet var imageView: UIImageView!
var backgroundView: UIImageView!
var backgroundViewDelayed: UIImageView!
let context = CIContext()
// ------------------
// Step 1
@hsiaoer
hsiaoer / SkyViewController.swift
Created June 16, 2019 21:27
View Controller for sky seg
class ViewController: UIViewController {
@IBOutlet var imageView: UIImageView!
var backgroundView: UIImageView!
var backgroundViewDelayed: UIImageView!
let context = CIContext()
// ------------------
// Step 1
@hsiaoer
hsiaoer / LuckyDollarStore.md
Last active June 13, 2019 19:02
Lucky Dollar Store Coding Challenge

Lucky Dollar Store Coding Challenge

The “Lucky Dollar Store” wants to test out a new loyalty program so that with each new purchase, a customer is automatically entered in a weekly raffle (one ticket per purchase). At the end of the week, several users can be chosen from a drawing to win giveaway prizes (depending how generous the manager feels).

They’ve hired you to implement this raffle and generously gave you these two functions they will be calling:

def record_customer_purchase(customer):
 # Fill this in
@hsiaoer
hsiaoer / SkyAnimation.java
Created June 11, 2019 16:08
Animation for the Background Sky
// Start the animation
mImageView.post(new Runnable() {
@Override
public void run() {
mScaleFactor = (float) mImageView.getHeight() / (float) mImageView.getDrawable().getIntrinsicHeight();
mMatrix.postScale(mScaleFactor, mScaleFactor);
mImageView.setImageMatrix(mMatrix);
animate();
}
});
@hsiaoer
hsiaoer / ScaleBitmap.java
Created June 11, 2019 16:03
Scale the sky result bitmap
// Scale the bitmap to fit the view port
float scaleWidth = ((float) cameraSize.getWidth()) / notSkyBitmap.getWidth();
float scaleHeight = ((float) cameraSize.getWidth()) / notSkyBitmap.getHeight();
final Matrix matrix = new Matrix();
float scale = Math.min(scaleWidth, scaleHeight);
matrix.postScale(scale, scale);
// Create a scaled bitmap
Bitmap scaledNonSkyBitmap = Bitmap.createBitmap(notSkyBitmap, 0, 0,
notSkyBitmap.getWidth(), notSkyBitmap.getHeight(), matrix, false);
FritzVisionSegmentResult segmentResult = skyPredictor.predict(visionImage);
@hsiaoer
hsiaoer / SkySegPredictor.java
Created June 11, 2019 15:56
Use Sky Seg on-device model
SkySegmentationOnDeviceModel onDeviceModel = new SkySegmentationOnDeviceModel();
predictor = FritzVision.ImageSegmentation.getPredictorTFL(onDeviceModel, options);