我正在使用 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 提供图像时的结果:
直接从相机拍摄图像时的结果:
在这两种情况下,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,现在设备相机拍摄的图像提供以下输出:
还是不准确。如果有人可以指出如何改进这一点,那就太好了。
最佳答案
在 tesseract 的官方 github 源上,提到了各种预处理方法以及这些措施,我建议使用 .tiff 图像而不是 .jpg 或 .png,因为使用 tiff 以外的任何其他类型的图像都会压缩图像并减少它使质量二值化。
关于ios - Tesseract OCR 无法识别从设备拍摄的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47946808/