0

我目前正在尝试构建一个使用 google 的 autoML 功能的应用程序。我已经训练了一个模型并将其发布在 google firebase 上,并按照文档将必要的代码集成到了我的应用程序中:

https://firebase.google.com/docs/ml-kit/ios/label-images-with-automl

我使用的是远程模型而不是本地模型。但是,当我尝试运行代码,然后在模拟器中选择一个图像时,控制台中会输出一个空的预测列表。

我也打开了调试功能,但这并没有帮助我修复我的错误。这是我在 ViewController 中运行的代码:

import UIKit
import CoreML
import Vision
import Firebase
import FirebaseMLCommon


var serverImage: UIImage? = nil
var topResult = ""
class ViewController: UIViewController {

@IBOutlet var skinDiseaseImageView: UIImageView!
@IBOutlet var result1Label: UILabel!
@IBOutlet var result1Confidence: UILabel!
@IBOutlet var result2Label: UILabel!
@IBOutlet var result2Confidence: UILabel!
@IBOutlet var result3Label: UILabel!
@IBOutlet var result3Confidence: UILabel!

override func viewDidLoad() {

let initialConditions = ModelDownloadConditions(allowsCellularAccess: true,
                                                                         allowsBackgroundDownloading: true)
let updateConditions = ModelDownloadConditions(allowsCellularAccess: false,
                                               allowsBackgroundDownloading: true)
let remoteModel = RemoteModel(
    name: "skinDiseaseModel",  // The name you assigned in the console.
    allowsModelUpdates: true,
    initialConditions: initialConditions,
    updateConditions: updateConditions
)
ModelManager.modelManager().register(remoteModel)

super.viewDidLoad()


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

}


extension ViewController {

@IBAction func selectImage(_ sender: Any) {
    let pickerController = UIImagePickerController()
    pickerController.delegate = self
    pickerController.sourceType = .savedPhotosAlbum
    present(pickerController, animated: true)
}

}


extension ViewController: UIImagePickerControllerDelegate {

func imagePickerController(_ picker: UIImagePickerController,     didFinishPickingMediaWithInfo info: [String : Any]) {
    dismiss(animated: true)

    guard let skinImage =     info[UIImagePickerControllerOriginalImage] as? UIImage else {
        fatalError("Error Retrieving Image Line 95")
    }

    var skinImageToDiagnose = VisionImage(image: skinImage)

    serverImage = skinImage
    skinDiseaseImageView.image = skinImage


let labelerOptions = VisionOnDeviceAutoMLImageLabelerOptions(
    remoteModelName: "skinDiseaseModel",  // Or nil to not use a remote model
    localModelName: nil     // Or nil to not use a bundled model
)
labelerOptions.confidenceThreshold = 0  // Evaluate your model in the Firebase console
// to determine an appropriate value.
let labeler = Vision.vision().onDeviceAutoMLImageLabeler(options: labelerOptions)

var topThreeResults = [String]()
var topThreeConfidences = [String]()
labeler.process(skinImageToDiagnose) { labels, error in
    guard error == nil, let labels = labels
        else {
            print(error)
            return
        }
    //task succeeded
    print("1")
    print(labels)
    var counter = 0
    for label in labels {
        topThreeResults.append(String(describing: label))
        topThreeConfidences.append(String(describing: label.confidence))
        counter = counter + 1
        print("counter")
        if counter == 3 {
            break
        }
    }

}

result1Label.text = topThreeResults[0]
result1Confidence.text = (topThreeConfidences[0] + "%")

result2Label.text = topThreeResults[1]
result2Confidence.text = (topThreeConfidences[1] + "%")

result3Label.text = topThreeResults[2]
result3Confidence.text = (topThreeConfidences[2] + "%")





}

}

这是我收到的错误:

Fatal error: Index out of range
2019-08-31 19:50:19.763469-0700 medicalAppFinal[13776:2281569]            
(lldb) 

我推断索引超出范围问题是由于标签列表(输出预测)在打印后为空。因此我明白为什么它的索引超出范围,但我不知道为什么在将图像传递到 labeler.process() 后我收到一个空列表我该如何解决这个错误?如果您需要更多信息,请告诉我

4

1 回答 1

1

这是一个逻辑错误。这部分代码:

result1Label.text = topThreeResults[0]
result1Confidence.text = (topThreeConfidences[0] + "%")

result2Label.text = topThreeResults[1]
result2Confidence.text = (topThreeConfidences[1] + "%")

result3Label.text = topThreeResults[2]
result3Confidence.text = (topThreeConfidences[2] + "%")

应该在 labeler.process() 函数中。否则,它甚至没有检索到预测列表就运行上述代码,从而导致致命错误。通过将其放入其中,我确保它已检索到预测列表,然后才运行上述代码以查找列表中的特定值。

于 2019-09-08T16:24:36.397 回答