Monday, November 14, 2016

Raspberry Pi - OpenCV - Setup

Description:
This post is to cover OpenCV installation in raspberry pi and list down the steps in concise fashion and get things up and running so that anyone can focus on the project has in mind. Majority of the contents can from the links and I found those are workable and tested at my end. Check the Notes sections for any problem faced or explanation on any particular step.

Pre-requisites and version of components:
- Raspberry Pi 3 
- Operating System raspbian Jessie
- OpenCV 3.1.0
- Python 3 (3.4.2)


Instruction:
StepsDescription
Install dependencies
sudo apt-get install build-essential cmake pkg-configCMake to configure OpenCV build process
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-devimage I/O packages to load image files
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-devvideo I/O packages to load video from files and streams
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk2.0-devSub module highgui to display image on screen
sudo apt-get install libatlas-base-dev gfortranlibrary to optimize OpenCV
sudo apt-get install python3-devPython 3 header file to compile OpenCV
Download OpenCV 
> cd ~
> wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
> unzip opencv.zip
OpenCV source code
> wget -O opencv_contrib.zip  https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip

unzip opencv_contrib.zip
OpenCV contribute source code
Setup Python3
> wget https://bootstrap.pypa.io/get-pip.py
> sudo python get-pip.py
install python package manager (pip)
sudo pip install virtualenv virtualenvwrapperinstall virtualenv and virtualwrapper to keep the python dependencies separate
sudo rm -rf ~/.cache/pipclear pip cache 
WORKON_HOME=$HOME/.virtualenvs 
source /usr/local/bin/virtualenvwrapper.sh
write these two lines in ~/.profile 
source ~/.profilereload profile changes
mkvirtualenv cv -p python3make python virtual environment
> source ~/.profile
> workon cv
all works will be done in cv python virtual environment
pip install numpyPython package for numeric processing
Install OpenCV
> cd ~/opencv-3.1.0/
> mkdir build 
> cd build 
> cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib
3.1.0/modules \ -D BUILD_EXAMPLES=ON ..
Cell 3:2
make -j4compile OpenCV on four cores
> sudo make install
> sudo ldconfig
install OpenCV
> cd /usr/local/lib/python3.4/site-packages/
> sudo mv cv2.cpython-34m.so cv2.so
rename cv file to cv2
> cd ~/.virtualenvs/cv/lib/python3.4/site-packages/
> ln -s /usr/local/lib/python3.4/site-packages/cv2.so cv2.so
create soft link to cv2.so
Test OpenCV installation 
> source ~/.profile
> workon cv
> python
>>> import cv2
>>> cv2.__version__
test OpenCV version (3.1.0)
Clean Up 
> rm -rf opencv-3.1.0 opencv_contrib-3.1.0test OpenCV version (3.1.0)


Notes:
1.  If NOOBS is used, the SD card is already expanded. In case SD card needs to be expanded, follow below steps - 
- Type command 'sudo raspi-config' in terminal
- Select option '1 - Expand Filesystem'
- Restart the system using 'sudo reboot' in terminal

2. OpenCV and OpenCV contribute version has to be same


Resources:
1. http://www.linuxcircle.com/2015/05/18/open-computer-vision-opencv3-with-python-3-on-raspberry-pi-2/
2. http://www.pyimagesearch.com/2016/04/18/install-guide-raspberry-pi-3-raspbian-jessie-opencv-3/
3. 


No comments: