ios - Swift 3 - Tesseract 识别任何图像处理的抛出错误

标签 ios swift swift3 avfoundation tesseract

我正在使用 Swift 3 开发一个应用程序,用户可以在其中拍照并使用 Tesseract OCR 识别其中的文本。

以下代码块有效。

func processPhoto() {
    if let tesseract = G8Tesseract(language: "eng") {
        tesseract.delegate = self

        // this is the resulting picture gotten after running the capture delegate
        tesseract.image = stillPicture.image!
        tesseract.recognize()
    }
}

但是,如果我尝试完全操纵图片 (stillPicture.image!),我会收到以下错误:

Error in pixCreateHeader: depth must be {1, 2, 4, 8, 16, 24, 32}
Error in pixCreateNoInit: pixd not made
Error in pixCreate: pixd not made
Error in pixGetData: pix not defined
Error in pixGetWpl: pix not defined
2017-03-13 11:13:05.336796 ProjectName[35238:9127211] Cannot convert image to Pix with bpp = 64
Error in pixSetYRes: pix not defined
Error in pixGetDimensions: pix not defined
Error in pixGetColormap: pix not defined
Error in pixClone: pixs not defined
Error in pixGetDepth: pix not defined
Error in pixGetWpl: pix not defined
Error in pixGetYRes: pix not defined
Please call SetImage before attempting recognition.Please call SetImage before attempting recognition.2017-03-13 11:13:05.343568 EOB-Reader[35238:9127211] No recognized text. Check that -[Tesseract setImage:] is passed an image bigger than 0x0.

我为操作图片所做的一些事情是旋转它:

// Rotate taken picture
let orig_image = stillPicture.image!

let new_image_canvas = UIGraphicsImageRenderer(size: CGSize(width: stillPicture.image!.size.height,
                                                                  height: stillPicture.image!.size.width))
let new_image = new_image_canvas.image { _ in
    let curr_context = UIGraphicsGetCurrentContext()!
    curr_context.translateBy(x: 0, y: stillPicture.image!.size.width)
    curr_context.rotate(by: -.pi/2)
    stillPicture.image!.draw(at: .zero)
}

tesseract.image = new_image

如果我这样做,砰!出现上面的错误。

我做的另一个操作是裁剪图像的一部分。

let finalImage : UIImage

let crop_section = CGRect(x: 590.0, y: 280.0, width: 950.0, height: 550.0)

let cg_image = stillPicture.image!.cgImage?.cropping(to: crop_section)
finalImage = UIImage(cgImage: cg_image!)      

tesseract.image = final_image

再次,砰!出现错误。知道为什么会发生这种情况以及为什么我的图像处理会导致问题吗?谢谢你的帮助!

最佳答案

无论您对图像进行何种转换,它都会以 Tesseract 无法理解的格式保留下来。 Tesseract 使用 Leptonica 库来处理图像格式,Leptonica 只能理解特定格式的图像。

第一行:

Error in pixCreateHeader: depth must be {1, 2, 4, 8, 16, 24, 32}

已经是一个关于错误是什么的重要提示。位深度意味着每个像素有多少位。例如,24 位图像通常是 RGB - 红色、绿色和蓝色各有 8 位(或一个字节)——总共 24 位。 32 位用于 ARGB(RGB+alpha channel )。 1 位为单色。

参见 http://search.cpan.org/dist/Image-Leptonica/lib/Image/Leptonica/Func/pix1.pm#pixCreateHeader - pixCreateHeader 是一个 leptopnica 函数。

因此请尝试以下操作 - 将图像保存到文件中,然后在某些图像编辑器中打开它并检查它是什么类型的图像,尤其是。位深度。

显然您的图像使用了一些奇怪的位深度。另请参阅 Node.js 20x slower than browser (Safari) with Tesseract.Js ,因为这是我能找到的唯一问题,其中还提到了 error in pixCreateHeader: depth must be {1, 2, 4, 8, 16, 24, 32}

关于ios - Swift 3 - Tesseract 识别任何图像处理的抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42767287/

相关文章:

swift - 当传入的 json 不完整时,我的 swift 应用程序崩溃。我该如何处理?

ios - 如何将 ipa 文件临时部署到我的 iPhone

ios - 如何在不重置审阅队列的情况下重新提交二进制文件

swift - 从 MacOS App Swift 启动 sudo 命令

swift - 快速更新标签中的 slider 值

ios - 常量 'spacesLeft' 被推断为类型 '()' ,这可能是 Swift 意想不到的

ios - 带有 swift 3 和 Alamofire 的 JSON 字符串上的 Unicode 字符

iPhone : In-App Purchase(s) must be submitted with a new app version

iphone - iPhone 中的 SOAP 和 REST 服务

Objective-C 类型别名优先于 Swift 类