I need to do text recognition on hundreds of images one at a time, but each time, memory grows by ~25mb+. I've searched the internet but can't find what is causing the memory retention or how to release it. By putting in breaks, I can see that the jump in size occurs with each call to imageRequestHandler.perform(). Below is the relevant code. Any suggestions?
func textRecognition(image:CGImage) {
let textRecognitionRequest = VNRecognizeTextRequest(
completionHandler: self.handleDetectedText)
textRecognitionRequest.recognitionLevel = .accurate
textRecognitionRequest.recognitionLanguages = ["en_US"]
textRecognitionRequest.usesLanguageCorrection = false
// request handler
let textRequest = [textRecognitionRequest]
let imageRequestHandler = VNImageRequestHandler(cgImage: image, orientation: .up, options: [:])
DispatchQueue.global(qos: .userInitiated).async {
do {
// perform request
try imageRequestHandler.perform(textRequest)
} catch let error {
print("Error \(error)")
}
}
}
func handleDetectedText(request: VNRequest?, error:Error?){
if let error = error { print("ERROR: \(error)"); return }
guard let results = request?.results, results.count > 0 else {
DispatchQueue.main.async {
self.result_field.isEnabled=false
self.result_field.text = "Scan failed - Retry"
let desc = NSMutableAttributedString(string: "Retake Photo", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 22, weight: .regular)])
self.take_button.setAttributedTitle(desc, for: UIControl.State.normal)
self.take_button.isHidden = false
self.take_button.isEnabled = true
}
return // code to process the text replaced by the 'return' statement
}}}