在imagePickerController中获取选定的图像,并将其传递给coreML

时间:2019-03-20 19:45:29

标签: swift uiimagepickercontroller coreml

我正在尝试使用coreML进行识别,该功能可以正常工作并正确显示结果。但是我想将该方法调用为一个按钮,就像当我按下catDog按钮并运行该方法一样。但是由于finalResult()和identifyCatOrDog()是其自身的函数,因此我无法在按钮中调用它。我试图将方法复制并粘贴到按钮内,但没有显示任何内容。如何编辑代码,使findResult()仅在按下不自动运行的按钮时起作用?

import UIKit
import CoreML
import Vision
import Photos

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet var loadImage: UIImageView!
@IBOutlet var Result: UILabel!

@IBAction func photoBtn(_ sender: UIButton) {
    getPhoto()
}

@IBAction func cameraBtn(_ sender: UIButton) {
}

@IBAction func catDog(_ sender: UIButton) {
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func getPhoto() {
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = .photoLibrary
    present(picker, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    picker.dismiss(animated: true, completion: nil)
    guard let gotImage = info[.originalImage] as? UIImage else {
        fatalError("No picture chosen")
    }

    loadImage.image = gotImage
    identifyCatOrDog(image: gotImage)
}

func identifyCatOrDog(image: UIImage) {
    let modelFile = ImageClassifier()
    let model = try! VNCoreMLModel(for: modelFile.model)

    let handler = VNImageRequestHandler(cgImage: image.cgImage!, options: [ : ])

    let request = VNCoreMLRequest(model: model, completionHandler: findResults)
    try! handler.perform([request])
}

func findResults(request: VNRequest, error: Error?) {
    guard let results = request.results as? [VNClassificationObservation] else {
        fatalError("Unable to get results")
    }

    var bestGuess = ""
    var bestConfidence: VNConfidence = 0

    for classification in results {
        if (classification.confidence > bestConfidence) {
            bestConfidence = classification.confidence
            bestGuess = classification.identifier
        }
    }

    Result.text = "Image is: \(bestGuess) with confidence \(bestConfidence) out of 1"
}

1 个答案:

答案 0 :(得分:0)

我认为问题是,有时在关闭图像选择器时,您想致电identifyCatOrDog,而在其他时候却不打电话。

这是一个很粗略的可能性:在按钮操作方法中,引发一个bool实例属性标志,以便在调用didFinishPickingMedia时,您知道是否调用identifyCatOrDog

一种更复杂的方法是将事物划分为帮助器类,以便在按下catDog按钮之后,图像选择器的操作将在完全不同的代码世界中进行。