swift - VNClassificationObservation 不起作用?

标签 swift computer-vision deep-learning keras coreml

我一直在努力尝试使用 CoreML 和 Vision 在 swift 中创建一个简单的计算机视觉应用程序。我已经训练了我自己的 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)
        }

最佳答案

您转换的结果类型错误。要检查结果的类型,您可以在 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 协议(protocol)包含有关可能值的信息。您还可以使用以下命令将它们从您的模型打印到调试器:

print(model.modelDescription.outputDescriptionsByName)

这应该让您明白您应该期待什么。

关于swift - VNClassificationObservation 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47113363/

相关文章:

tensorflow - 如何在神经网络中添加 "OTHER"类?

ios - 检查核心数据数据库中保存的 ID 并在 Swift 3 中显示/隐藏按钮 “Add to Fav”

ios - 当 UITextField 达到最大字符数时,键盘上的“完成”按钮将不起作用

colors - 色彩空间RGB、XYZ与配色函数有什么关系?

mysql - 如何使用mysql中存储的任意数据特征训练DNN分类模型?

keras - keras LSTM 中的训练损失为 nan

ios - 在 Collection View 中访问 indexPath

swift - 快速访问协议(protocol)方法中的变量

python - 如何从检查点使用 tf.estimator.Estimator 进行预测?

matlab - 如何将此图像处理从 Matlab 转换为 OpenCV?