Skip to content


Installing OpenCV 4.5.0 in Ubuntu 20.04 LTS

OpenCV was initially released about 20 years ago. It’s one of the most well established computer vision libraries in the world, with thousands of algorithm implementations ready to be used in commercial and research applications.

In this guide I’ll show you how to install OpenCV 4.5.0 in your Ubuntu 20.04 LTS and how to create computer vision applications with C++ and Python.

face detection

Note: I have noticed some copies of my posts elsewhere, so make sure that you are reading this from the original source, at samontab dot com, accessible from here so that you don’t miss the comments.

First, make sure you have the latest software installed:

sudo apt-get update
sudo apt-get upgrade

Now, you need to install some dependencies, such as support for reading and writing video files, drawing on the screen, some needed tools, etc… This step is very easy, you only need to write the following command in the Terminal:

sudo apt-get install build-essential cmake python3-numpy python3-dev python3-tk libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libavresample-dev libdc1394-dev libeigen3-dev libgtk-3-dev libvtk7-qt-dev

Time to download and compile OpenCV 4.5.0:

wget https://github.com/opencv/opencv/archive/4.5.0.tar.gz
tar -xvzf 4.5.0.tar.gz
rm 4.5.0.tar.gz
cd opencv-4.5.0
mkdir build;cd build
cmake -DBUILD_EXAMPLES=ON ..

configuration for opencv

Check that the above command produces no error and that in particular it reports FFMPEG as YES. If this is not the case you will not be able to read or write videos. Double check that every feature you want present in OpenCV is reported correctly. If you’re missing something, make sure to install the missing package and run the cmake line again. In some cases you might need to delete the whole build directory first to get a correct detection. Once you’re happy with what’s shown then we’ll build it. After running the following lines you can go grab a coffee as it will take a long time.

make -j4
sudo make install
echo '/usr/local/lib' | sudo tee --append /etc/ld.so.conf.d/opencv.conf
sudo ldconfig
echo 'PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig' | sudo tee --append ~/.bashrc
echo 'export PKG_CONFIG_PATH' | sudo tee --append ~/.bashrc
source ~/.bashrc

After that’s finished you should be able to use OpenCV with C++ and Python. Let’s do a quick check:

python3
import cv2
cv2.__version__

python_opencv

You should see no errors and 4.5.0 as the version.

Now let’s run some Python demos. You can start all of them from a central place:

cd ~/opencv-4.5.0/samples/python
python3 demo.py

python examples

You can also execute any of the already compiled C++ demos. For example let’s run the intelligent scissors demo:

cd ~/opencv-4.5.0/build/bin
./example_cpp_intelligent_scissors

If you click on the image and move your mouse you’ll see that the selection moves to match the object edges. Once you are ready selecting the contour of the object press right click and the object will be selected.

intelligent scissors

Now you’ll learn how to compile an application that uses OpenCV with C++ using cmake. Let’s copy another example source code and prepare it for compilation.

cd ~
mkdir sample
cd sample
cp ~/opencv-4.5.0/samples/cpp/text_skewness_correction.cpp .
cp ~/opencv-4.5.0/samples/data/imageTextR.png .

Now we’re going to create a cmake file that will allow us to compile the sample. Create a text file called CMakeLists.txt and put this content in it:

cmake_minimum_required (VERSION 3.0.2)
project (text_skewness_correction)
find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable (${PROJECT_NAME} text_skewness_correction.cpp)
target_link_libraries (${PROJECT_NAME} ${OpenCV_LIBS})

OK, now we’re ready to build it and run it:

mkdir build
cd build
cmake ..
make
./text_skewness_correction ../imageTextR.png

rotated text alignment

Now you have OpenCV 4.5.0 correctly installed in your Ubuntu 20.04 LTS system, and you know how to create applications using C++ and Python.

Posted in Computer Vision, Open Source, OpenCV.

Tagged with , , , , , , , , , .


Minimal install of OpenVINO 2020.4 in Kubuntu 20.04 LTS for C++ inference on CPU

The official installer of OpenVINO 2020.4 requires Ubuntu 18.04 LTS, but since Kubuntu 20.04 LTS is already out I wanted to use that instead. Here’s what you need to do to get a minimal install for C++ inference on the CPU.

First make sure you have everything up to date:

sudo apt-get update
sudo apt-get upgrade

You’ll need git installed to get the source code. Also, python3 comes already pre-installed with 20.04 but not python, so we’re going to assign any python calls to python3.

sudo apt-get install git python-is-python3

Now get the OpenVINO source code into your home directory (or wherever you prefer). Don’t download the zip file directly from github as there are some 3rd party dependencies that need to get downloaded as well. That’s the –recursive part of the following command. The specific –branch we’re using is the same as the official installer uses, releases/2020/4.

cd ~
git clone --recursive --branch releases/2020/4 https://github.com/openvinotoolkit/openvino.git

Install the dependencies by running the included script:

~/openvino/install_dependencies.sh

Before continuing you need to change some of the files you just downloaded. I’m using xdg-open which will invoke your preferred application but you can use whatever you prefer instead like vi, nano, etc.

First open up the gna_helper.cpp file.

xdg-open ~/openvino/inference-engine/src/gna_plugin/gna_helper.cpp

Update the code for these functions as follows:

void profilerRtcStart(intel_gna_profiler_rtc *p) {
    if (nullptr == p) return;
    clearTimeB(p->passed);
    clearTimeB(p->stop);
    //ftime(&p->start);
    timespec start;
    clock_gettime(CLOCK_REALTIME, &start);
    p->start.time = start.tv_sec;
    p->start.millitm = start.tv_nsec/1000000;
}

void profilerRtcStop(intel_gna_profiler_rtc *p) {
    if (nullptr == p) return;
    //ftime(&p->stop);
    timespec stop;
    clock_gettime(CLOCK_REALTIME, &stop);
    p->stop.time = stop.tv_sec;
    p->stop.millitm = stop.tv_nsec/1000000;
}

Now open the execution_engine.cpp file:

xdg-open ~/openvino/inference-engine/thirdparty/ade/sources/ade/source/execution_engine.cpp

In line 141, simply update this:

//return std::move(ret);
return ret;

Now you’re ready to build it. And this will take a long time.

  • ENABLE_MKL_DNN=ON means we want the CPU plugin
  • ENABLE_CLDNN=OFF means we don’t want the GPU plugin
cd ~/openvino
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DENABLE_MKL_DNN=ON -DENABLE_CLDNN=OFF -DENABLE_OPENCV=OFF -DENABLE_PYTHON=OFF ..
make -j4

You should now be able to run the compiled applications, for example to get information about your device you can run the following:

~/openvino/bin/intel64/Release/hello_query_device

It should output detailed information about your device. Check out the Intel Model Zoo for models and samples that you can use.

Posted in Open Source, OpenVINO, Programming.

Tagged with , , , .