Tesseract OCR iOS 图片格式

Tesseract OCR iOS image format

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

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

  1. 从选择器中获取原始图像,并将其提供给 tesseract: 是否有效。
  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];
}

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

- (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"]];

image orientation 是不同的 images.When 你将图像加载到 engine:In 你的情况下,这两个图像都是作为具有不同方向的图像生成的引擎:

这是他们在引擎前面的样子:

原图:

Photoshop 图片:

如果你仔细观察,它们都会出现 differently.I 相信 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 个旋转。