swift - 不从远程 Firebase Automl 自定义模型中检索输出预测列表

标签 swift firebase firebase-mlkit google-cloud-automl

我目前正在尝试构建一个使用谷歌的 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() 后我收到一个空列表我该如何解决这个错误?如果您需要更多信息,请告诉我

最佳答案

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

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() 函数中。否则它甚至没有检索到预测列表就运行上面的代码,从而导致 fatal error 。通过将其放入内部,我确保它已检索到预测列表,然后才运行上述代码以在列表中查找特定值。

关于swift - 不从远程 Firebase Automl 自定义模型中检索输出预测列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57747464/

相关文章:

uitableview - 将按日期分隔的部分添加到 Swift CoreData 中的 UITableView

node.js - 在 %RESOURCE_DIR% 中找不到 Firebase 函数部署问题 : package. json

swift - 将 xib 文件与 UIViewController 和自定义类链接

swift - “UICollectionView 必须使用非零布局参数初始化”?

swift - xcode swift 4 数字增加动画?

firebase - Firestore 规则使用 Flutter 客户端验证时间戳

java - 如何访问父节点的子节点而不影响同一节点中的其他子节点?

google-cloud-platform - 用于调用 AutoML Vision 模型预测的移动应用程序

java - 如何在 Windows 上的 java 中使用 Firebase ML-Kit 文本识别

ios - 有没有办法只在 VisionLabelDetector 中获得最高置信度的结果?