Showing posts with label opencv. Show all posts
Showing posts with label opencv. Show all posts

Monday 23 September 2019

Install OpenCV 4.1.1 using Python3.5 in Ubuntu 16.04

Hi all
I am sharing tried and tested steps for installation of latest version of OpenCV using Python3.5 in Ubuntu 16.04. Python3.5 is installed by default in Ubuntu 16.04.

sudo apt update
sudo apt upgrade
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python2.7-dev python3.5-dev

wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.1.zip
unzip opencv.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.1.zip
unzip opencv_contrib.zip
sudo apt-get install python-pip && pip install --upgrade pip
sudo python -m pip uninstall pip && sudo apt install python-pip --reinstall 

sudo pip install virtualenv virtualenvwrapper
sudo rm -rf ~/.cache/pip

nano .bashrc
Add following lines:
# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh


source ~/.bashrc
mkvirtualenv py3cv4 -p python3


x


pip install numpy
cd ~/opencv-4.1.1/
mkdir build
cd build

cmake -D BUILD_TIFF=ON -D WITH_CUDA=OFF -D ENABLE_AVX=OFF -D WITH_OPENGL=OFF -D WITH_OPENCL=OFF -D WITH_IPP=OFF -D WITH_TBB=ON -D BUILD_TBB=ON -D WITH_EIGEN=OFF -D WITH_V4L=OFF -D WITH_VTK=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-4.1.1/modules -D PYTHON_EXECUTABLE=~/.virtualenvs/py3cv4/bin/python -D BUILD_EXAMPLES=ON ..

make -j4 (for core i3 system)
sudo make install
sudo ldconfig
cd /usr/local/lib/python3.5/site-packages/
cd cv2
cd python-3.5/
sudo mv cv2.cpython-35m-x86_64-linux-gnu.so cv2.so
(py3cv4) abhi@ubuntu:~$ cd ~/.virtualenvs/py3cv4/lib/python3.5/site-packages/
(py3cv4) abhi@ubuntu:~$ ln -s /usr/local/lib/python3.5/site-packages/cv2/python-3.5/cv2.so cv2.so

To move out from Virtual environment, type "deactivate". To switch inside virtual environment, type "workon py3cv4".

To test proper installation of OpenCV in Ubuntu, open terminal, activate python interpreter and type commands:
$python
>>import cv2
>>cv2.__version__
'4.1.1'

Hope it helps. Keep learning!