Last active
May 9, 2018 18:28
-
-
Save azamsharp/3d62c1f7694941d51b4d535cef906b3b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// ViewController.swift | |
// FirebaseML | |
// | |
// Created by Mohammad Azam on 5/8/18. | |
// Copyright © 2018 Mohammad Azam. All rights reserved. | |
// | |
import UIKit | |
import Firebase | |
class ViewController: UIViewController { | |
@IBOutlet weak var classificationLabel :UILabel! | |
@IBOutlet weak var imageView :UIImageView! | |
private var index = 0 | |
let images = ["tulip","dog","cat","fish","bat"] | |
lazy var vision = Vision.vision() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
@IBAction func detect() { | |
let imageName = images[index] | |
index += 1 | |
let img = UIImage(named: imageName)! | |
self.imageView.image = img | |
let labelDetector = vision.labelDetector() | |
let visionImage = VisionImage(image: img) | |
labelDetector.detect(in: visionImage) { (labels, error) in | |
if let error = error { | |
print(error.localizedDescription) | |
return | |
} | |
// find the label with highest confidence | |
let predictionLabel = labels!.max { lhs, rhs in | |
return lhs.confidence < rhs.confidence | |
} | |
for label in labels! { | |
print("\(label.label) has \(label.confidence)") | |
self.classificationLabel.text = predictionLabel?.label | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment