Created
August 12, 2017 14:31
-
-
Save Alicelovecode/3987bcff03f7ea24890011224647d716 to your computer and use it in GitHub Desktop.
seefood.swift
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
| import UIKit | |
| import VisualRecognitionV3 | |
| class ViewController: UIViewController,UINavigationControllerDelegate,UIImagePickerControllerDelegate { | |
| let apiKey = "f394cba206eb4ff8778fce96d95801f7494fdfc8" | |
| let version = "2017-08-12" | |
| @IBOutlet weak var imageView: UIImageView! | |
| @IBOutlet weak var cameraButton: UIBarButtonItem! | |
| let imagePicker = UIImagePickerController() | |
| var classificationResults : [String] = [] | |
| //設一個Array空字串放可能的答案 | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| imagePicker.delegate = self | |
| } | |
| func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | |
| if let image = info[UIImagePickerControllerOriginalImage] as? UIImage{ | |
| imageView.image = image | |
| imagePicker.dismiss(animated: true, completion: nil) | |
| let visualRecognition = VisualRecognition(apiKey: apiKey, version: version) | |
| let imageData = UIImageJPEGRepresentation(image, 0.01) | |
| let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! | |
| let fileURL = documentURL.appendingPathComponent("tempImage.jpg") | |
| try? imageData?.write(to: fileURL, options: []) | |
| visualRecognition.classify(imageFile: fileURL, success: { (classifiedImages)in | |
| let classes = classifiedImages.images.first!.classifiers.first!.classes | |
| //利用迴圈在回傳的資料中抓出可能的答案放到Array | |
| for index in 1..<classes.count{ | |
| self.classificationResults.append(classes[index].classification) | |
| } | |
| print(self.classificationResults) | |
| }) | |
| }else{ | |
| print("There was an error picking the image") | |
| } | |
| } | |
| @IBAction func cameraTapped(_ sender: UIBarButtonItem) { | |
| imagePicker.sourceType = .savedPhotosAlbum | |
| imagePicker.allowsEditing = false | |
| present(imagePicker, animated: true, completion: nil) | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment