Monday, 18 November 2019

How to install OpenCV on Ubuntu 18.04

I needed to create (a virtual) environment on my Ubuntu dev box for project which uses Python 3 and OpenCV. To install OpenCV in it I followed, with some small modifications, instructions listed in Adrian Rosebrock's article Ubuntu 18.04: How to install OpenCV. Here I want to share my experience with this process.

When installing virtualenv make sure you use pip3. If you by mistake use pip:

$ sudo pip install virtualenv virtualenvwrapper
The directory '/home/bojan/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/bojan/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting virtualenv
Downloading https://files.pythonhosted.org/packages/c5/97/00dd42a0fc41e9016b23f07ec7f657f636cb672fad9cf72b80f8f65c6a46/virtualenv-16.7.7-py2.py3-none-any.whl (3.4MB)
100% || 3.4MB 442kB/s
Collecting virtualenvwrapper
Downloading https://files.pythonhosted.org/packages/c1/6b/2f05d73b2d2f2410b48b90d3783a0034c26afa534a4a95ad5f1178d61191/virtualenvwrapper-4.8.4.tar.gz (334kB)
100% || 337kB 2.9MB/s
Collecting stevedore (from virtualenvwrapper)
Downloading https://files.pythonhosted.org/packages/b1/e1/f5ddbd83f60b03f522f173c03e406c1bff8343f0232a292ac96aa633b47a/stevedore-1.31.0-py2.py3-none-any.whl (43kB)
100% || 51kB 11.4MB/s
Collecting virtualenv-clone (from virtualenvwrapper)
Downloading https://files.pythonhosted.org/packages/ba/f8/50c2b7dbc99e05fce5e5b9d9a31f37c988c99acd4e8dedd720b7b8d4011d/virtualenv_clone-0.5.3-py2.py3-none-any.whl
Collecting pbr!=2.1.0,>=2.0.0 (from stevedore->virtualenvwrapper)
Downloading https://files.pythonhosted.org/packages/46/a4/d5c83831a3452713e4b4f126149bc4fbda170f7cb16a86a00ce57ce0e9ad/pbr-5.4.3-py2.py3-none-any.whl (110kB)
100% || 112kB 6.7MB/s
Requirement already satisfied: six>=1.10.0 in /home/bojan/.local/lib/python2.7/site-packages (from stevedore->virtualenvwrapper)
Installing collected packages: virtualenv, pbr, stevedore, virtualenv-clone, virtualenvwrapper
Running setup.py install for virtualenvwrapper ... done
Successfully installed pbr-5.4.3 stevedore-1.31.0 virtualenv-16.7.7 virtualenv-clone-0.5.3 virtualenvwrapper-4.8.4

...then sourcing bashrc will fail:

$ source ~/.bashrc
/usr/bin/python3: Error while finding module specification for 'virtualenvwrapper.hook_loader' (ModuleNotFoundError: No module named 'virtualenvwrapper')
virtualenvwrapper.sh: There was a problem running the initialization hooks.

If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 and that PATH is
set properly.

We need to use pip3 so virtual environment is installed with Python3:

$ sudo pip3 install virtualenv virtualenvwrapper
The directory '/home/bojan/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/bojan/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting virtualenv
Downloading https://files.pythonhosted.org/packages/c5/97/00dd42a0fc41e9016b23f07ec7f657f636cb672fad9cf72b80f8f65c6a46/virtualenv-16.7.7-py2.py3-none-any.whl (3.4MB)
100% || 3.4MB 516kB/s
Collecting virtualenvwrapper
Downloading https://files.pythonhosted.org/packages/c1/6b/2f05d73b2d2f2410b48b90d3783a0034c26afa534a4a95ad5f1178d61191/virtualenvwrapper-4.8.4.tar.gz (334kB)
100% || 337kB 3.2MB/s
Collecting stevedore (from virtualenvwrapper)
Downloading https://files.pythonhosted.org/packages/b1/e1/f5ddbd83f60b03f522f173c03e406c1bff8343f0232a292ac96aa633b47a/stevedore-1.31.0-py2.py3-none-any.whl (43kB)
100% || 51kB 9.7MB/s
Collecting virtualenv-clone (from virtualenvwrapper)
Downloading https://files.pythonhosted.org/packages/ba/f8/50c2b7dbc99e05fce5e5b9d9a31f37c988c99acd4e8dedd720b7b8d4011d/virtualenv_clone-0.5.3-py2.py3-none-any.whl
Requirement already satisfied: six>=1.10.0 in /home/bojan/.local/lib/python3.6/site-packages (from stevedore->virtualenvwrapper)
Collecting pbr!=2.1.0,>=2.0.0 (from stevedore->virtualenvwrapper)
Downloading https://files.pythonhosted.org/packages/46/a4/d5c83831a3452713e4b4f126149bc4fbda170f7cb16a86a00ce57ce0e9ad/pbr-5.4.3-py2.py3-none-any.whl (110kB)
100% || 112kB 6.2MB/s
Installing collected packages: virtualenv, pbr, stevedore, virtualenv-clone, virtualenvwrapper
Running setup.py install for virtualenvwrapper ... done
Successfully installed pbr-5.4.3 stevedore-1.31.0 virtualenv-16.7.7 virtualenv-clone-0.5.3 virtualenvwrapper-4.8.4


$ sudo rm -rf ~/get-pip.py ~/.cache/pip


$ source ~/.bashrc
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/get_env_details

Now we can create virtual environment for our project:

$ mkvirtualenv py3.6_cv2 -p python3.6
Running virtualenv with interpreter /usr/bin/python3.6
Already using interpreter /usr/bin/python3.6
Using base prefix '/usr'
New python executable in /home/bojan/.virtualenvs/py3.6_cv2/bin/python3.6
Also creating executable in /home/bojan/.virtualenvs/py3.6_cv2/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/py3.6_cv2/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/py3.6_cv2/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/py3.6_cv2/bin/preactivate
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/py3.6_cv2/bin/postactivate
virtualenvwrapper.user_scripts creating /home/bojan/.virtualenvs/py3.6_cv2/bin/get_env_details


(py3.6_cv2) bojan@...$ workon py3.6_cv2

Let's check pip version in this virtual environment:

(py3.6_cv2) bojan@...$ which pip
/home/bojan/.virtualenvs/py3.6_cv2/bin/pip

(py3.6_cv2) bojan@...$ pip --version
pip 19.3.1 from /home/bojan/.virtualenvs/py3.6_cv2/lib/python3.6/site-packages/pip (python 3.6)

Installing necessary packages (only these were required for the OpenCV functionality in my project; some other packages might be necessary for different OpenCV features):

$ pip install numpy
$ sudo apt install clang cmake libgtk2.0-dev

Downloading and unpacking OpenCV source code (version 4.1.1 was the last one at the time of writing):

$ wget https://github.com/opencv/opencv/archive/4.1.1.zip
$ unzip 4.1.1.zip 
$ cd 4.1.1/
$ mkdir build
$ cd build/

If we don't explicitly specify Python version that cmake should use, cmake's output will contain reference to Python 2:

$ cmake -D ...
...
Python (for build): /usr/bin/python2.7

But if we have add options set in bold:

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_CUDA=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_ENABLE_NONFREE=OFF \
-D BUILD_EXAMPLES=OFF \
-D CMAKE_C_COMPILER=/usr/bin/clang \
-D CMAKE_CXX_COMPILER=/usr/bin/clang++ \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=ON \
-D HAVE_opencv_python3=ON \
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.6 \
.. \

...cmake will output:

...
Python (for build): /usr/bin/python3.6
...

Let's now build and install OpenCV:

~/Downloads/opencv-4.1.1/build$ make install
...
file cannot create directory: /usr/local/share/licenses/opencv4. Maybe
need administrative privileges.

We need to use sudo:

~/Downloads/opencv-4.1.1/build$ sudo make install
$ sudo ldconfig

Let's check OpenCV version:

(py3.6_cv2) bojan@...$ opencv_version 
4.1.1

cv2 package still can't be loaded to Python code:

$ python
Python 3.6.8 (default, Oct 7 2019, 12:59:55)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'cv2'
>>>
KeyboardInterrupt
>>> exit()


Lets' see where is Python binding for OpenCV:

$ sudo ls /usr/local/lib/python3.6/site-packages/cv2/python-3.6/
cv2.cpython-36m-x86_64-linux-gnu.so



We need to create soft link:

(py3.6_cv2) bojan@...:~/Downloads/opencv-4.1.1/build$ cd ~/.virtualenvs/py3.6_cv2/lib/python3.6/site-packages/

(py3.6_cv2) bojan@...:~/.virtualenvs/py3.6_cv2/lib/python3.6/site-packages$ ln -s /usr/local/lib/python3.6/site-packages/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so cv2.so


Importing cv2 package works now:

(py3.6_cv2) bojan@...:~/.virtualenvs/py3.6_cv2/lib/python3.6/site-packages$ python
Python 3.6.8 (default, Oct 7 2019, 12:59:55)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.1'
>>> quit()

1 comment:

micheal pan said...

BE SMART AND BECOME RICH IN LESS THAN 3DAYS....It all depends on how fast 
you can be to get the new PROGRAMMED blank ATM card that is capable of
hacking into any ATM machine,anywhere in the world. I got to know about 
this BLANK ATM CARD when I was searching for job online about a month 
ago..It has really changed my life for good and now I can say I'm rich and 
I can never be poor again. The least money I get in a day with it is about 
$50,000.(fifty thousand USD) Every now and then I keeping pumping money 
into my account. Though is illegal,there is no risk of being caught 
,because it has been programmed in such a way that it is not traceable,it 
also has a technique that makes it impossible for the CCTVs to detect 
you..For details on how to get yours today, email the hackers on : (
atmmachinehackers1@gmail.com ). Tell your 
loved once too, and start to live large. That's the simple testimony of how 
my life changed for good...Love you all ...the email address again is ;
atmmachinehackers1@gmail.com