Created
May 9, 2018 19:47
-
-
Save azamsharp/3a4476a2f9b3348f2037bc775d80ad60 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 CloudBasedViewController: 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.cloudLabelDetector() | |
let metadata = VisionImageMetadata() | |
metadata.orientation = .rightTop | |
let visionImage = VisionImage(image: img) | |
labelDetector.detect(in: visionImage) { (labels, error) in | |
if let error = error { | |
print(error.localizedDescription) | |
return | |
} | |
let predictionLabel = labels?.max(by: { (lhs, rhs) -> Bool in | |
let l :Double = (lhs.confidence?.doubleValue)! | |
let r :Double = (rhs.confidence?.doubleValue)! | |
return r < l | |
}) | |
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