Tesseract OCR w / iOS& Swift返回错误或乱码 [英] Tesseract OCR w/ iOS & Swift returns error or gibberish

查看:179
本文介绍了Tesseract OCR w / iOS& Swift返回错误或乱码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用本教程让Tesseract OCR与Swift一起工作: http://www.piterwilson.com/blog/2014/10/18/minimal-tesseact-ocr-setup-in-swift/

I used this tutorial to get Tesseract OCR working with Swift: http://www.piterwilson.com/blog/2014/10/18/minimal-tesseact-ocr-setup-in-swift/

如果我上传演示图像并调用

It works fine if I upload the demo image and call

 tesseract.image = UIImage(named: "image_sample.jpg");

但如果我使用相机代码拍摄同一张照片并致电

But if I use my camera code and take a picture of that same image and call

 tesseract.image = self.image.blackAndWhite();

结果是胡言乱语

s I 5E251:Ec
' - 。 -7.//:E*髧
ag:_ {:7 IC'
J 7 iii-1553'
:fi zzle - '; - :

s I 5E251 :Ec ‘-. —7.//:E*髧 a g :_{:7 IC‘ J 7 iii—1553‘ : fizzle —‘;-—:

; 〜:〜。/: - : - ' -

; ~:~./: -:-‘-

' - :〜£:':_ - '〜':

‘- :~£:': _-'~‘:

:37%; §:' - _

: 37%; §:‘—_

::::: E 7 ,;。
1f:,:〜 - ,

: ::::E 7,;. 1f:,:~ ——,

或者它返回BAD_EXC_ACCESS错误。我无法重现为什么它会给出错误或乱码的原因。这是我的相机拍摄代码(拍照())和处理步骤(nextStepTapped()):

Or it returns a BAD_EXC_ACCESS error. I haven't been able to reproduce the reasoning behind why it gives the error or the gibberish. This is the code of my camera capture (photo taken()) and the processing step (nextStepTapped()):

 @IBAction func photoTaken(sender: UIButton) {

    var videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo)

    if videoConnection != nil {

        // Show next step button
        self.view.bringSubviewToFront(self.nextStep)
        self.nextStep.hidden = false

        // Secure image
        stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
            (imageDataSampleBuffer, error) -> Void in
                var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)

                self.image = UIImage(data: imageData)

                //var dataProvider = CGDataProviderCreateWithCFData(imageData)
                //var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault)
                //self.image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.Right)

        }

        // Freeze camera preview
        captureSession.stopRunning()

    }

}

@IBAction func nextStepTapped(sender: UIButton) {

    // Save to camera roll & proceeed
    //UIImageWriteToSavedPhotosAlbum(self.image.blackAndWhite(), nil, nil, nil)
    //UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil)

    // OCR

    var tesseract:Tesseract = Tesseract();
    tesseract.language = "eng";
    tesseract.delegate = self;
    tesseract.image = self.image.blackAndWhite();
    tesseract.recognize();

    NSLog("%@", tesseract.recognizedText);

}

图像保存到相机胶卷并且完全清晰可辨我取消注释评论的行。不知道为什么它不起作用。如果将图像上的文本作为支持文件直接上传到Xcode中,则读取图像上的文本没有问题,但是如果我在屏幕上拍摄完全相同的图像,则无法读取它。

The image saves to the Camera Roll and is completely legible if I uncomment the commented lines. Not sure why it won't work. It has no problem reading the text on the image if it's uploaded directly into Xcode as a supporting file, but if I take a picture of the exact same image on my screen then it can't read it.

推荐答案

偶然发现本教程: http://www.raywenderlich.com/93276/implementing-tesseract-ocr-ios

碰巧提到缩放图像。他们选择最大尺寸为640.我将我的照片视为640x480,所以我认为我不需要缩放它们,但我认为这段代码基本上重绘了图像。出于某种原因,现在我的照片OCR相当不错。我仍然需要处理较小文本的图像处理,但它适用于大文本。通过这个缩放功能运行我的图像,我很高兴。

It happened to mention scaling the image. They chose the max dimension as 640. I was taking my pictures as 640x480, so I figured I didn't need to scale them, but I think this code essentially redraws the image. For some reason now my photos OCR fairly well. I still need to work on image processing for smaller text, but it works perfectly for large text. Run my image through this scaling function and I'm good to go.

  func scaleImage(image: UIImage, maxDimension: CGFloat) -> UIImage {

   var scaledSize = CGSize(width: maxDimension, height: maxDimension)
   var scaleFactor: CGFloat

   if image.size.width > image.size.height {
      scaleFactor = image.size.height / image.size.width
      scaledSize.width = maxDimension
      scaledSize.height = scaledSize.width * scaleFactor
   } else {
      scaleFactor = image.size.width / image.size.height
      scaledSize.height = maxDimension
      scaledSize.width = scaledSize.height * scaleFactor
   }

   UIGraphicsBeginImageContext(scaledSize)
   image.drawInRect(CGRectMake(0, 0, scaledSize.width, scaledSize.height))
   let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
   UIGraphicsEndImageContext()

 return scaledImage
}

这篇关于Tesseract OCR w / iOS& Swift返回错误或乱码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆