gpt4 book ai didi

IOS OCR tesseract 在为 nil 并使用 ACR 后不释放内存

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:47 26 4
gpt4 key购买 nike

我花了超过 24 小时来调试和解决 tesseract 中的问题,问题是我为多个图像循环下面的函数,每次我跟踪内存,发现每次调用时内存都会增加下划线

Tesseract* tesseract = [[Tesseract alloc] initWithLanguage:@"eng+ita"];

并且不受下面一行的影响

tesseract = nil;

下面是调用的完整函数

    -(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
}

更新 1:

经过深入排查,我发现setimage的tesseract代码是原因,代码如下,我需要知道我必须更新哪些代码才能解决这个问题

- (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;
}

请支持我解决这个问题

非常感谢

最佳答案

您在紧密循环中执行此操作,并且在每个应用程序运行循环后释放内存。你需要做的是

// your loop here 
for (int i = 0; i < n; i++) {
@autoreleasepool {
// perform memory intensive task here;
}
}

所以每次在你完成繁重的任务后,内存自动释放池都会被耗尽。

阅读此处了解更多详情:

Objective-C: Why is autorelease (@autoreleasepool) still needed with ARC?

关于IOS OCR tesseract 在为 nil 并使用 ACR 后不释放内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26704952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com