VNClassificationObservation无法正常工作?

时间:2017-11-04 17:15:39

标签: swift computer-vision deep-learning keras coreml

我一直在努力尝试使用CoreML和Vision快速创建一个简单的计算机视觉应用程序。我已经训练了我自己的Keras网络,它采用64x64分辨率的彩色图像,然后告诉你它是哪个字母。当我在手机上执行此应用程序并拍摄图像并点击“使用此图像”时,代码会在这段代码中崩溃:

//send a request to the network to identify the image
            let request = VNCoreMLRequest(model: model) { (request, error) in
                guard let results = request.results as? [VNClassificationObservation] else {
                    fatalError("Model failed to load image")
            }

我在过去的三个小时里遇到了这个错误,希望你们能帮助我弄清楚出了什么问题!下面是我使用的其余代码。

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imageView: UIImageView!

    let imagePicker = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()

        imagePicker.delegate = self
        imagePicker.sourceType = .camera
        imagePicker.allowsEditing = false

    }

    //function to chose an image from your library or take one with your camera
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

            imageView.image = userPickedImage

            //transform the image from UIImage to a CIIMage type
            guard let ciImage = CIImage(image: userPickedImage) else {
                fatalError("Couldn't transform image to CIImage type")
            }

            //call the detect function to pass the new ciImage into the network
            detect(image: ciImage)

        }

        imagePicker.dismiss(animated: true, completion: nil)

    }

    //function to classify the image that is taken with the camera or chosen from the library
    func detect(image: CIImage) {

        //try to load the model, if not throw an error
        guard let model = try? VNCoreMLModel(for: chars74k().model) else {
            fatalError("Loading coreML model failed")
        }

        //send a request to the network to identify the image
        let request = VNCoreMLRequest(model: model) { (request, error) in
            guard let results = request.results as? [VNClassificationObservation] else {
                fatalError("Model failed to load image")
        }

            print(results)

    }
        //create handler for image
        let handler = VNImageRequestHandler(ciImage: image)

        do{
            try handler.perform([request])
        }
        catch {
            print(error)
        }

1 个答案:

答案 0 :(得分:1)

您投射的是错误的结果类型。要检查结果的类型,可以在fatalError行中添加断点并使用调试器执行此操作。但我认为你是初学者所以试试这个。替换:

guard let results = request.results as? [VNClassificationObservation] else {
    fatalError("Model failed to load image")
}

使用:

print("your results are \(type(of: results))")

if let results = request.results as? [VNClassificationObservation] {
    print("your results are of type VNClassificationObservation")
}

if let results = request.results as? [VNPixelBufferObservation] {
    print("your results are of type VNPixelBufferObservation")
}

if let results = request.results as? [VNCoreMLFeatureValueObservation] {
    print("your results are of type VNCoreMLFeatureValueObservation")
}

docsVNCoreMLRequest的可能结果非常清楚所以你应该阅读它们(我强烈推荐这个,即使你没有那个成功,它也不多,它们很简单)。

您的请求输出的大部分情况都是将分类与模型的其他输出结合起来的,因此您的失败闭包很可能会替换为:

guard let results = request.results as? [VNCoreMLFeatureValueObservation] else {
    fatalError("Model failed to load results")
}

请记住相应地修改引用results的代码。 VNCoreMLFeatureValueObservation协议包含有关可能值的信息。您还可以使用以下命令将它们打印到模型的调试器中:

print(model.modelDescription.outputDescriptionsByName)

这应该可以解释你应该期待什么