Dog or Cat


David Bourne

Find my other Apps, eBooks and an AppleTV app at

27 Apr 2019 v1.0 not submitted to the Apple App Store (just for demonstration)
Initial screen
First take a picture and Ask Dog or Cat?
A Cat? Must be all the fur

Dog or Cat is an app demonstrating machine learning (ML). A collection of dog and cat images (Kaggle Cats and Dogs Dataset provided by Microsoft) were classified using turicreate ( The dataset is subset of a larger set of dog and cat images. Theses images were placed in a folder, Dog or Cat, before running the turicreate classifier.
Python code for the image classification and creation of the ML model

First install turicreate import turicreate as tc

# Load images (Note: you can ignore 'Not a JPEG file' errors)
data = tc.image_analysis.load_images('PetImages', with_path=True)

# From the path-name, create a label column
data['label'] = data['path'].apply(lambda path: 'dog' if '/Dog' in path else 'cat')

# Save the data for future use'cats-dogs.sframe')

# Load the data
data = tc.SFrame('cats-dogs.sframe')

# Make a train-test split - 80% train and 20% test
train_data, test_data = data.random_split(0.8)

# Automatically pick the right model based on your data.
# Note: Because the dataset is large, model creation may take hours. Ten to fifteen minutes on my iMac
# Increase max_iteration
# Parmeters at
model = tc.image_classifier.create(train_data, target='label')

# Save predictions to an SArray
predictions = model.predict(test_data)

# Evaluate the model and save the results into a dictionary
metrics = model.evaluate(test_data)

# Save the model for later use in Turi Create'mymodel.model')

# Export for use in Core ML

Swift code to use this ML model @IBAction func whatIsIt(_ sender: Any)
let modelID = 0
UIGraphicsBeginImageContextWithOptions(CGSize(width: modelSize, height: modelSize), true, 1.0)
imageTest.draw(in: CGRect(x: 0, y: 0, width: modelSize, height: modelSize))
let newImage = UIGraphicsGetImageFromCurrentImageContext()!

// Copied from example
let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue, kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue] as CFDictionary
var pixelBuffer : CVPixelBuffer?
let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(newImage.size.width), Int(newImage.size.height), kCVPixelFormatType_32ARGB, attrs, &pixelBuffer)
guard (status == kCVReturnSuccess) else { return }

CVPixelBufferLockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
let pixelData = CVPixelBufferGetBaseAddress(pixelBuffer!)

let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
let context = CGContext(data: pixelData, width: Int(newImage.size.width), height: Int(newImage.size.height), bitsPerComponent: 8, bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer!), space: rgbColorSpace, bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue)

context?.translateBy(x: 0, y: newImage.size.height)
context?.scaleBy(x: 1.0, y: -1.0)

newImage.draw(in: CGRect(x: 0, y: 0, width: newImage.size.width, height: newImage.size.height))
CVPixelBufferUnlockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))

model1(modelID, pixelBuffer: (pixelBuffer)!)

print("Image is \(predictionString) [\(probInt)%]")
answerMessage = "\nThe image is a \n\n" + "\(predictionString) [\(probInt)%]\n"

let alert = UIAlertController(title: "Best Guess", message: answerMessage, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)

func model1(_ modelID: Int, pixelBuffer: CVPixelBuffer)
// Dog or Cat Classifier model
let model = DogCatClassifier()
guard let prediction = try? model.prediction(image: pixelBuffer) else { return }
predictionString = prediction.label
let prob = Int(100 * prediction.labelProbability[String(prediction.label)]! )
probInt = prob

Privacy policy link
David Bourne (