【问题标题】:How do I extract the label with the highest confidence value and print it out in Swift?如何提取具有最高置信度值的标签并在 Swift 中打印出来?
【发布时间】:2020-08-24 22:54:07
【问题描述】:
private func showResults(_ results: [(label: String, confidence: 
Float)]?) {
    var resultsText = Constants.failedToDetectObjectsMessage
    if let results = results {
      resultsText = results.reduce("") { (resultString, result) -> 
String in
        let (label, confidence) = result
        return resultString + "\(label): \(String(describing: 
confidence))\n"
      }
    }
    resultsAlertController.message = resultsText
    resultsAlertController.popoverPresentationController?.sourceRect = self.annotationOverlayView.frame
    resultsAlertController.popoverPresentationController?.sourceView = self.annotationOverlayView
    present(resultsAlertController, animated: true, completion: nil)
    print(resultsText)
}

这是我尝试过的示例代码。如何提取置信度最高的标签并打印出来?

【问题讨论】:

  • 您好,欢迎来到 StackOverflow。如果您可以添加实际代码而不是屏幕截图,以及您已经尝试过的内容,这将有助于获得有意义的答案。
  • @matt 我认为这就是 OP 所要求的:如何仅显示具有最高置信度的标签。

标签: swift firebase-mlkit vision google-mlkit


【解决方案1】:

知道哪个标签的置信度最高的方法是在results 数组上调用max(by:)。见https://developer.apple.com/documentation/swift/sequence/2906531-max

private func showResults(_ results: [(label: String, confidence: Float)]?) {
    if let results = results {
        let biggest = results.max { $0.confidence < $1.confidence }
        if let biggest = biggest {
            let (label, confidence) = biggest
            // ...
        }
    }
}

【讨论】:

    猜你喜欢
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    相关资源
    最近更新 更多