Build OpenCV 4 from source with Gstreamer (Ubuntu/Zorin/Peppermint)

Alcatraz47
3 min readSep 24, 2020

--

If you want to build your machine’s environment from source with OpenCV and Gstreamer then you are welcome to read the blog till the end. I myself had to face a true pain in the head while I was told to do that for the first time at my job. So…. let’s begin!

Requirements:

  1. Ubuntu(16.04–20.04)/Zorin(15.1 or newer, or matching with Ubuntu versions stated before)/Peppermint(10 or newer, or matching with Ubuntu versions stated before).
  2. Anaconda (if you want to build in a separate environment. I will use anaconda).
  3. Gstreamer installed in the pc
  4. OpenCV
  5. Some codec files which will be shown below.

At first, update and upgrade your system using the command below:

sudo apt update

sudo apt upgrade

Secondly, install Gstreamer and it’s plugins along with its tools using the commands below:

sudo apt-get install libgstreamer1.0–0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-doc gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio

sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

sudo apt install gstreamer1.0-tools

Then, install the codec libraries from apt:

sudo apt-get install -y libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libavresample-dev

Now, install “pkg-config” and “gtk2” or “gtk3”. I will be using “gtk3”. Install them from apt.

sudo apt pkg-config
sudo apt install libgtk-3-dev

#for libgtk2.0-dev:

sudo apt install libgtk2.0-dev

To enable IP camera stream option also, install the package below:

sudo apt install libdc1394–22-dev

Now clone the repositories of OpenCV and OpenCV contrib from their GitHub page and put them in your home directory.

https://github.com/opencv/

To clone the project from GitHub, run the commands below in your terminal:

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

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

Now, cd into your cloned “opencv” folder and make a directory named build to put the build files in there during “make” and then cd into the build folder.

cd /home/arfan/opencv

mkdir build

cd build

Now create an Anaconda environment in your pc with python3 using the command below and activate your environment using:

conda create -n “your_env_name” python=3

conda activate “your_env_name”

Then, use the pipeline below to “cmake” into the build folder.

CMake Command to Run.

From the above section, replace “/home/arfan/OpenCV/opencv_contrib/modules” with your path to the OpenCV contrib modules. After it, press enter and wait a few seconds until you see this screen:

Build Information after “CMake”

Check the build information output and scroll down to see whether Gstreamer is on or not. You will see “YES” if it is on. Check and re-check your Python 3 path and also Python for build path. Usually, Python 2 will be used to build the process. Do not get tensed after seeing my DC1394 is not enabled. I did not install it on my laptop!

Now write in the terminal the command below to build:

sudo make -j$(nproc)

I have used all the processors but you can specify the number of cores needed to be used. Now, press enter and go on for a walk. In my laptop, it took 1 hour to build(!!!!) whereas it took 15–20 mins in my workstation. In my office server, it took only 10 mins!

After completing your building install the make files and create necessary links and cache for it.

sudo make install

sudo ldconfig

It is DONE! To check the build, you can either use your terminal and write:

python

import cv2

print(cv2.getBuildInformation())

you will see a similar output as the above picture if everything is correct or you can use the code below after attaching your webcam to your machine:

That’s it! I hope you liked it! :)

--

--