[LINUX] Build Python3 and OpenCV environment on Ubuntu 18.04


I decided to try customer analysis by face recognition using Python with Raspberry Pi and camera, and build a test environment with VM. The goal this time is to create an Ubuntu virtual environment on a Mac and run Python 3 and OpenCV on Ubuntu.


--Assuming that Ubuntu 16.04 works --Introduced Python 3.6.9 --Introduced OpenCV 4.2.0

Installation of libraries that depend on OpenCV

I'm using the git command to get the source code.

$ 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 libdc1394-22-dev

Obtaining the source code

I'm using the git command to get the source code.

$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git

When downloading with the wget command, unzip the zip file with the unzip command.

$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.2.0.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/master.zip

$ unzip opencv.zip
$ unzip opencv_contrib

Check the directory where the source is saved.

OpenCV compilation and build

Build OpenCV. In case of cmake, contrib is also built together by adding -D OPENCV_EXTRA_MODULES_PATH = ../../opencv_contrib/modules.

$ cd ~/opencv-4.2.0
$ mkdir build
$ cd build
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \

Compile OpenCV.

$ make -j4

[100%] Build target opencv_python3   //If this message appears, the compilation is successful.

In my Mac Book Pro VM environment, it took about an hour to compile and build. After successfully compiling and building, install OpenCV.

$ sudo make install
$ sudo ldconfig

Check the version of OpenCV.

$ opencv_version

Installation of Python 3 and related modules

First, install pip, which manages Python packages. Make the following modules available (numpy, pandas, matplotlib, sklearn).

$ sudo install pip3
$ pip3 install numpy pandas matplotlib sklearn

Operation check

Enter the python console and check if various modules can be imported. If there are no errors, the module is successfully installed. Check the version of opnecv just in case.

$ python3
>>>import numpy
>>>import pandas
>>>import sklearn
>>>import matplotlib
>>>import cv2       //This is the opencv library
>>>cv2.__version__  //Check the version of opencv

You can now use Python 3 and OpenCV on the virtual machine Ubuntu. If you want to do deep learning, you should be able to use it by installing a library for deep learning (tensorflow, keras, etc.) with pip3. Next time, I would like to execute the face recognition sample code using OpenCV.

Reference site

・ Click here Install Python3.6 and OpenCV (Ubuntu18.04LTS) The article was very helpful. ・ This Build opencv from source article was also helpful. ・ The official OpenCV website is here .

Recommended Posts

Build Python3 and OpenCV environment on Ubuntu 18.04
Build Python 3.8 + Pipenv environment on Ubuntu 18.04
OpenCV3 & Python3 environment construction on Ubuntu
build Python on Ubuntu
Python virtual environment and packages on Ubuntu
Created Ubuntu, Python, OpenCV environment on Docker
Build a Python + OpenCV environment on Cloud9
Build python environment with pyenv on EC2 (ubuntu)
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
Build python3 environment with ubuntu 16.04
Build Python environment on Windows
Install OpenCV on Ubuntu + python
Build python environment on windows
Install Python3 on Mac and build environment [Definitive Edition]
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Environment construction of python and opencv
Prepare Python development environment on Ubuntu
Building a Python environment on Ubuntu
Build and install OpenCV on Windows
Build a python3 environment on CentOS7
How to build a new python virtual environment on Ubuntu
Build an Ubuntu python development environment on Google Cloud Platform
Python 2.7, 3.4, 3.5 extension module build environment on Windows
Build a python environment on MacOS (Catallina)
Steps to install Python environment on Ubuntu
Build Python environment with Anaconda on Mac
Create an OpenCV3 + python3 environment on OSX
Install pyenv and Python 3.6.8 on Ubuntu 18.04 LTS
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Build a Python environment on your Mac with Anaconda and PyCharm
# 3 Build a Python (Django) environment on AWS EC2 instance (ubuntu18.04) part2
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Build and try an OpenCV & Python environment in minutes using Docker
Build Python environment on Ubuntu (when pip is not the default)
Install Python 3.3 on Ubuntu 12.04
Simply build a Python 3 execution environment on Windows
[Latest] How to build Java environment on Ubuntu
Install python package in personal environment on Ubuntu
Build a python environment with ansible on centos6
Build a Python environment on Mac (Mountain Lion)
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Build a Python development environment on your Mac
[Venv] Create a python virtual environment on Ubuntu
Build Python3 + flask environment on GCP Compute Engine
Build a Kubernetes environment for development on Ubuntu
Try using tensorflow ① Build python environment and introduce tensorflow
How to build Java environment on Ubuntu (Linux)
Install MongoDB on Ubuntu 16.04 and operate via python
Build PyPy and Python execution environment with Docker
Build a Python development environment on Raspberry Pi
Don't work Python with OpenCV on AMD Ryzen CPU on WSL2 Ubuntu 18.04 And 20.04
Try to build python and anaconda environment on Mac (by pyenv, conda)
Set up Python 3.4 on Ubuntu
Build a GVim-based Python development environment on Windows 10 (3) GVim8.0 & Python3.6
Build an OpenCV4 environment on Raspberry Pi using Poetry
Use matplotlib on Ubuntu 12 & Python
Python on Ruby and angry Ruby on Python
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a machine learning Python environment on Mac OS
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]
Put Python 3.x on Ubuntu