IOS OCR tesseract does not free memory after zero and uses ACR
I have spent over 24 hours on a debugging and troubleshooting issue in tesseract, the problem is that I execute the following function on multiple images and every time I keep track of the memory and I found that the memory grows every time I call below strings
Tesseract* tesseract = [[Tesseract alloc] initWithLanguage:@"eng+ita"];
and it is not affected by the bottom line
tesseract = nil;
below is a complete function called
-(void)recognizeImageWithTesseract:(UIImage *)img
{
UIImage *testb = [img blackAndWhite];
Tesseract* tesseract = [[Tesseract alloc] initWithLanguage:@"eng+ita"];
tesseract.delegate = self;
[tesseract setVariableValue:@"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+-/*._=':!)(" forKey:@"tessedit_char_whitelist"]; //limit search
[tesseract setImage:testb];
[tesseract recognize];
recognizedText = [tesseract recognizedText];
tesseract = nil; //deallocate and free all memory
}
UPDATE 1:
after deep troubleshooting, i found the tesseract setimage code is the cause, the code is as below, i need to know which code i should update to fix this issue.
- (void)setImage:(UIImage *)image {
if (image == nil || image.size.width <= 0 || image.size.height <= 0) {
NSLog(@"WARNING: Image has not size!");
return;
}
self.imageSize = image.size; //self.imageSize used in the characterBoxes method
int width = self.imageSize.width;
int height = self.imageSize.height;
CGImage *cgImage = image.CGImage;
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));
_pixels = CFDataGetBytePtr(data);
size_t bitsPerComponent = CGImageGetBitsPerComponent(cgImage);
size_t bitsPerPixel = CGImageGetBitsPerPixel(cgImage);
size_t bytesPerRow = CGImageGetBytesPerRow(cgImage);
tesseract::ImageThresholder *imageThresholder = new tesseract::ImageThresholder();
assert(bytesPerRow < MAX_INT32);
{
imageThresholder->SetImage(_pixels,width,height,(int)(bitsPerPixel/bitsPerComponent),(int)bytesPerRow);
_tesseract->SetImage(imageThresholder->GetPixRect());
}
imageThresholder->Clear();
CFRelease(data);
delete imageThresholder;
imageThresholder = nil;
}
please support me to resolve this issue.
thanks alot
source to share
You do this in a hard loop, and memory is freed after each application startup cycle. What you need to do is
// your loop here
for (int i = 0; i < n; i++) {
@autoreleasepool {
// perform memory intensive task here;
}
}
So, every time the autorun memory pool is flushed after a heavy task.
Read more here:
Objective-C: Why is the abstract (@autoreleasepool) still needed with ARC?
source to share