Tesseract OCR iOS图像格式

5
我使用了Tesseract OCR iOS扫描文本,并且已经将其与项目中包含的照片配合使用。
但是,当传递一个UIImage从UIImagePickerController时,它就无法工作。我设置了这个简单的测试:
1. 从选择器中获取原始图像,并将其提供给tesseract:不起作用。 2. 将UIImage保存为JPEG,从应用程序容器中复制它,在项目中包含它并将其提供给tesseract:不起作用。 3. 在Photoshop中打开保存的UIImage,并再次保存(使用默认JPEG质量12设置没有更改)。在项目中包含它并将其提供给tesseract:起作用??
Tesseract确实识别出原始图像中正确数量的行,但是结果却是垃圾(我测试了几个示例测试)。一旦在Photoshop中保存图像,它就具有良好的识别率。
我简直无法弄清楚原始UIImage出了什么问题,而Photoshop则可以修复它。请帮忙!这里是图片:

将图片输入到Tesseract的代码:

- (void)recognizeWithImage:(UIImage *)image {
    G8RecognitionOperation *operation = [[G8RecognitionOperation alloc] initWithLanguage:@"dan"];
    operation.tesseract.image = image;
    self.imageView.image = image;
    operation.recognitionCompleteBlock = ^(G8Tesseract *recognizedTesseract) {
        NSLog(@"Result:\n%@", [recognizedTesseract recognizedText]);
    };
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
}

这是从相机获取图像的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [self dismissViewControllerAnimated:YES completion:nil];
    UIImage *originalImage = info[UIImagePickerControllerOriginalImage];

    NSData *dataForJPEGFile = UIImageJPEGRepresentation(originalImage, 1.0);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [paths[0] stringByAppendingPathComponent:@"temp_ocr_image_orig.jpg"];
    [dataForJPEGFile writeToFile:filePath atomically:YES];

    [self recognizeWithImage:originalImage];
}

测试两个图像文件:

[self recognizeWithImage:[UIImage imageNamed:@"temp_ocr_image_orig.jpg"]];
[self recognizeWithImage:[UIImage imageNamed:@"temp_ocr_image_photoshopped.jpg"]];

尝试使用不同的引擎设置,并尝试使用png图像。更多信息请访问http://www.raywenderlich.com/93276/implementing-tesseract-ocr-ios - Teja Nandamuri
大小相同,两个图像都可以在UIImageView中显示得很好。不同的引擎设置和语言包没有任何区别。PNG也没有帮助。 - Sune
正确:Tesseract 测试 1 - Sune
是的,我尝试了“eng”,但没有成功。 - Sune
我会逐个调用方法,并在将图像传递给引擎之前设置断点。我将查看两个图像的属性,如果它们的大小、比例、插入和变换相同,则说明它们是合适的。 - Teja Nandamuri
显示剩余8条评论
1个回答

3

imageorientation对于这两张图片是不同的。当你将这些图片加载到引擎中时,在你的情况下,这两个图像都会以不同的方向生成为引擎中的图像:

以下是它们在引擎前面的样子:

原始图片:

enter image description here

Photoshop图片:

enter image description here

仔细看,它们呈现出不同的方式。我相信UIImageJPEGRepresentation在某些情况下会做一些奇怪的事情,或者当你将image写入container时,图像会变成不同的方向。

你需要找到一种方法来修改从选择器或容器中获取的图像的方向。

我进行了一些组合,以获得与Photoshop图片相同的正确方向:

                                                   //image is the original image
UIImage *imageToDisplay =[UIImage imageWithCGImage:[image CGImage]
                    scale:1.0
              orientation: UIImageOrientationRight];

UIImage *newImage=  [UIImage imageWithCGImage:[imageToDisplay CGImage]
                     scale:1.0
              orientation: UIImageOrientationDown];


UIImage *newImage2=  [UIImage imageWithCGImage:[newImage CGImage]
                                        scale:1.0
                                  orientation: UIImageOrientationLeft];

//Now I get the correct orientation

// Set the image on which Tesseract should perform recognition
operation.tesseract.image = newImage2 ;

现在,您可以如预期地从OCR中获取文本。

您应该尝试在一行代码中获得正确的方向。我在此处使用了3个旋转。


那就是问题所在!我还没有弄清楚为什么,但只需要将方向设置为左侧即可。原始的UIImage是右侧取向的。我还没有测试过其他方向拍照,但如果我找到更好的处理方法,我会在这里进行评论一旦我做到了。 - Sune

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接