Installing OpenCV with Tesseract text module on Ubuntu

This post shows how to install OpenCV on Ubuntu (14.04) with all text and Tesseract goodies.

First install all dependencies.

sudo apt-get install -y build-essential cmake git pkg-config
sudo apt-get install -y libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install -y libgtk2.0-dev
sudo apt-get install -y libatlas-base-dev gfortran

Install Tesseract dependencies to be able to build it with OpenCV.

apt-get install -y tesseract-ocr libtesseract-dev libleptonica-dev

Install some extra Python stuff. I presume you already have Python 3 (or 2.7).

# Install Pip if you don't have it yet.
curl -O https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
sudo apt-get install -y python3.4-dev
pip install numpy

Get the right OpenCV source code.

cd ~
git clone https://github.com/Itseez/opencv.git
cd opencv
git checkout 3.1.0 # Or just use master

cd ~
git clone https://github.com/Itseez/opencv_contrib.git
cd opencv_contrib
git checkout 3.1.0 # Or just use master

Compile OpenCV from source.

cd ~/opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
      -D BUILD_opencv_python3=ON \
      -D BUILD_EXAMPLES=ON ..
make -j4
make install
ldconfig

Next you’re ready to go. Test your install by opening Python and check if you can access the new version and features:

>>> import cv2
>>> cv2.__version__
'3.1.0'
>>> cv2.text # should return the module

You could also check this Dockerfile to run everything in a container.