Compiling tesseract v3 for iPhone

Update 2: Before using the script, ensure that you can build tesseract for your host system normally. Also, I only tested the script with the v3 release of tesseract, not svn HEAD. If you get build errors, please try with rev 498.

Update: The script has been updated, thanks mostly to the prompting of fopen2003 in the comments below. I’ve successfully tested the resulting libs in both Simulator and an iPhone 4 (both at iOS4.x) using the PocketOCR project.

After many requests, I finally got around to looking into updating the build script to cross-compile tesseract ocr v3 for use with iPhone. Here’s the script. It seems to build the static, fat library without error. I haven’t tried to update my app to use it yet, so I really don’t know if it even works. Let me know in the comments if it actually does indeed work.

  1. Check out the svn source of tesseract: http://code.google.com/p/tesseract-ocr/source/checkout
  2. Copy this script into the source directory and run from there
  3. Profit???

The libraries will be in a directory named “outdir”

#!/bin/sh
# build_fat.sh
#
# Created by Robert Carlsen on 15.07.2009. Updated 24.9.2010
# build an arm / i386 lib of standard linux project
#
# initially configured for tesseract-ocr v2.0.4
# updated for tesseract prerelease v3

outdir=outdir
mkdir -p $outdir/arm $outdir/i386

libdirs=( api ccutil ccmain ccstruct classify cutil dict image textord training viewer wordrec )
libs=( api ccutil main ccstruct classify cutil dict image textord training viewer wordrec )
count=${#libdirs[@]}


make distclean
unset CPPFLAGS CFLAGS LDFLAGS CPP CXX CC CXXFLAGS DEVROOT SDKROOT LD

export DEVROOT=/Developer/Platforms/iPhoneOS.platform/Developer
export SDKROOT=$DEVROOT/SDKs/iPhoneOS4.1.sdk
export CFLAGS="-arch armv6 -pipe -no-cpp-precomp -isysroot$SDKROOT -miphoneos-version-min=3.0 -I$SDKROOT/usr/include/"
export CPPFLAGS="$CFLAGS"
export CXXFLAGS="$CFLAGS"
export LDFLAGS="-L$SDKROOT/usr/lib/"
export LD="$DEVROOT/usr/bin/ld"
export CPP="$DEVROOT/usr/bin/cpp-4.2"
export CXX="$DEVROOT/usr/bin/g++-4.2"
export CC="$DEVROOT/usr/bin/gcc-4.2"
./configure --host=arm-apple-darwin 
make -j3

index=0
while [ "$index" -lt "$count" ]
do
    cp ${libdirs[index]}/.libs/libtesseract_${libs[index]}.a $outdir/arm/libtesseract_${libs[index]}_armv6.a
    ((index++))
done


make distclean
unset CPPFLAGS CFLAGS LDFLAGS CPP CXX CC CXXFLAGS DEVROOT SDKROOT LD

export DEVROOT=/Developer/Platforms/iPhoneSimulator.platform/Developer
export SDKROOT=$DEVROOT/SDKs/iPhoneSimulator4.1.sdk
export CFLAGS="-arch i386 -pipe -no-cpp-precomp -isysroot$SDKROOT -miphoneos-version-min=3.0 -I$SDKROOT/usr/include/"
export CPPFLAGS="$CFLAGS"
export CXXFLAGS="$CFLAGS"
export LDFLAGS="-L$SDKROOT/usr/lib/"
export LD="$DEVROOT/usr/bin/ld"
export CPP="$DEVROOT/usr/bin/cpp-4.2"
export CXX="$DEVROOT/usr/bin/g++-4.2"
export CC="$DEVROOT/usr/bin/gcc-4.2"
./configure
make -j3

index=0
while [ "$index" -lt "$count" ]
do
    cp ${libdirs[index]}/.libs/libtesseract_${libs[index]}.a $outdir/i386/libtesseract_${libs[index]}_i386.a
    ((index++))
done


# are the fat libs making the bundle too big?
index=0
while [ "$index" -lt "$count" ]
do
    /usr/bin/lipo -arch armv6 $outdir/arm/libtesseract_${libs[index]}_armv6.a -arch i386 $outdir/i386/libtesseract_${libs[index]}_i386.a -create -output $outdir/libtesseract_${libs[index]}.a
    ((index++))
done

unset CPPFLAGS CFLAGS LDFLAGS CPP CXX CC CXXFLAGS DEVROOT SDKROOT

Posted

in

by

Comments

101 responses to “Compiling tesseract v3 for iPhone”

  1. Duncan Avatar

    Here is a solution for iOS5 and SDK 4.x. For OCR tesseract 3.01 and leptonica 1.68.

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

Leave a Reply