不确定问题是什么,因为我的代码只是整夜停止工作,但是Google Vision上的文本检测要么返回nil,要么返回主题上不存在的单词。
这是我的请求功能:
func createRequest(with imageBase64: String) {
let jsonRequest = [
"requests": [
"image": [
"content": imageBase64 ],
"features": [
["type": "TEXT_DETECTION"],
["type": "IMAGE_PROPERTIES"]
]
]
]
let jsonData = try? JSONSerialization.data(withJSONObject: jsonRequest)
var request = URLRequest(url: googleURL)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue(Bundle.main.bundleIdentifier ?? "", forHTTPHeaderField: "X-Ios-Bundle-Identifier")
request.httpBody = jsonData
DispatchQueue.global().async {
let task: URLSessionDataTask = self.URLsession.dataTask(with: request) { (encodedObject, response, error) in
guard let data = encodedObject, error == nil else {
print(error?.localizedDescription ?? "no data")
return
}
self.analyzeResults(data)
}
task.resume()
}
}
我的分析结果功能的一部分:
func analyzeResults(_ data: Data) {
DispatchQueue.main.async {
guard let response = try? JSONDecoder().decode(Root.self, from: data) else { return }
guard let responseArray = response.responses else { return }
print(response)