0
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)
}

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

4

1 回答 1

0

知道哪个标签的置信度最高的方法是调用max(by:)数组results。见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
            // ...
        }
    }
}
于 2020-05-09T14:47:28.203 回答