为什么Tesseract OCR库(iOS)根本不能识别文本?

时间:2021-09-29 23:26:25

I'm trying to use Tesseract OCR library in my iOS application. I downloaded tesseract-ios library from github and when I tried to recognize a simple text image I got garbage instead. Here is an image of what I tried to recognize:

我正在尝试在我的iOS应用程序中使用Tesseract OCR库。我从github上下载了tesserac -ios库,当我试图识别一个简单的文本图像时,我得到的却是垃圾。这是我试图识别的一个图像:

为什么Tesseract OCR库(iOS)根本不能识别文本?

I got unreadable text:

我有阅读文本:

T0I1101T0W KIR1 H1I1101T0W KIR1 H1I1101T0W CIBEPS H1 ES PBHY P306 EHH11 133I R1 11335 11I1H1 19 13S SYIL 3B19 M H300H1911 H1113 AIR1 J1 OIII 3I9SH5H133IS 13V9 I1 Q1H211 E015 19 W331 H1 111SW

我想知道的是,我想知道的是,我想知道的是,我想说的是,我想说的是,这是一个非常重要的问题。

Why Tesseract can't recognise even simple image? Here is code which I used to instantiate Tesseract:

为什么Tesseract连简单的图像都不能识别?下面是我用来实例化Tesseract的代码:

Tesseract* tesseractObject = [[Tesseract alloc] initWithDataPath:@"tessdata" language:@"eng"];
[tesseractObject setVariableValue:@"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" forKey:@"tessedit_char_whitelist"];
[tesseractObject setImage:image];
[tesseractObject recognize];
NSLog(@"RECOGNISED= %@" , [tesseractObject recognizedText]);

Here is my project structure:

以下是我的项目结构:

为什么Tesseract OCR库(iOS)根本不能识别文本?

I added English testdata folder by reference. So what am I doing wrong? How can I fix this?

我添加了英语testdata文件夹作为参考。我做错了什么?我该怎么解决这个问题呢?

5 个解决方案

#1


19  

Make sure you have the latest tessdata file from Google code

确保您有来自谷歌代码的最新tessdata文件

http://code.google.com/p/tesseract-ocr/downloads/list

http://code.google.com/p/tesseract-ocr/downloads/list

This will provide you with a list of tessdata files that you need to download and include in your app if you haven't already. In your case you will need tesseract-ocr-3.02.eng.tar.gz as you are looking for the English language files

这将为您提供一个需要下载的tessdata文件列表,如果您还没有下载的话,它将包含在您的应用程序中。在你的情况下,你需要的是tesseract- ec -3.02.eng.tar。您正在查找英语语言文件

The following article will show you where you need to install it. I read through this tutorial when I built my first Tesseract project and found it really useful

下面的文章将向您展示您需要在何处安装它。当我构建我的第一个Tesseract项目时,我阅读了本教程并发现它非常有用

http://lois.di-qual.net/blog/install-and-use-tesseract-on-ios-with-tesseract-ios/

http://lois.di-qual.net/blog/install-and-use-tesseract-on-ios-with-tesseract-ios/

#2


21  

You are using the option tessedit_char_whitelist with the value "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" which limits the character recognition to this list only. However the image that you want to process contains lower case characters, if you want to use this option you will have to include lower cases char too.

您使用的选项是tessedit_char_whitelist,它的值为“0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ”,该值只将字符识别限制在此列表中。然而,您想要处理的图像包含小写字符,如果您想使用这个选项,您将不得不包括小写字符。

[tesseractObject setVariableValue:@"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" forKey:@"tessedit_char_whitelist"];

#3


12  

Like Adam said, if you want good results, you'll have to do some image processing and configure some settings (white-listing certain characters, etc).

就像Adam说的,如果你想要好的结果,你需要做一些图像处理和配置一些设置(白名单某些字符,等等)。

For anyone else stumbling upon this question, I've put together a sample project here that does some white-listing and image processing:https://github.com/mstrchrstphr/OCR-iOS-Example

对于任何遇到这个问题的人,我在这里创建了一个示例项目,它执行一些白列表和图像处理:https://github.com/mstrchrstphr/ocr-ios示例

#4


0  

为什么Tesseract OCR库(iOS)根本不能识别文本?

and my output is

我的输出是

为什么Tesseract OCR库(iOS)根本不能识别文本?

Solution :

解决方案:

 tesseract.language = @"eng+fra";

tesseract.pageSegmentationMode = G8PageSegmentationModeAuto;
tesseract.engineMode  = G8OCREngineModeTesseractCubeCombined;
tesseract.image = [image.image g8_blackAndWhite];

tesseract.maximumRecognitionTime = 60.0;
[tesseract recognize];

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

reco_area.text = [tesseract recognizedText];

for tessdata click here

对于tessdata点击这里

#5


0  

whatever @ Adam Richardson explained is correct along with that add this 1) scaleimage method for increase size of the image(dimensions increase)

任何@ Adam Richardson解释的都是正确的加上这个1)scaleimage方法来增加图像的尺寸(尺寸增加)

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

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.draw(in: CGRect(x: 0, y: 0, width: scaledSize.width, height: scaledSize.height))
    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return scaledImage!
}

2) store this eng.traineddata language file in filemanager

2)存储这个eng。在filemanager中的traineddata语言文件

 func storeLanguageFile() throws{
    var fileManager: FileManager = FileManager.default
    let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
    let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
    let docDirectory = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)[0] as NSString
    let path: String = docDirectory.appendingPathComponent("/tessdata/eng.traineddata")
    if fileManager.fileExists(atPath: path){
        var data: NSData = NSData.dataWithContentsOfMappedFile((Bundle.main.resourcePath?.appending("/tessdata/eng.traineddata"))!)! as! NSData
        var error: NSError
        try FileManager.default.createDirectory(atPath: docDirectory.appendingPathComponent("/tessdata"), withIntermediateDirectories: true, attributes: nil)
        data.write(toFile: path, atomically: true)
    }
}

3) after that you can use https://github.com/BradLarson/GPUImage for increase clarity of the image

3)之后,您可以使用https://github.com/BradLarson/GPUImage来增加图像的清晰度

you can use this

你可以使用这个

func preprocessedImage(for tesseract: G8Tesseract!, sourceImage: UIImage!) -> UIImage! {
    var stillImageFilter: GPUImageAdaptiveThresholdFilter = GPUImageAdaptiveThresholdFilter()
    stillImageFilter.blurRadiusInPixels = 4.0
    var filterImage: UIImage = stillImageFilter.image(byFilteringImage: sourceImage)
    return filterImage
}

these 3 steps will help you to increase the accuracy of the tesseract upto 60 ~ 70 %

这三个步骤将帮助您将tesseract的准确度提高到60% ~ 70%

#1


19  

Make sure you have the latest tessdata file from Google code

确保您有来自谷歌代码的最新tessdata文件

http://code.google.com/p/tesseract-ocr/downloads/list

http://code.google.com/p/tesseract-ocr/downloads/list

This will provide you with a list of tessdata files that you need to download and include in your app if you haven't already. In your case you will need tesseract-ocr-3.02.eng.tar.gz as you are looking for the English language files

这将为您提供一个需要下载的tessdata文件列表,如果您还没有下载的话,它将包含在您的应用程序中。在你的情况下,你需要的是tesseract- ec -3.02.eng.tar。您正在查找英语语言文件

The following article will show you where you need to install it. I read through this tutorial when I built my first Tesseract project and found it really useful

下面的文章将向您展示您需要在何处安装它。当我构建我的第一个Tesseract项目时,我阅读了本教程并发现它非常有用

http://lois.di-qual.net/blog/install-and-use-tesseract-on-ios-with-tesseract-ios/

http://lois.di-qual.net/blog/install-and-use-tesseract-on-ios-with-tesseract-ios/

#2


21  

You are using the option tessedit_char_whitelist with the value "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" which limits the character recognition to this list only. However the image that you want to process contains lower case characters, if you want to use this option you will have to include lower cases char too.

您使用的选项是tessedit_char_whitelist,它的值为“0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ”,该值只将字符识别限制在此列表中。然而,您想要处理的图像包含小写字符,如果您想使用这个选项,您将不得不包括小写字符。

[tesseractObject setVariableValue:@"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" forKey:@"tessedit_char_whitelist"];

#3


12  

Like Adam said, if you want good results, you'll have to do some image processing and configure some settings (white-listing certain characters, etc).

就像Adam说的,如果你想要好的结果,你需要做一些图像处理和配置一些设置(白名单某些字符,等等)。

For anyone else stumbling upon this question, I've put together a sample project here that does some white-listing and image processing:https://github.com/mstrchrstphr/OCR-iOS-Example

对于任何遇到这个问题的人,我在这里创建了一个示例项目,它执行一些白列表和图像处理:https://github.com/mstrchrstphr/ocr-ios示例

#4


0  

为什么Tesseract OCR库(iOS)根本不能识别文本?

and my output is

我的输出是

为什么Tesseract OCR库(iOS)根本不能识别文本?

Solution :

解决方案:

 tesseract.language = @"eng+fra";

tesseract.pageSegmentationMode = G8PageSegmentationModeAuto;
tesseract.engineMode  = G8OCREngineModeTesseractCubeCombined;
tesseract.image = [image.image g8_blackAndWhite];

tesseract.maximumRecognitionTime = 60.0;
[tesseract recognize];

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

reco_area.text = [tesseract recognizedText];

for tessdata click here

对于tessdata点击这里

#5


0  

whatever @ Adam Richardson explained is correct along with that add this 1) scaleimage method for increase size of the image(dimensions increase)

任何@ Adam Richardson解释的都是正确的加上这个1)scaleimage方法来增加图像的尺寸(尺寸增加)

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

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.draw(in: CGRect(x: 0, y: 0, width: scaledSize.width, height: scaledSize.height))
    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return scaledImage!
}

2) store this eng.traineddata language file in filemanager

2)存储这个eng。在filemanager中的traineddata语言文件

 func storeLanguageFile() throws{
    var fileManager: FileManager = FileManager.default
    let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
    let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
    let docDirectory = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)[0] as NSString
    let path: String = docDirectory.appendingPathComponent("/tessdata/eng.traineddata")
    if fileManager.fileExists(atPath: path){
        var data: NSData = NSData.dataWithContentsOfMappedFile((Bundle.main.resourcePath?.appending("/tessdata/eng.traineddata"))!)! as! NSData
        var error: NSError
        try FileManager.default.createDirectory(atPath: docDirectory.appendingPathComponent("/tessdata"), withIntermediateDirectories: true, attributes: nil)
        data.write(toFile: path, atomically: true)
    }
}

3) after that you can use https://github.com/BradLarson/GPUImage for increase clarity of the image

3)之后,您可以使用https://github.com/BradLarson/GPUImage来增加图像的清晰度

you can use this

你可以使用这个

func preprocessedImage(for tesseract: G8Tesseract!, sourceImage: UIImage!) -> UIImage! {
    var stillImageFilter: GPUImageAdaptiveThresholdFilter = GPUImageAdaptiveThresholdFilter()
    stillImageFilter.blurRadiusInPixels = 4.0
    var filterImage: UIImage = stillImageFilter.image(byFilteringImage: sourceImage)
    return filterImage
}

these 3 steps will help you to increase the accuracy of the tesseract upto 60 ~ 70 %

这三个步骤将帮助您将tesseract的准确度提高到60% ~ 70%