swift Camera_PIcker_CoreML

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift Camera_PIcker_CoreML相关的知识,希望对你有一定的参考价值。

//
//  ViewController.swift
//  WhatFlower
//
//  Created by John on 11/04/2018.
//  Copyright © 2018 Rom and Ram. All rights reserved.
//

import UIKit
import CoreML
import Vision

class ViewController: UIViewController,  UIImagePickerControllerDelegate, UINavigationControllerDelegate  {

    
    @IBOutlet weak var imageView: UIImageView!
    
        let imagePicker = UIImagePickerController()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        imagePicker.delegate = self
        imagePicker.allowsEditing = false // "true" to allow editing
        imagePicker.sourceType = .camera
  
    }

  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    
    if let userPickedImage =  info[UIImagePickerControllerOriginalImage] as? UIImage {
        
        imageView.image = userPickedImage
        
        
        guard let convertedCIImage = CIImage(image: userPickedImage) else {
            fatalError("Could not convert UIImage to CIImage")
        }
        
        detect(image: convertedCIImage)
        imageView.image = userPickedImage
    }
    imagePicker.dismiss(animated: true, completion: nil)
    }
    

    func detect(image: CIImage) {
        
        guard let model = try? VNCoreMLModel(for: FlowerClassifier().model) else {
            fatalError("Cannot import model!")
        }
        
        let request = VNCoreMLRequest(model: model) { (request, error) in
            let classification = request.results?.first as? VNClassificationObservation
                
     self.navigationItem.title = classification?.identifier.capitalized
   
            
        }
        let handler = VNImageRequestHandler(ciImage: image)
        
        do {
            
            
            try handler.perform([request])
            
        }
        catch {
            print(error)
            
        }
    }


    @IBAction func cameraTapped(_ sender: UIBarButtonItem) {
        
              present(imagePicker, animated: true, completion: nil)
        
    }
}

以上是关于swift Camera_PIcker_CoreML的主要内容,如果未能解决你的问题,请参考以下文章

Swift入门系列--Swift官方文档(2.2)--中文翻译--About Swift 关于Swift

swift 示例BS swift.swift

swift swift_bug.swift

ios 整理(一)swift和oc的区别

swift swift_extension5.swift

swift swift_optional4.swift