Skip to content

Instantly share code, notes, and snippets.

@daradecic
Last active May 9, 2020 15:49
Show Gist options
  • Save daradecic/7388eea2f1ea1ba73bc8c75e48ae1877 to your computer and use it in GitHub Desktop.
Save daradecic/7388eea2f1ea1ba73bc8c75e48ae1877 to your computer and use it in GitHub Desktop.
Dog vs Cat Swift Code
/* 1. Import Libraries */
import UIKit
import CoreML
import Vision
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
/* 2. Declare variables for our views */
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var textView: UILabel!
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
/* 4. Handle image picker */
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
self.textView.text = ""
}
/* 6. Link detect function to the image picker controller */
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let userPickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
imageView.image = userPickedImage
guard let ciImage = CIImage(image: userPickedImage) else {
fatalError("Cannot convert to CIImage.")
}
detect(image: ciImage)
}
imagePicker.dismiss(animated: true, completion: nil)
}
/* 5. Declare a function for image classification */
func detect(image: CIImage) {
guard let model = try? VNCoreMLModel(for: CatDog_ImageClassifier().model) else {
fatalError("Loading CoreML Model Failed.")
}
let request = VNCoreMLRequest(model: model) { (request, error) in
guard let results = request.results as? [VNClassificationObservation] else {
fatalError("Model failed to process image.")
}
if let firstResult = results.first {
self.textView.text = firstResult.identifier.capitalized
}
}
let handler = VNImageRequestHandler(ciImage: image)
do {
try handler.perform([request])
}
catch {
print(error)
}
}
/* 3. Show image picker when the camera button is pressed */
@IBAction func cameraTapped(_ sender: UIBarButtonItem) {
present(imagePicker, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment