How to adapt the swift google sample code of (FACE_DETECTION) and (LABEL_DETECTION) to use the OCR API (TEXT_DETECTION) so far I did not find any sample code or explanation how to use the google OCR on iOS, I have changed the type of the API request to TEXT_DETECTION but it does not gives a reply any help?
class GoogleCloudVisionOCR {
let session = URLSession.shared
var googleAPIKey = ""
var googleURL: URL {
return URL(string: "https://vision.googleapis.com/v1/images:annotate?key=\(googleAPIKey)")!
}
var textFromImageArray: [String]?
public func GetTextFromImage(imageURLString: String, handler: @escaping (String) -> Void) {
guard let url = URL(string: imageURLString) else { print("no URL in TextFromImageRequest.GetTextFromImage"); return}
let data = try? Data(contentsOf: url)
if let unwrappedData = data {
guard let image = UIImage(data: unwrappedData) else { print("no image from URL in TextFromImageRequest.GetTextFromImage"); return}
let binaryImageData = base64EncodeImage(image)
createRequest(with: binaryImageData, handler: { (result) in
//DEBUG PRINT print("RESULT = \(result)")
handler(result)
})
} else { print("unwrapped data nil in TextFromImageRequest.GetTextFromImage") }
}
// Base64 encode the image and create the request
//createRequest(with: binaryImageData)
private func base64EncodeImage(_ image: UIImage) -> String {
var imagedata = UIImagePNGRepresentation(image)
// Resize the image if it exceeds the 2MB API limit
if ((imagedata?.count)! > 2097152) {
let oldSize: CGSize = image.size
let newSize: CGSize = CGSize(width: 800, height: oldSize.height / oldSize.width * 800)
imagedata = resizeImage(newSize, image: image)
}
return imagedata!.base64EncodedString(options: .endLineWithCarriageReturn)
}
private func resizeImage(_ imageSize: CGSize, image: UIImage) -> Data {
UIGraphicsBeginImageContext(imageSize)
image.draw(in: CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
let resizedImage = UIImagePNGRepresentation(newImage!)
UIGraphicsEndImageContext()
return resizedImage!
}
private func createRequest(with imageBase64: String, handler: @escaping (String) -> Void) {
// Create our request URL
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")
// Build our API request
let jsonRequest = [
"requests": [
"image": [
"content": imageBase64
],
"features": [
[
"type": "LABEL_DETECTION",
"maxResults": 10
],
[
"type": "FACE_DETECTION", //added by me
"maxResults": 10,
]
]
]
]
let jsonObject = JSON(jsonDictionary: jsonRequest)
//let jsonObject = JSONSerialization.jsonObject(with: jsonRequest, options: []) as? [String : Any]
// Serialize the JSON
guard let data = try? jsonObject.rawData() else {
return
}
request.httpBody = data
// Run the request on a background thread
DispatchQueue.global().async { self.runRequestOnBackgroundThread(request, handler: { (result) in
handler(result)
}) }
}
private func runRequestOnBackgroundThread(_ request: URLRequest, handler: @escaping (String) -> Void) {
// run the request
let task: URLSessionDataTask = session.dataTask(with: request) { (data, response, error) in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "")
return
}
self.analyzeResults(data, handler: { (result) in
handler(result)
})
}
task.resume()
}
private func analyzeResults(_ dataToParse: Data, handler: @escaping (String) -> Void) {
var textArray: [String] = []
// Update UI on the main thread
DispatchQueue.main.async(execute: {
// Use SwiftyJSON to parse results
let json = JSON(data: dataToParse)
let errorObj: JSON = json["error"]
//Check for errors
if (errorObj.dictionaryValue != [:]) {
print("Error code \(errorObj["code"]): \(errorObj["message"])")
} else {
//Parse the response
let responses: JSON = json["responses"][0]
//Get text
let textAnnotations: JSON = responses["textAnnotations"]
//DEBUG PRINT print(textAnnotations)
let numTextAnnos: Int = textAnnotations.count
if numTextAnnos > 0 {
for index in 0..<numTextAnnos {
let text = textAnnotations[index]["description"].stringValue
textArray.append(text)
//DEBUG PRINT print("TEXT = \(text)")
}
let finalString = textArray.joined(separator: " ")
let filteredString = finalString
handler(filteredString)
}
}
})
}
}