How to install OpenCV with python on CentOS7

To install OpenCV with python on CentOS7
OpenCV-Python is a Python wrapper for the original OpenCV C++ implementation. OpenCV is an open source cross-platform computer vision algorithm library. It is used in all kind of visual processing areas, such as facial recognition, gesture recognition, human-computer interaction, Object identification, motion tracking and many more. In this tutorial, we will see the installation of OpenCV with python on Centos 7.

Installation

Before starting your installation you have to need python. Here I have to use centos7 default version.

[root@linuxhelp ~]# python --version
Python 2.7.5

Install OpenCV from yum repo. Before installation of OpenCV-python from yum repository we have to install require library numpy.

[root@linuxhelp ~]# yum install python-devel python-nose python-setuptools gcc gcc-gfortran gcc-c++ blas-devel lapack-devel atlas-devel -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.xeonbd.com
 * extras: mirror.dhakacom.com
 * updates: centos-hn.viettelidc.com.vn
Package python-setuptools-0.9.8-7.el7.noarch already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package atlas-devel.x86_64 0:3.10.1-12.el7 will be installed
--> Processing Dependency: atlas = 3.10.1-12.el7 for package: atlas-devel-3.10.1-12.el7.x86_64
--> Processing Dependency: libtatlas.so.3()(64bit) for package: atlas-devel-3.10.1-12.el7.x86_64
--> Processing Dependency: libsatlas.so.3()(64bit) for package: atlas-devel-3.10.1-12.el7.x86_64
.
.
.

Updated:
  gcc.x86_64 0:4.8.5-28.el7_5.1                    gcc-c++.x86_64 0:4.8.5-28.el7_5.1          
  gcc-gfortran.x86_64 0:4.8.5-28.el7_5.1          

Dependency Updated:
  cpp.x86_64 0:4.8.5-28.el7_5.1               libgcc.x86_64 0:4.8.5-28.el7_5.1                 
  libgfortran.x86_64 0:4.8.5-28.el7_5.1       libgomp.x86_64 0:4.8.5-28.el7_5.1                
  libquadmath.x86_64 0:4.8.5-28.el7_5.1       libquadmath-devel.x86_64 0:4.8.5-28.el7_5.1      
  libstdc++.x86_64 0:4.8.5-28.el7_5.1         libstdc++-devel.x86_64 0:4.8.5-28.el7_5.1        
  python.x86_64 0:2.7.5-68.el7                python-libs.x86_64 0:2.7.5-68.el7                

Complete!

Here, install pip run the following command.

[root@linuxhelp ~]# easy_install pip
Searching for pip
Reading https://pypi.python.org/simple/pip/
Best match: pip 10.0.1
Downloading https://files.pythonhosted.org/packages/ae/e8/2340d46ecadb1692a1e455f13f75e596d4eab3d11a57446f08259dee8f02/pip-10.0.1.tar.gz#sha256=f2bd08e0cd1b06e10218feaf6fef299f473ba706582eb3bd9d52203fdbd7ee68
Processing pip-10.0.1.tar.gz
Writing /tmp/easy_install-qHsmBY/pip-10.0.1/setup.cfg
Running pip-10.0.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-qHsmBY/pip-10.0.1/egg-dist-tmp-mkZKb9
/usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires'
.
.
.
no previously-included directories found matching 'contrib'
no previously-included directories found matching 'tasks'
no previously-included directories found matching 'tests'
Adding pip 10.0.1 to easy-install.pth file
Installing pip script to /usr/bin
Installing pip2.7 script to /usr/bin
Installing pip2 script to /usr/bin

Installed /usr/lib/python2.7/site-packages/pip-10.0.1-py2.7.egg
Processing dependencies for pip
Finished processing dependencies for pip

Then, install numpy by using pip.

[root@linuxhelp ~]# pip install numpy==1.6.1
Collecting numpy==1.6.1
  Downloading https://files.pythonhosted.org/packages/44/3b/63aa0464c495de6bac1f9f5a6c4626264ea07ad85b51cf6ebfdbbaa9fab4/numpy-1.6.1-cp27-cp27mu-manylinux1_x86_64.whl (13.6MB)
    100% |████████████████████████████████| 13.6MB 384kB/s
Installing collected packages: numpy
Successfully installed numpy-1.6.1

Now, install the opencv package.

[root@linuxhelp ~]# yum install opencv -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.xeonbd.com
 * extras: mirror.dhakacom.com
 * updates: centos-hn.viettelidc.com.vn
Resolving Dependencies
--> Running transaction check
---> Package opencv.x86_64 0:2.4.5-3.el7 will be installed
--> Processing Dependency: opencv-core(x86-64) = 2.4.5-3.el7 for package: opencv-2.4.5-3.el7.x86_64
--> Processing Dependency: libopencv_video.so.2.4()(64bit) for package: opencv-2.4.5-3.el7.x86_64
--> Processing Dependency: libopencv_photo.so.2.4()(64bit) for package: opencv-2.4.5-3.el7.x86_64
.
.
.
Installing : opencv-core-2.4.5-3.el7.x86_64                                              3/4 
  Installing : opencv-2.4.5-3.el7.x86_64                                                   4/4 
  Verifying  : opencv-2.4.5-3.el7.x86_64                                                   1/4 
  Verifying  : ilmbase-1.0.3-7.el7.x86_64                                                  2/4 
  Verifying  : opencv-core-2.4.5-3.el7.x86_64                                              3/4 
  Verifying  : OpenEXR-libs-1.7.1-7.el7.x86_64                                             4/4 

Installed:
  opencv.x86_64 0:2.4.5-3.el7                                                                  

Dependency Installed:
  OpenEXR-libs.x86_64 0:1.7.1-7.el7                ilmbase.x86_64 0:1.0.3-7.el7               
  opencv-core.x86_64 0:2.4.5-3.el7                

Complete!

Installing Opencv from Source. So, first of all Install the cmake run the following command.

[root@linuxhelp ~]# yum install cmake -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.xeonbd.com
 * extras: mirror.dhakacom.com
 * updates: centos-hn.viettelidc.com.vn
Resolving Dependencies
--> Running transaction check
---> Package cmake.x86_64 0:2.8.12.2-2.el7 will be installed
--> Finished Dependency Resolution
.
.
.
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : cmake-2.8.12.2-2.el7.x86_64                                                 1/1 
  Verifying  : cmake-2.8.12.2-2.el7.x86_64                                                 1/1 

Installed:
  cmake.x86_64 0:2.8.12.2-2.el7                                                                

Complete!

Then, install python-devel and numpy for creating python extension.

[root@linuxhelp ~]# yum install python-devel numpy
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.xeonbd.com
 * extras: mirror.dhakacom.com
 * updates: centos-hn.viettelidc.com.vn
Package python-devel-2.7.5-68.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package numpy.x86_64 1:1.7.1-13.el7 will be installed
--> Finished Dependency Resolution
.
.
.
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:numpy-1.7.1-13.el7.x86_64                                                 1/1 
  Verifying  : 1:numpy-1.7.1-13.el7.x86_64                                                 1/1 

Installed:
  numpy.x86_64 1:1.7.1-13.el7                                                                  

Complete!

Install GTK To support GUI features, Camera support (libv4l), Media Support (gstreamer) etc.

[root@linuxhelp ~]# yum install gtk2-devel  libv4l-devel  gstreamer-plugins-base-devel -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.xeonbd.com
 * extras: mirror.dhakacom.com
 * updates: centos-hn.viettelidc.com.vn
Resolving Dependencies
--> Running transaction check
---> Package gstreamer-plugins-base-devel.x86_64 0:0.10.36-10.el7 will be installed
--> Processing Dependency: pkgconfig(gstreamer-base-0.10) for package: gstreamer-plugins-base-devel-0.10.36-10.el7.x86_64
--> Processing Dependency: pkgconfig(gstreamer-0.10) for package: gstreamer-plugins-base-devel-0.10.36-10.el7.x86_64
--> Processing Dependency: pkgconfig(glib-2.0) for package: gstreamer-plugins-base-devel-0.10.36-10.el7.x86_64
---> Package gtk2-devel.x86_64 0:2.24.31-1.el7 will be installed
.
.
.
  xz-devel.x86_64 0:5.2.2-1.el7                                                                
  zlib-devel.x86_64 0:1.2.7-17.el7                                                             

Dependency Updated:
  glib2.x86_64 0:2.54.2-2.el7                   graphite2.x86_64 0:1.3.10-1.el7_3              
  libdrm.x86_64 0:2.4.83-2.el7                  mesa-libEGL.x86_64 0:17.2.3-8.20171019.el7     
  mesa-libGL.x86_64 0:17.2.3-8.20171019.el7     mesa-libGLES.x86_64 0:17.2.3-8.20171019.el7    
  mesa-libgbm.x86_64 0:17.2.3-8.20171019.el7    mesa-libglapi.x86_64 0:17.2.3-8.20171019.el7   

Complete!

Install git repository run the following command.

[root@linuxhelp ~]# yum install git -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.xeonbd.com
 * extras: mirror.dhakacom.com
 * updates: centos-hn.viettelidc.com.vn
Resolving Dependencies
--> Running transaction check
---> Package git.x86_64 0:1.8.3.1-11.el7 will be updated
--> Processing Dependency: git = 1.8.3.1-11.el7 for package: perl-Git-1.8.3.1-11.el7.noarch
---> Package git.x86_64 0:1.8.3.1-14.el7_5 will be an update
--> Running transaction check
.
.
.
Running transaction
  Updating   : perl-Git-1.8.3.1-14.el7_5.noarch                                            1/4 
  Updating   : git-1.8.3.1-14.el7_5.x86_64                                                 2/4 
  Cleanup    : perl-Git-1.8.3.1-11.el7.noarch                                              3/4 
  Cleanup    : git-1.8.3.1-11.el7.x86_64                                                   4/4 
  Verifying  : git-1.8.3.1-14.el7_5.x86_64                                                 1/4 
  Verifying  : perl-Git-1.8.3.1-14.el7_5.noarch                                            2/4 
  Verifying  : git-1.8.3.1-11.el7.x86_64                                                   3/4 
  Verifying  : perl-Git-1.8.3.1-11.el7.noarch                                              4/4 

Updated:
  git.x86_64 0:1.8.3.1-14.el7_5                                                                

Dependency Updated:
  perl-Git.noarch 0:1.8.3.1-14.el7_5                                                           

Complete!

Now, here first of create one new directory named opencv-build.

[root@linuxhelp ~]# mkdir opencv-build
[root@linuxhelp ~]# cd opencv-build

Clone the opencv package by using git.

[root@linuxhelp opencv-build]# git clone https://github.com/Itseez/opencv.git
Cloning into 'opencv'...
remote: Counting objects: 231414, done.
remote: Compressing objects: 100% (31/31), done.
remote: Total 231414 (delta 13), reused 8 (delta 3), pack-reused 231380
Receiving objects: 100% (231414/231414), 445.20 MiB | 1.02 MiB/s, done.
Resolving deltas: 100% (160867/160867), done.
Checking out files: 100% (5927/5927), done.

Here,install the dependencies packages(need latest libraries of PNG, JPEG, JPEG2000, TIFF, WebP).

[root@linuxhelp opencv-build]# yum install libpng-devel libjpeg-turbo-devel jasper-devel openexr-devel libtiff-devel libwebp-devel -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.xeonbd.com
 * extras: mirror.dhakacom.com
 * updates: centos-hn.viettelidc.com.vn
Package 2:libpng-devel-1.5.13-7.el7_2.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package OpenEXR-devel.x86_64 0:1.7.1-7.el7 will be installed
--> Processing Dependency: pkgconfig(IlmBase) for package: OpenEXR-devel-1.7.1-7.el7.x86_64
.
.
.
Installed:
  OpenEXR-devel.x86_64 0:1.7.1-7.el7               jasper-devel.x86_64 0:1.900.1-31.el7        
  libjpeg-turbo-devel.x86_64 0:1.2.90-5.el7        libtiff-devel.x86_64 0:4.0.3-27.el7_3       
  libwebp-devel.x86_64 0:0.3.0-7.el7              

Dependency Installed:
  ilmbase-devel.x86_64 0:1.0.3-7.el7                mesa-libGLU.x86_64 0:9.0.0-4.el7           
  mesa-libGLU-devel.x86_64 0:9.0.0-4.el7           

Complete!

After downloading list out the directory.

[root@linuxhelp opencv-build]# ls
 opencv
[root@linuxhelp opencv-build]# cd opencv/
[root@linuxhelp opencv]# ls
3rdparty  cmake           CONTRIBUTING.md  doc      LICENSE  platforms  samples
apps      CMakeLists.txt  data             include  modules  README.md

Check out the tags by using git.

[root@linuxhelp opencv]# git checkout tags/2.4.8.2
Note: checking out 'tags/2.4.8.2'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at b4845d8... temporary disable performance test for alphaComp function(cherry picked from commit 1ce5165cb7ccabdd0280970e3f1b6bc180055a3d)

Here, create new directory named build.

[root@linuxhelp opencv]# mkdir build
[root@linuxhelp opencv]# cd build/

Now, build to compile opencv from source.

[root@linuxhelp build]# cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
-- The CXX compiler identification is GNU 4.8.5
-- The C compiler identification is GNU 4.8.5
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detected version of GNU GCC: 48 (408)
-- Performing Test HAVE_CXX_FSIGNED_CHAR
-- Performing Test HAVE_CXX_FSIGNED_CHAR - Success
-- Performing Test HAVE_C_FSIGNED_CHAR
-- Performing Test HAVE_C_FSIGNED_CHAR – Success
.
.
.
--   Tests and samples:
--     Tests:                       YES
--     Performance tests:           YES
--     C/C++ Examples:              NO
-- 
--   Install path:                  /usr/local
-- 
--   cvconfig.h is in:              /root/opencv-build/opencv/build
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /root/opencv-build/opencv/build

Now, check build directory.

[root@linuxhelp build]# ls
apps                   CPackConfig.cmake        junk                OpenCVConfig-version.cmake
bin                    CPackSourceConfig.cmake  lib                 OpenCVModules.cmake
CMakeCache.txt         cvconfig.h               Makefile            unix-install
CMakeFiles             data                     modules             version_string.tmp
cmake_install.cmake    doc                      opencv2
cmake_uninstall.cmake  include                  OpenCVConfig.cmake

Here, give make run this command.

[root@linuxhelp build]# make
[  0%] Generating opencv_core_pch_dephelp.cxx
Scanning dependencies of target opencv_core_pch_dephelp
[  0%] Building CXX object modules/core/CMakeFiles/opencv_core_pch_dephelp.dir/opencv_core_pch_dephelp.cxx.o
Linking CXX static library ../../lib/libopencv_core_pch_dephelp.a
[  0%] Built target opencv_core_pch_dephelp
Scanning dependencies of target pch_Generate_opencv_core
[  0%] Generating precomp.hpp
[  0%] Generating precomp.hpp.gch/opencv_core_RELEASE.gch
[  0%] Built target pch_Generate_opencv_core
Scanning dependencies of target opencv_core
[  0%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/convert.cpp.o
.
.
.
[ 99%] Built target opencv_performance
Scanning dependencies of target opencv_traincascade
[ 99%] Building CXX object apps/traincascade/CMakeFiles/opencv_traincascade.dir/traincascade.cpp.o
[100%] Building CXX object apps/traincascade/CMakeFiles/opencv_traincascade.dir/cascadeclassifier.cpp.o
[100%] Building CXX object apps/traincascade/CMakeFiles/opencv_traincascade.dir/boost.cpp.o
[100%] Building CXX object apps/traincascade/CMakeFiles/opencv_traincascade.dir/features.cpp.o
[100%] Building CXX object apps/traincascade/CMakeFiles/opencv_traincascade.dir/haarfeatures.cpp.o
[100%] Building CXX object apps/traincascade/CMakeFiles/opencv_traincascade.dir/lbpfeatures.cpp.o
[100%] Building CXX object apps/traincascade/CMakeFiles/opencv_traincascade.dir/HOGfeatures.cpp.o
[100%] Building CXX object apps/traincascade/CMakeFiles/opencv_traincascade.dir/imagestorage.cpp.o
Linking CXX executable ../../bin/opencv_traincascade
[100%] Built target opencv_traincascade

Then, give make install.

[root@linuxhelp build]# make install
[  0%] Built target opencv_core_pch_dephelp
[  0%] Built target pch_Generate_opencv_core
[  3%] Built target opencv_core
[  3%] Built target opencv_ts_pch_dephelp
[  3%] Built target pch_Generate_opencv_ts
[  3%] Built target opencv_flann_pch_dephelp
[  3%] Built target pch_Generate_opencv_flann
[  4%] Built target opencv_flann
[  5%] Built target opencv_imgproc_pch_dephelp
[  6%] Built target pch_Generate_opencv_imgproc
[ 10%] Built target opencv_imgproc
.
.
.
usr/local/bin/opencv_createsamples
-- Set runtime path of "/usr/local/bin/opencv_createsamples" to "/usr/local/lib"
-- Installing: /usr/local/bin/opencv_performance
-- Set runtime path of "/usr/local/bin/opencv_performance" to "/usr/local/lib"
-- Installing: /usr/local/bin/opencv_traincascade
-- Set runtime path of "/usr/local/bin/opencv_traincascade" to "/usr/local/lib"

Above installation will install package in /usr/local/lib. so, check that location.

[root@linuxhelp build]# cd /usr/local/lib/python2.7/site-packages/
[root@linuxhelp site-packages]# ls
cv2.so  cv.py

now, we need to copy site-packages from /usr/local/lib to /usr/lib.Move opencv module from in defult python path.

[root@linuxhelp build]# cp /usr/local/lib/python2.7/site-packages/cv2.so /usr/lib/python2.7/site-packages

Now, verify the installation.

[root@linuxhelp site-packages]# python
Python 2.7.5 (default, Apr 11 2018, 07:36:10) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> 
>>> 
>>> print cv2.__version__
2.4.8.2
>>> 
>>> 
>>> exit ()

Check sample test.

[root@linuxhelp site-packages]# cd /root/opencv-build/opencv/samples/python2
[root@linuxhelp python2]# python turing.py 

USAGE: turing.py [-o <output.avi>]
[…]

With this, the method to install OpenCV with python on centos 7 comes to an end.

Tag : Python
Comment
Gokul
Sep 11 2018
The required Files and packages were Python Library numpy Pip numpy==1.6.1 For Installation procedure Please Follow the Tutorial
sandeepkumar6360
Sep 07 2018
sir, before starting installation which file required please list out.
Add a comment
FAQ
Q
How to install OpenCV?
A
Using the following command:
# yum install opencv
Q
How to install pip?
A
Execute the following command:
# easy_install pip
Q
How to install OpenCV with python on CentOS7?
A
Run the following commmand:
# yum install python-devel python-nose python-setuptools gcc gcc-gfortran gcc-c++ blas-devel lapack-devel atlas-devel
Q
What is OpenCV with python on CentOS7?
A
OpenCV-Python is a Python wrapper for the original OpenCV C++ implementation. OpenCV is an open source cross-platform computer vision algorithm library. It is used in all kind of visual processing areas, such as facial recognition, gesture recognition, human-computer interaction, Object identification, motion tracking and many more. In this tutorial, we will see the installation of OpenCV with python on Centos 7.
Q
Can I check the python current version?
A
Use the following command:
# python --version