Hello friends,
I am going to show you the installation of OpenCV from source, followed by installation of TensorFlow & Keras on Manjaro Linux. This is the best way to take advantage of features of these libraries. It also allows us to customize the installation. For example, you can choose the version of softwares if their latest version is unavailable or not working on your system.
This post is inspired from the tutorials on PyImageSearch and Techestorica.com.
Check the version of Python installed on your system:
First of all, we should check the version of python installed on the system, because different versions of TensorFlow requires different versions of python.
You can do this by typing following command in terminal:
$ python --version
You should get following output :
Python <version_number>
You can check if Python is working or not by doing following step:
$ python
>>>print("It is working!!")
It is working!!
>>>quit()
Well, that was the easy part. Now we start the real installation process.
I am going to show you the installation of OpenCV from source, followed by installation of TensorFlow & Keras on Manjaro Linux. This is the best way to take advantage of features of these libraries. It also allows us to customize the installation. For example, you can choose the version of softwares if their latest version is unavailable or not working on your system.
This post is inspired from the tutorials on PyImageSearch and Techestorica.com.
Check the version of Python installed on your system:
First of all, we should check the version of python installed on the system, because different versions of TensorFlow requires different versions of python.
You can do this by typing following command in terminal:
$ python --version
You should get following output :
Python <version_number>
You can check if Python is working or not by doing following step:
$ python
>>>print("It is working!!")
It is working!!
>>>quit()
Well, that was the easy part. Now we start the real installation process.
Installation Of OpenCV:
Step 1: Install Dependencies :
We need many dependencies for OpenCV to work properly. So open the terminal and make sure that you're in the home directory & if you're not then change directory using following command:
$ cd ~
Now, first update your system using following command:
$ sudo pacman -Syu
followed by installation of developer tools:
$ sudo pacman -S pkg-config cmake unzip
There is no need to install build-essentials as Manjaro already has PKGBUILD preinstalled.
Next, we install some OpenCV specific prerequisites. OpenCV is an image processing/ computer vision library & therefore it needs to be able to load standard image formats such as JPEG, PNG, TIFF etc. So we will install following dependencies:
$ sudo pacman -S libjpeg
$ sudo pacman -S libpng
$ sudo pacman -S libtiff
$ sudo pacman -S xvidcore
$ sudo pacman -S x264
$ sudo pacman -S ffmpeg
$ sudo pacman -S v4l-utils
$ sudo pacman -S gtk3
$ sudo pacman -S libcanberra
$ sudo pacman -S lapack
$ sudo pacman -S gcc-fortran
You can install each dependency separately or you can install 2-3 dependencies at a time, it is completely your choice.
$ unzip opencv_contrib.zip
$ mv opencv_contrib-<version> opencv_contrib
Step 2: Download official OpenCV source:
Now we'll download the official OpenCV source. We will use OpenCV version 3.4 since OpenCV version 4 does not work very well on Manjaro Linux.
Before starting to download, make sure you're in your home directory. If you're not, then run following command to do just that:
$ cd ~
After this, run following commands for downloading OpenCV main and contrib modules:
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/<opencv _version>.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/<opencv _contrib_version>.zip
Next, we will unzip these archives:
$ unzip opencv.zip
Next rename the folder for easy reference:
$ mv opencv-<version> opencv
$ mv opencv_contrib-<version> opencv_contrib
Step 3: Create virtual environment :
First, you'll need pip(Python Package Manager) on your system, so let's install it on the system
$ sudo pacman -S python-pip
Now, let's create a virtual environment. Python virtual environments allow us to work on our python projects in isolation and it also doesn't result in breakage of dependencies and resetting our system. They are best practice for python development.
Run the following command in terminal to download & install necessary components for creating python virtual environment:
$ sudo pip install virtualenv virtualenvwrapper
After installation, don't forget to clear package cache using following command:
$ sudo rm -rf ~/get-pip.py ~/.cache/pip
After this, we need to update our bash profile with some virtualenvwrapper settings to make the tools work together:
$ export WORKON_HOME=$HOME/.virtualenvs
$ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
$ source /usr/bin/virtualenvwrapper.sh
To avoid entering following commands each and every time, perform as follows:
Run the following command in terminal to download & install necessary components for creating python virtual environment:
$ sudo pip install virtualenv virtualenvwrapper
After installation, don't forget to clear package cache using following command:
$ sudo rm -rf ~/get-pip.py ~/.cache/pip
After this, we need to update our bash profile with some virtualenvwrapper settings to make the tools work together:
$ export WORKON_HOME=$HOME/.virtualenvs
$ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
$ source /usr/bin/virtualenvwrapper.sh
To avoid entering following commands each and every time, perform as follows:
$ echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc
The above commands will append the previously used 3 commands into the .bashrc file.
$ echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
$ echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.bashrc
$ echo "source /usr/bin/virtualenvwrapper.sh" >> ~/.bashrc
The above commands will append the previously used 3 commands into the .bashrc file.
Next, source the .bashrc file to save the changes
$ source ~/.bashrc
Now we're at the point at which we'll create our virtual environment to hold OpenCV and the additional components.
Simply type following command:
$ mkvirtualenv <env-name> -p python3
Your output should look like this:
To work in the this virtual environment afterwards, just use following command:
$ workon <env-name>
and Voila! You will enter in your virtual environment.
If you want to exit from the virtual environment, just use following command:
$ deactivate
and you'll exit from virtual environment.
Note:
For performing next steps, you need to be in the virtual environment. If you exit from virtual environment, than all the next steps will result in complete failure.
Simply type following command:
$ mkvirtualenv <env-name> -p python3
Your output should look like this:
To work in the this virtual environment afterwards, just use following command:
$ workon <env-name>
and Voila! You will enter in your virtual environment.
If you want to exit from the virtual environment, just use following command:
$ deactivate
and you'll exit from virtual environment.
Note:
For performing next steps, you need to be in the virtual environment. If you exit from virtual environment, than all the next steps will result in complete failure.
Step 4: Building OpenCV
Let’s set up our OpenCV build using CMake.
After creating virtual environment, enter in it and follow the steps given below:
1. Install pip:
In the latest version of python, you'll need to install pip in virtual environment as well.
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py
$ sudo rm -rf ~/get-pip.py ~/.cache/pip
1. Install pip:
In the latest version of python, you'll need to install pip in virtual environment as well.
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py
$ sudo rm -rf ~/get-pip.py ~/.cache/pip
2. Install NumPy:
The first package and only Python prerequisite we’ll install is NumPy
$ pip install numpy
3. Change the directory to OpenCV folder:
$ cd opencv
4. Create a folder named "build":
$ mkdir build
5. Change direcory to build
$ cd build
6. Run following commands to compile and build using CMake like shown in picture below:
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_CUDA=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
After successful execution of above commands, you should get a screen like this:
7. Now, compile OpenCV using make command:
$ make -j4
Note:
Here, "-j4" means CPU cores assigned to perform this process. If you want, you can assign lesser cores or more cores by changing the number in the argument.I chose 8 because my CPU is an 8 core AMD APU (i.e. 4 Compute Cores + 4 Graphic Cores).
You'll get a screen like following:
This process may take up to 15 to 30 minutes, which depends on the number of cores you have selected for the process.
7. Install OpenCV:
Let's install OpenCV using following commands:
$ sudo make install
$ sudo ldconfig
To verify the install, run following command:
$ pkg-config --modversion opencv
You should get an output :
3.4.4
8. Add the finishing touches:
At this point, your Python 3 bindings for OpenCV should reside in the following folder:
$ ls /usr/local/python/cv2/python-3.7
cv2.cpython-36m-x86_64-linux-gnu.so (output)
Then rename them:
$ cd /usr/local/python/cv2/python-3.8
$ sudo mv cv2.cpython-37-x86_64-linux-gnu.so cv2.so
Lastly sym-link the OpenCV bindings:
$ cd '/home/<username>/.virtualenvs/cv/lib/python<version>/site-packages'
$ ln -s /usr/local/python/cv2/python-<version>/cv2.so cv2.so
Perform one last verification:
$ cd ~
$ workon cv
$ python
Python 3.7.2 (default, Jan 10 2019, 16:09:48)
[GCC 8.2.1 20181127] on linux
Type "help", "copyright", "credits" or "license" for more information..
>>> import cv2
>>> cv2.__version__
'3.4.4'
>>> quit()
Congratulations! The Installation of OpenCV is finished completely now.
Installation of TensorFlow & Keras:
Here I'm going to show you installation process of TensorFlow's CPU edition since it can be installed on almost every machine. TensorFlow's GPU edition specifically requires NVIDIA GPUs, and I'm just not sure that everyone has one.
After TensorFlow, we will also install Keras, yet another Machine Learning API ,which can be used for image detection. So without further ado, let's get going.
Install dependencies:
Like OpenCV, we need many dependencies for TensorFlow & Keras to work properly. So open the terminal and make sure that you're in the home directory & if you're not then change directory using following command:
$ cd ~
Now, first update your system using following command:
$ sudo pacman -Syu
followed by installation of developer tools:
$ sudo pacman -S pkg-config cmake unzip
There is no need to install build-essentials as Manjaro already has PKGBUILD preinstalled.
Next, we install some TensorFlow specific prerequisites i.e. we will install following dependencies:
$ sudo pacman -S libxmu
$ sudo pacman -S libxi
$ sudo pacman -S mesa
$ sudo pacman -S glu
$ sudo pacman -S libjpeg
$ sudo pacman -S libpng
$ sudo pacman -S libtiff
$ sudo pacman -S xvidcore
$ sudo pacman -S x264
$ sudo pacman -S ffmpeg
$ sudo pacman -S v4l-utils
$ sudo pacman -S gtk3
$ sudo pacman -S blas
$ sudo pacman -S blas
$ sudo pacman -S hdf5
$ sudo pacman -S lapack
$ sudo pacman -S gcc-fortran
$ sudo pacman -S python-pillow
$ sudo pacman -S tk
$ sudo pacman -S python-pillow
$ sudo pacman -S tk
Installation Of TensorFlow:
Now, enter the virtual environment in which you have installed OpenCV using the following steps:
$ export WORKON_HOME=$HOME/.virtualenvs
$ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
$ source /usr/bin/virtualenvwrapper.sh
$ workon <env_name>
Next, we have to install some python libraries that are required by TensorFlow.
$ pip install numpy
$ pip install opencv-contrib-python
$ pip install scipy matplotlib pillow
$ pip install imutils h5py requests progressbar2
$ pip install scikit-learn scikit-image
After successful installation of above libraries, install TensorFlow & Keras:
$ pip install tensorflow
$ pip install keras
Important:
If you have upgraded to next version of python, and stable version of TensorFlow is not available for your python version, then you can try nightly version of TensorFlow.
$ pip install tf-nightly
Warning:
IF you are installing nightly version, use it with caution and you must install it only in a virtual environment. Since it is an unstable version, installing it in main system can break dependencies and you may lose your data.
After installation is completed, then verify the installation as follows:
$ python
>>> import tensorflow
>>>
Similarly, for Keras
$ python
>>> import tensorflow
>>>
Similarly, for Keras
$ python
>>> import keras
Using TensorFlow backend.
>>
Note:
It shows few warnings, but they can be ignored as long as you are using TensorFlow's CPU version.
>>> import keras
Using TensorFlow backend.
>>
Note:
It shows few warnings, but they can be ignored as long as you are using TensorFlow's CPU version.
There!
You have successfully completed the installation of OpenCV, TensorFlow & Keras. Now you can use these for your machine learning based image processing projects.
Important Note:
Since Manjaro is an Arch linux based Distribution, make sure to update your version of the above softwares and dependencies on time. If you try to run this in old versions of python, then it would cause various errors and exceptions to trigger and it will be a nightmare.
Also, keep at least 1 backup image of OS prepared using Timeshift, if things don't work out on new updates.
You have successfully completed the installation of OpenCV, TensorFlow & Keras. Now you can use these for your machine learning based image processing projects.
Important Note:
Since Manjaro is an Arch linux based Distribution, make sure to update your version of the above softwares and dependencies on time. If you try to run this in old versions of python, then it would cause various errors and exceptions to trigger and it will be a nightmare.
Also, keep at least 1 backup image of OS prepared using Timeshift, if things don't work out on new updates.






Nice tutorial
ReplyDelete