Skip to content

Instantly share code, notes, and snippets.

@barbolo
Created January 27, 2017 17:31
Show Gist options
  • Save barbolo/e59aa45ec8e425a26ec4da1086acfbc7 to your computer and use it in GitHub Desktop.
Save barbolo/e59aa45ec8e425a26ec4da1086acfbc7 to your computer and use it in GitHub Desktop.
Tesseract OCR on AWS Lambda with Python

References:

https://github.com/tesseract-ocr/tesseract/wiki/Compiling

http://stackoverflow.com/questions/33588262/tesseract-ocr-on-aws-lambda-via-virtualenv

https://github.com/sirfz/tesserocr

Instructions for running Tesseract OCR on AWS Lambda with Python

  1. Launch an Amazon Linux AMI instance

  2. Connect to the instance and generate an AWS Lambda Package

# system libs
sudo yum -y update
sudo yum -y upgrade
sudo yum -y groupinstall "Development Tools"

# tesseract / leptonica / pillow dependencies
sudo yum -y install gcc gcc-c++ make autoconf aclocal automake libtool \
                    libjpeg-devel libpng-devel libtiff-devel zlib-devel \
                    libzip-devel freetype-devel lcms2-devel libwebp-devel \
                    tcl-devel tk-devel

# install leptonica
cd ~
mkdir leptonica
cd leptonica
wget http://www.leptonica.org/source/leptonica-1.74.1.tar.gz
tar -zxvf leptonica-*.tar.gz
cd leptonica-*
./configure
make
sudo make install

# install tesseract
cd ~
git clone --branch 4.00.00alpha https://github.com/tesseract-ocr/tesseract.git
cd tesseract
./autogen.sh
./configure --enable-debug
LDFLAGS="-L/usr/local/lib" CFLAGS="-I/usr/local/include" make
sudo make install

# create a python virtual env
virtualenv ~/tfenv
source ~/tfenv/bin/activate

# Install pillow
pip install pillow

# Install cython
pip install cython

# Install tesserocr
pip install tesserocr

# prepare the zip package
cd ~
mkdir lambda-tesseract
cd lambda-tesseract
cp /usr/local/bin/tesseract .
mkdir lib
cp /usr/local/lib/libtesseract.so.4 lib/
cp /usr/local/lib/liblept.so.5 lib/
cp /lib64/librt.so.1 lib/
cp /lib64/libz.so.1 lib/
cp /usr/lib64/libpng12.so.0 lib/
cp /usr/lib64/libjpeg.so.62 lib/
cp /usr/lib64/libtiff.so.5 lib/
cp /lib64/libpthread.so.0 lib/
cp /usr/lib64/libstdc++.so.6 lib/
cp /lib64/libm.so.6 lib/
cp /lib64/libgcc_s.so.1 lib/
cp /lib64/libc.so.6 lib/
cp /lib64/ld-linux-x86-64.so.2 lib/
cp /usr/lib64/libjbig.so.2.0 lib/
cp -r ~/tesseract/tessdata/ tessdata
cp -r ~/tfenv/lib/python2.7/site-packages/* .
cp -r ~/tfenv/lib64/python2.7/site-packages/* .

mkdir tessdata
wget https://github.com/tesseract-ocr/tessdata/raw/master/eng.traineddata -O tessdata/eng.traineddata

# Create lambda_function.py file (see example below)
# lambda_function.py
import tesserocr
import PIL.Image
import io
from base64 import b64decode

def lambda_handler(event, context):
  binary = b64decode(event['image64'])
  image = PIL.Image.open(io.BytesIO(binary))
  text = tesserocr.image_to_text(image)
  return {'text' : text}
# zip the package
cd ~
zip -r lambda-tesseract.zip lambda-tesseract --exclude *.pyc
  1. You may then copy the zip package to your computer and upload it to S3
scp -i key.pem ec2-user@AWS_EC2_INSTANCE_IP:~/lambda-tesseract.zip .
  1. Use the zip url in S3 to configure AWS Lambda.

  2. Create an environment variable with key "TESSDATA_PREFIX" and leave the value empty.

  3. You can test the function with a test.json file like this:

{
"image64": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhUTExMVFhUWGB0XGRYY\nGB4YGRYYGR8dIxgeFxgYHSggGxslIiAZITEhJisrLi4xGB8zODMsNygtLisB\nCgoKBQUFDgUFDisZExkrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysr\nKysrKysrKysrKysrKysrKysrK//AABEIAFMCWAMBIgACEQEDEQH/xAAcAAEA\nAgIDAQAAAAAAAAAAAAAABwgFBgECBAP/xABPEAABAwIEAwUFAwgFBw0BAAAB\nAgMRAAQFEiExBgdBEyJRYXEIFDKBkSNCUhUzVGJykpOhFySisdE1U3Sz0+Hw\nGCVjZXOCg5SywcPS4xb/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/8QAFBEBAAAA\nAAAAA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Y0GlBgeV/BAwy2KVKSt5w5nVpGkjQ\nISTqUJ1ieqlHSYrdKUoFKUoFKUoOMormlKBSlKBSKUoFKUoOIpFKUCKRSlBz\nSlKBSlKBFKUoFKUoFKUoFKUoFKUoFKUoFKUoFKUoFKUoFKUoFKUoFKUoFKUo\nFKUoFKUoFKUoFKUoFKUoFKUoFKUoFKUoFKUoFKUoFKUoFKUoFKUoFKUoFKUo\nFKUoP//Z\n"
}

It should print 1234567890.

@Suryaphaneeth
Copy link

@rheft, even I am having same issues. Please post an update if you could find a solution. Will update the post if I have any progress.

@mpetryszyn
Copy link

Struggling to get tesseract 4.0.0 to run on python 3.6 in Lambda. Anyone have any luck?

@luizgustavogp
Copy link

luizgustavogp commented Nov 20, 2018

Hi,

I configured the code entry with this s3 link URL https://s3.amazonaws.com/ca-lambda-tesseract/lambda-tesseract.zip, provided by @Ango but I received this error:

{ "stackTrace": [ [ "/var/task/lambda_function.py", 15, "lambda_handler", "text = pytesseract.image_to_string(image)" ], [ "/var/task/pytesseract/pytesseract.py", 125, "image_to_string", "raise TesseractError(status, errors)" ] ], "errorType": "TesseractError", "errorMessage": "(1, u'Error opening data file /usr/local/share/tessdata/eng.traineddata')" }

Any idea?

@johnykifle
Copy link

johnykifle commented Nov 28, 2018

Hello guys,

Has any one experienced this error message.

{
"errorMessage": "Unable to import module 'lambda_function'"
}

I have had this issue previously when i zip the 'lambda-tesseract' folder. And resolved it by zipping the content instead of the folder.
But now it doesn't work at all.

@gwittchen
Copy link

gwittchen commented Nov 28, 2018

Did you download the correct traindata since each version has it's own dataset.
Older version < 4 from tesseract require additional data in the tessdata folder such as "".cube.ln files
Check that TESSDATA_PREFIX is correctly set.

@johnykifle
Copy link

I am actually using leptonica-1.73.tar.gz and tesseract-3.04.01.

@gwittchen
Copy link

gwittchen commented Nov 29, 2018

I created a newer version using a docker container to build the binaries for the AWS lambda environment.
It's using leptonica-1.76 and tesseract-4.0.0 and the same simple lambda from this tutorial.

https://github.com/gwittchen/lambda-ocr

@pog8tor
Copy link

pog8tor commented Jan 4, 2019

I created a newer version using a docker container to build the binaries for the AWS lambda environment.
It's using leptonica-1.76 and tesseract-4.0.0 and the same simple lambda from this tutorial.

https://github.com/gwittchen/lambda-ocr

This is pure gold... i went back, just to say thanks
Python3.6 + tesseract 4 over AWS lambda

@seth10
Copy link

seth10 commented May 20, 2020

Ready to use https://s3.amazonaws.com/ca-lambda-tesseract/lambda-tesseract.zip

Thank you very much @Ango for preparing this! All I had to do to get it running was add os.environ["TESSDATA_PREFIX"] = os.path.join(LAMBDA_TASK_ROOT, 'tessdata'). Also I had to make sure I selected Python 2.7 as the runtime instead of Python 3.8.

@juang97
Copy link

juang97 commented Jun 4, 2020

@seth10 where exactly did you add this line? Was it in the .py file ? Thanks

@seth10
Copy link

seth10 commented Jun 4, 2020

@juang97 Exactly, in lambda_function.py I added it on line 10, right below the line that appends to os.environ["PATH"].

To be clear, this is what I did and worked for me in Python 2.7. The link gwittchen shared seems promising and updated. Being able to use Python 3.6 would be great, but I only noticed this after I started using Ango's zip. I haven't tried it myself.

@Gautami007
Copy link

Hello @seth10, @juang97, @Ango @johnykifle

I have followed all the steps mentioned above and created "lambda-tesseract.zip" file and uploaded to lambda through S3. But, I am getting below error white Test function;

{
"errorMessage": "Unable to import module 'lambda_function'"
}

I am using all below configuration;
tesseract 3.05.00
leptonica-1.74.4
libjpeg 6b (libjpeg-turbo 1.2.90) : libpng 1.5.13 : libtiff 4.0.3 : zlib 1.2.7 : libwebp 0.3.0
python 3.7

Also, in lambda "Environment variables" value field is "must" parameter, we can not keep it empty.

Could anyone of you help me out with my error?

@seth10
Copy link

seth10 commented Aug 6, 2020

Hi @Gautami007, to me this sounds like a general AWS Lambda issue, nothing related to tesseract. Please make sure that you haven't changed the name of the primary function you intend Lambda to call. If it's no longer called lambda_function, either change the function name or tell Lambda which function you want it to call using the Handler field, as seen here. Documentation here.

@Gautami007
Copy link

Hello @seth10,
Thank you for response.

I neither changed the lambda function name nor handler name. Also I did upload the zip that you have mentioned in comment "Ready to use" but that also not working.

Getting below error;

START RequestId: 37a526a1-af54-4e2c-bacf-82df1ab01ac8 Version: $LATEST
(1, u'Error opening data file /usr/local/share/tessdata/eng.traineddata'): TesseractError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 15, in lambda_handler
text = pytesseract.image_to_string(image)
File "/var/task/pytesseract/pytesseract.py", line 125, in image_to_string
raise TesseractError(status, errors)
TesseractError: (1, u'Error opening data file /usr/local/share/tessdata/eng.traineddata')

END RequestId: 37a526a1-af54-4e2c-bacf-82df1ab01ac8
REPORT RequestId: 37a526a1-af54-4e2c-bacf-82df1ab01ac8 Duration: 123.13 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 52 MB

@seth10
Copy link

seth10 commented Aug 6, 2020

Oh I see, so leaving the handler as lambda_function.lambda_handler should be fine then as that zip contines that file and that function. Given that you're seeing that error ("Error opening data file /usr/local/share/tessdata/eng.traineddata") now, I assume that means you're Lambda is at least starting up.

I feel like I remember seeing this error and that's why I added the os.environ["TESSDATA_PREFIX"] = os.path.join(LAMBDA_TASK_ROOT, 'tessdata') line, so it would know where to look for that file. Could you try modifying the lambda_function.py file and adding that on line 10?

@adriaanbd
Copy link

Hey I was very tempted to try this one out but opted for this EC2 + Docker that generates zip files using shell scripts to compile the libraries and dependencies, over here. It's very straightforward and reproducible.

@kollols
Copy link

kollols commented Aug 19, 2020

I am getting the below error while using seths solution :

START RequestId: 81a1f718-71a3-4597-8cf4-ea74a3304f02 Version: $LATEST
[Errno 13] Permission denied: OSError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 16, in lambda_handler
text = pytesseract.image_to_string(image)
File "/var/task/pytesseract/pytesseract.py", line 122, in image_to_string
config=config)
File "/var/task/pytesseract/pytesseract.py", line 46, in run_tesseract
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
File "/usr/lib64/python2.7/subprocess.py", line 394, in init
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1047, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment