Created
May 17, 2020 22:53
-
-
Save navczydev/b7663a933feee7d57c09d4bba3b1cc46 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
| // | |
| // AppDelegate.swift | |
| // ImageClassifierApp | |
| // | |
| // Created by Nav Singh on 10/5/20. | |
| // Copyright © 2020 Nav Singh . All rights reserved. | |
| // | |
| import UIKit | |
| import CoreML | |
| import Vision | |
| class ViewController: UIViewController { | |
| @IBOutlet weak var classifyButton: UIBarButtonItem! | |
| @IBOutlet weak var imageView: UIImageView! | |
| @IBOutlet weak var classifyLabel: UILabel! | |
| @IBOutlet weak var imageSizeConstraint: NSLayoutConstraint? | |
| //image classifying code here! | |
| @IBAction func didTapClassify(_ sender: Any) { | |
| guard let model = try? VNCoreMLModel(for: MyImageClassifier_2().model) else {return} | |
| let request = VNCoreMLRequest(model: model, completionHandler: {request, error in | |
| guard let results = request.results else { return } | |
| print(results) | |
| guard let firstResult = results.first as? VNClassificationObservation else {return} | |
| self.classifyLabel.text = firstResult.identifier | |
| }) | |
| guard let image = imageView.image else {return} | |
| let handler = VNImageRequestHandler(cgImage: image.cgImage!, orientation: .up) | |
| try? handler.perform([request]) | |
| } | |
| } | |
| extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
| @IBAction func didTapChoose(_ sender: Any) { | |
| // Present an image picker view controller | |
| let imagePicker = UIImagePickerController() | |
| imagePicker.sourceType = .photoLibrary | |
| imagePicker.allowsEditing = false | |
| imagePicker.delegate = self | |
| self.present(imagePicker, animated: true, completion: nil) | |
| } | |
| func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { | |
| // Set the image to our image view | |
| guard let image = info[.originalImage] as? UIImage else { return } | |
| imageView.image = image | |
| // Change the aspect ratio of the image to match | |
| if let imageView = imageView { | |
| if let constraint = imageSizeConstraint { | |
| imageView.removeConstraint(constraint) | |
| } | |
| let aspectRatio = image.size.height / image.size.width | |
| let constraint = NSLayoutConstraint(item: imageView, attribute: .height, | |
| relatedBy: .equal, | |
| toItem: imageView, attribute: .width, | |
| multiplier: aspectRatio, constant: 0) | |
| imageView.addConstraint(constraint) | |
| self.imageSizeConstraint = constraint | |
| } | |
| // Dismiss the image picker | |
| dismiss(animated: true, completion: nil) | |
| // Enable the classify button | |
| //reset the state of label | |
| self.classifyLabel.text = "...." | |
| classifyButton.isEnabled = true | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment