Tesseract OCR iOS图像格式

Sun*_*une 5 tesseract objective-c uiimage ios

我已经使用Tesseract OCR iOS扫描文本,我已经使用它来处理项目中包含的照片.

但是当从UIImagePickerController传递一个UIImage时,它不起作用.我设置了这个简单的测试:

  1. 从选择器以原始图像,并且将其提供给正方体:难道没有工作.
  2. 保存的UIImage为JPEG,从应用程序容器复制,它包括在项目和饲料它的Tesseract:难道没有工作.
  3. 在photoshop中打开保存的UIImage,然后再次保存(默认的JPEG质量12设置没有变化).将它包含在项目中,将其提供给tesseract:Works?!?

Tesseract确实识别出原始中正确的行数,但是作为垃圾(我测试了几个示例测试).保存在Photoshop中后,图像具有良好的识别率.

我简直无法弄清楚Photoshop以某种方式修复的原始UIImage有什么问题.请帮忙!

这是图像:

将图像输送到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];
}
Run Code Online (Sandbox Code Playgroud)

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

- (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];
}
Run Code Online (Sandbox Code Playgroud)

并测试了两个图像文件:

[self recognizeWithImage:[UIImage imageNamed:@"temp_ocr_image_orig.jpg"]];
[self recognizeWithImage:[UIImage imageNamed:@"temp_ocr_image_photoshopped.jpg"]];
Run Code Online (Sandbox Code Playgroud)

Tej*_*uri 3

image orientation两个图像的情况是不同的。当您将图像加载到引擎中时:在您的情况下,两个图像都会生成为与引擎具有不同方向的图像:

以下是它们在发动机前面的样子:

原图:

在此输入图像描述

Photoshop 图像:

在此输入图像描述

如果你仔细观察,它们都会以不同的方式呈现。我相信UIImageJPEGRepresentation正在做一些疯狂的事情,或者当你将 写入imagecontainer,图像会进入不同的方向。

您需要修改从选择器或容器获取的图像的方向。

我做了一些组合以获得 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 ;
Run Code Online (Sandbox Code Playgroud)

现在您可以按预期从 OCR 中获取文本。

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