How To: Compile and Use Tesseract (3.01) on iOS (SDK 5)

http://tinsuke.wordpress.com/2011/11/01/how-to-compile-and-use-tesseract-3-01-on-ios-sdk-5/

Update

  • I don’t have access to a Mac computer now (actually it has been 3 months) and I couldn’t update the guide to Xcode 4.5 (iOS 6), but the fine gentlemanbengl3rt has done so and the updated script is available at: http://goo.gl/wQea5 (I haven’t been able to test it, so some feedback on whether it works or not would be appreciated!)

I never thought that my last post would have so much audience. Among other things, it earned me 3 direct job interview offers (1 of  ‘em from Google itself, maintainer of tesseract), an invite to write articles to a TI digital e-magazine and a few digital friends, but that’s something to discuss at other posts. Thank you!

Getting back to what really matters: last post was focused on cross compiling (potentially) any library for iOS (armv6/armv7/i386) and to use as an example I chose Tesseract, which was the library I was using on a work project. But the repercussion was so great and both Tesseract and iOS got newer versions that I’ve decided to write this post specifically about getting Tesseract compiled and using it on your iOS project.

As stated earlier, Tesseract has been officially launched at version 3.01 (that now uses an autogen.sh setup script and an improved configure script ) and iOS has received a major upgrade, version 5.0. As you may guess, these changes broke my script!


So let’s restart this party! (or: Compiling Tesseract 3.01 for iOS SDK 5.0)

The basics about the script were explained at last post and I’ll be just covering the changes and how to use it.

As noted by Rafael, the default C/C++/Objective-C compilers for iOS 5 (bundled with Xcode 4.2) have changed, actually, now you just need Clang, so the CPP, CXX, CXXPP, and CC definitions (inside setenv_all()) have changed to:

1
2
export CXX= "$DEVROOT/usr/bin/llvm-g++"
export CC= "$DEVROOT/usr/bin/llvm-gcc"

Additionally, as Tesseract now has on autogen.sh script to run before configuring, we run it before each configure call:

1
bash autogen.sh

And because Tesseract’s configure script now accepts a path to Leptonica to be specified, no hacks with it are needed, just calling it with another parameter is just enough:

1
. /configure -- enable -shared=no LIBLEPT_HEADERSDIR=$GLOBAL_OUTDIR /include/

To build your desired library, create a directory, I’ll refer to it as “./build/”. Inside it, create the following structure:

  • ./build/

Open Terminal, enter our “./build/” directory, cross your fingers (one very important step pointed out byVenusbai) and run:

1
bash build_dependencies.sh

Well, if you’re lucky enough and deserve the holy right to use Tesseract on mobile Apps, check the dependencies folder content and there you’ll have all the needed header and library files to play with OCR on your iPhone (I don’t have one, personally prefer Android, but you got it….).

Great!!! Now what?! (or: Using Tesseract on your iOS project)

  1. Create one new iOS project at Xcode (or just open your existing one)
  2. Add the generated ./build/dependencies/ folder to your project. It contains the needed .h Header and lib*.a Library files
  3. Add the tessdata folder, containing, well, erhm, hum, the tessdata files you need at your project. If you don’t know what the “tessdata” folder is: it contains preprocessed data for a certain language so Tesseract can recognize that language, download language data from: http://code.google.com/p/tesseract-ocr/downloads/list. Check the sub-instructions below to add it the right way (not the default Xcode way…)
    1. Right-click your project/group at Xcode
    2. Choose “Add files to your project”
    3. Select the “tessdata” folder
    4. At the same window, check the “Create folder references for any added folders”. This is the most important step, as it instructs Xcode to add your “tessdata” folder as a regular folder (a resource, as well), not as a Xcode project group.
  4. Create your TessBaseAPI object with the code below to start playing with it!
  5. Make sure that every source file that includes/imports or sees (includes/imports one file that may include/import) Tesseract Header files has the .mm extension instead of the regular .m. This allows the compiler to interpret Tesseract Headers as C/C++ headers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Set up the tessdata path. This is included in the application bundle
// but is copied to the Documents directory on the first run.
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = ([documentPaths count] > 0) ? [documentPaths objectAtIndex:0] : nil;
 
NSString *dataPath = [documentPath stringByAppendingPathComponent:@ "tessdata" ];
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn't exist, copy the default store.
if (![fileManager fileExistsAtPath:dataPath]) {
     // get the path to the app bundle (with the tessdata dir)
     NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
     NSString *tessdataPath = [bundlePath stringByAppendingPathComponent:@ "tessdata" ];
     if (tessdataPath) {
         [fileManager copyItemAtPath:tessdataPath toPath:dataPath error:NULL];
     }
}
 
setenv( "TESSDATA_PREFIX" , [[documentPath stringByAppendingString:@ "/" ] UTF8String], 1);
 
// init the tesseract engine.
tesseract = new tesseract::TessBaseAPI();
tesseract->Init([dataPath cStringUsingEncoding:NSUTF8StringEncoding], "eng" );

Well, that’s it! Hope you can reproduce this and I also provide to download one Xcode 4.2 iOS SDK 5 project with Tesseract configured and already recognizing one sample image, check it out if having any troubles following this howto.

Files for Download

Final Considerations

I really hope you guys have enjoyed it and if you have any opinion, compliment, suggestion or just wanna state something, feel free to comment, I’ll try to approve it ASAP.

acknowledgements

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MXNet错误:编译时使用use_cuda = 1以启用GPU使用。 这个错误意味着你需要在编译MXNet时使用use_cuda = 1选项来启用GPU使用。这通常需要安装CUDA和cuDNN,并确保它们与MXNet版本兼容。如果您已经安装了CUDA和cuDNN,但仍然遇到此错误,请确保您的环境变量和路径设置正确。 ### 回答2: MXNetError: 编译时使用use_cuda = 1以启用GPU使用 MXNet是一个流行的深度学习框架,它提供了使用CPU和GPU来执行深度学习任务的能力。在许多情况下,使用GPU可以大大提高训练和推理的速度,因为GPU上的并行计算非常适合执行深度学习操作。当您在使用MXNet时遇到MXNetError: compile with use_cuda = 1 to enable GPU usage错误时,这意味着您的代码正在尝试使用GPU,但MXNet没有编译以支持GPU操作。 要解决此错误,您需要使用GPU编译MXNet。您可以按照以下步骤来完成: 1.安装CUDA:CUDA是NVIDIA提供的并行计算平台和编程模型,可用于加速深度学习任务。要使用MXNet的GPU功能,您需要在计算机上安装CUDA。您可以从NVIDIA网站上下载与您的计算机配置相对应的CUDA版本,并按照安装指南进行安装。 2.安装CuDNN:CuDNN是针对深度学习应用程序的CUDA库,它提供了高效的卷积神经网络和循环神经网络的实现。MXNet需要CuDNN来优化GPU上的计算。您可以从NVIDIA网站上下载与您的CUDA版本相匹配的CuDNN版本,并按照安装指南进行安装。 3.使用use_cuda = 1重新编译MXNet:一旦您安装了CUDA和CuDNN,您需要重新编译MXNet以使用GPU。您可以将use_cuda标志设置为1来启用GPU支持,并将其他MXNet选项设置为适当的值。在重新编译之前,您需要设置MXNet的CUDA路径和CuDNN路径以便正确地连接CUDA和CuDNN库。重新编译MXNet可能需要一些时间,具体取决于您的计算机配置。 完成上述步骤后,您应该能够使用MXNet的GPU功能,而不会再遇到MXNetError: compile with use_cuda = 1 to enable GPU usage错误。 总之,MXNetError: compile with use_cuda = 1 to enable GPU usage错误表明MXNet代码正在尝试使用GPU,但MXNet没有编译以支持GPU操作。要解决此错误,您需要安装CUDA和CuDNN,并重新编译MXNet以启用GPU支持。 ### 回答3: mxneterror: compile with use_cuda=1 to enable gpu usage是MXNet深度学习框架在训练深度神经网络时会遇到的一个错误提示。这个错误提示的意思是说你需要在编译MXNet源代码时使用use_cuda=1的选项,才能启用GPU加速功能。 MXNet是一个支持跨平台的深度学习框架,它支持多种语言和运行环境,包括Python、R、C++等。在使用MXNet进行深度学习训练时,使用GPU可以大幅提升训练速度和效率。因此,如果你的MXNet程序需要使用GPU进行加速,那么你需要在编译MXNet源代码时加入use_cuda=1选项来启用GPU加速功能。 具体的操作步骤如下: 1. 下载MXNet源代码并解压缩到本地目录。 2. 安装CUDA和cuDNN库,这两个库是MXNet使用GPU加速的必备条件。 3. 打开命令行窗口,进入MXNet源代码目录,执行以下命令: make USE_CUDA=1 USE_CUDA_PATH=/usr/local/cuda USE_CUDNN=1 这个命令指定了MXNet编译时使用CUDA和cuDNN库,并启用了GPU加速功能。 4. 等待编译完成,获取MXNet的编译结果,使用这个编译结果替换原来的MXNet库文件。 如果编译成功,会在MXNet源代码目录下生成一个lib文件夹,这个文件夹下包含了编译好的MXNet库文件。 以上就是解决mxneterror: compile with use_cuda=1 to enable gpu usage的方法,希望能帮助你顺利完成MXNet的GPU加速开发和训练。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值