Tesseract OCR无法识别从设备

时间:2017-12-22 20:04:13

标签: ios ocr tesseract

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

我坚持让Tesseract检测图像中的文字。

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

-(void)startTess:(UIImage *)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 drawInRect:(CGRect){0, 0, chosenImage.size}];
    chosenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

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

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

编辑:

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

enter image description here

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

1 个答案:

答案 0 :(得分:0)

在tesseract的官方github来源上,提到了各种预处理方法,我建议使用.tiff图像而不是.jpg或.png的那些措施,因为使用除tiff以外的任何其他图像都会压缩图像并减少它使质量二进制化。