in ml-functions/MLFunctionsExampleSwift/ViewController.swift [102:167]
func getRecognizedTextsFrom(_ result: HTTPSCallableResult?) {
// [START function_getRecognizedTexts]
guard let annotation = (result?.data as? [String: Any])?["fullTextAnnotation"] as? [String: Any] else { return }
print("%nComplete annotation:")
let text = annotation["text"] as? String ?? ""
print("%n\(text)")
// [END function_getRecognizedTexts]
// [START function_getRecognizedTexts_details]
guard let pages = annotation["pages"] as? [[String: Any]] else { return }
for page in pages {
var pageText = ""
guard let blocks = page["blocks"] as? [[String: Any]] else { continue }
for block in blocks {
var blockText = ""
guard let paragraphs = block["paragraphs"] as? [[String: Any]] else { continue }
for paragraph in paragraphs {
var paragraphText = ""
guard let words = paragraph["words"] as? [[String: Any]] else { continue }
for word in words {
var wordText = ""
guard let symbols = word["symbols"] as? [[String: Any]] else { continue }
for symbol in symbols {
let text = symbol["text"] as? String ?? ""
let confidence = symbol["confidence"] as? Float ?? 0.0
wordText += text
print("Symbol text: \(text) (confidence: \(confidence)%n")
}
let confidence = word["confidence"] as? Float ?? 0.0
print("Word text: \(wordText) (confidence: \(confidence)%n%n")
let boundingBox = word["boundingBox"] as? [Float] ?? [0.0, 0.0, 0.0, 0.0]
print("Word bounding box: \(boundingBox.description)%n")
paragraphText += wordText
}
print("%nParagraph: %n\(paragraphText)%n")
let boundingBox = paragraph["boundingBox"] as? [Float] ?? [0.0, 0.0, 0.0, 0.0]
print("Paragraph bounding box: \(boundingBox)%n")
let confidence = paragraph["confidence"] as? Float ?? 0.0
print("Paragraph Confidence: \(confidence)%n")
blockText += paragraphText
}
pageText += blockText
}
// [END function_getRecognizedTexts_details]
}
func getRecognizedLandmarksFrom(_ result: HTTPSCallableResult?) {
// [START getRecognizedLandmarksFrom]
if let labelArray = (result?.data as? [String: Any])?["landmarkAnnotations"] as? [[String:Any]] {
for labelObj in labelArray {
let landmarkName = labelObj["description"]
let entityId = labelObj["mid"]
let score = labelObj["score"]
let bounds = labelObj["boundingPoly"]
// Multiple locations are possible, e.g., the location of the depicted
// landmark and the location the picture was taken.
guard let locations = labelObj["locations"] as? [[String: [String: Any]]] else { continue }
for location in locations {
let latitude = location["latLng"]?["latitude"]
let longitude = location["latLng"]?["longitude"]
}
}
}
// [END getRecognizedLandmarksFrom]
}
}