OGeek|极客世界-中国程序员成长平台

标题: ios - Tesseract OCR 无法识别从设备拍摄的图像 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:30
标题: ios - Tesseract OCR 无法识别从设备拍摄的图像

我正在使用 https://github.com/gali8/Tesseract-OCR-iOS/ 制作一个检测名片上的文本的应用。

我一直坚持让 Tesseract 检测图像中的文本。

如果我通过代码传递图像,Tesseract 能够检测到它。如果我提供从相机拍摄的图像,则 tesseract 无法识别它。

-(void)startTessUIImage *)img{

 G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage"eng"];
 tesseract.delegate = self;
 tesseract.engineMode=G8OCREngineModeTesseractCubeCombined;

 // Optional: Limit the character set Tesseract should try to recognize from
 tesseract.charWhitelist = @"@.,()-,abcdefghijklmnopqrstuvwxyz0123456789";

 // Specify the image Tesseract should recognize on
 tesseract.image = [img g8_blackAndWhite];

 // Optional: Limit the area of the image Tesseract should recognize on to a rectangle
 CGRect tessRect = CGRectMake(0, 0, img.size.width, img.size.height);
 tesseract.rect = tessRect;

 // Optional: Limit recognition time with a few seconds
 tesseract.maximumRecognitionTime = 4.0;

 // Start the recognition
 [tesseract recognize];

 // Retrieve the recognized text
 NSLog(@"text %@", [tesseract recognizedText]);

 // You could retrieve more information about recognized text with that methods:
 NSArray *characterBoxes = [tesseract recognizedBlocksByIteratorLevel:G8PageIteratorLevelSymbol];
 NSArray *paragraphs = [tesseract recognizedBlocksByIteratorLevel:G8PageIteratorLevelParagraph];
 NSArray *characterChoices = tesseract.characterChoices;
 UIImage *imageWithBlocks = [tesseract imageWithBlocks:characterBoxes drawText:YES thresholded:NO];

 self.imgView.image = imageWithBlocks;

 NSString * result = [[characterBoxes valueForKey"description"] componentsJoinedByString"\n"];

 _txtView.text=result;


}

从 .xcassets 提供图像时的结果:

enter image description here

直接从相机拍摄图像时的结果:

enter image description here

在这两种情况下,Tesseract 都可以识别带有一些随机字符的空白空间。我在两张图片中都标记了该区域(图片的左上角)。

我确保从设备相机拍摄的图像具有向上的方向,因为一些报告称 Tesseract 无法识别从相机拍摄的图像,因为它具有 180 度偏移。

UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];

// Redraw the image (if necessary) so it has the corrent orientation:
if (chosenImage.imageOrientation != UIImageOrientationUp) {
    UIGraphicsBeginImageContextWithOptions(chosenImage.size, NO, chosenImage.scale);
    [chosenImage drawInRectCGRect){0, 0, chosenImage.size}];
    chosenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

调试此问题并继续前进的最佳方法是什么?

我在 git 上提交了一个问题: https://github.com/gali8/Tesseract-OCR-iOS/issues/358

编辑:

我已将迭代器级别更改为G8PageIteratorLevelTextline,现在设备相机拍摄的图像给出以下输出:

enter image description here

仍然不准确。如果有人可以指出如何改进这一点,那就太好了。



Best Answer-推荐答案


在 tesseract 的官方 github 源中,提到了各种预处理方法,除了这些措施,我建议使用 .tiff 图像而不是 .jpg 或 .png,因为使用除 tiff 之外的任何其他类型的图像会压缩图像并减少它将质量二值化。

关于ios - Tesseract OCR 无法识别从设备拍摄的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47946808/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4