Skip to main content

Building OpenCV with GStreamer Support

This guide explains how to build OpenCV with GStreamer support on Jetson.

There is an automatic bash script that you can use, or you can follow the instructions below. Download the automatic bash script.

Use bash script

Download the file to your jetson and run the following command:

bash opencv-build.sh

It will install OpenCV with gstreamer support and make it available in your python installation.

Python Installation and Setup

Update and Install Python 3.8

sudo apt-get update
sudo apt install python3.8

Set Python 3.8 as Default

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
sudo update-alternatives --config python3

Check Python and Pip Versions

python3 --version
pip3 --version
note

If the version for Python is not showing 3.8, proceed with the following commands:

Install Additional Packages (if needed)

sudo apt install python3.8-venv python3.8-distutils
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.8 get-pip.py

Run this again to verify:

python3 --version
pip3 --version
caution

If the versions are still not correct, you need to troubleshoot the problem before continuing with this instruction.

Library and Development Environment Setup

Install NVIDIA Performance Primitives Development Libraries

sudo apt update
sudo apt-get install libnpp-dev

Install Essential Build Tools and Libraries

sudo apt-get install -y build-essential cmake git libgtk3.0-dev libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y python2.7-dev python3.8-dev python-dev python-numpy python3-numpy
sudo apt-get install -y libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
sudo apt-get install -y libv4l-dev v4l-utils qv4l2 v4l2ucp
sudo apt-get install -y curl

OpenCV Installation

Clone OpenCV and Open3D Installation Repository

git clone https://github.com/asujaykk/Install-Opencv-and-open3D-in-Jetson-nano-with-cuda-support.git
cd Install-Opencv-and-open3D-in-Jetson-nano-with-cuda-support

Run Swap File Script

sudo bash 8gb_swap.sh
cd ..

Create Workspace Directory

mkdir workspace
cd workspace

Download OpenCV and Contrib Modules

curl -L https://github.com/opencv/opencv/archive/4.0.0.zip -o opencv-4.0.0.zip
curl -L https://github.com/opencv/opencv_contrib/archive/4.0.0.zip -o opencv_contrib-4.0.0.zip

Unzip OpenCV and Contrib Modules

unzip opencv-4.0.0.zip
unzip opencv_contrib-4.0.0.zip
cd opencv-4.0.0/
mkdir release
cd release/

Find Python Path

This finds the python paths, you might need to update the paths in the next step if they differ from the ones on your system:

python3.8 -m site

Configure OpenCV Build with CMake

note

nppicom has been deprecated in cuda11, but if you have a previous version you could specify the paths. You could also try to remove the cmake instructions referring to nppicom since they should be applied automatically. But if your system has cuda11 or later, this instruction tells the system to ignore the deprecated libraries.

cmake -D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D CUDA_ARCH_BIN="5.3,6.2,7.2" \
-D CUDA_ARCH_PTX="" \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.0.0/modules \
-D WITH_GSTREAMER=ON \
-D WITH_LIBV4L=ON \
-D BUILD_opencv_python2=OFF \
-D PYTHON_EXECUTABLE=/usr/bin/python3.8 \
-D BUILD_opencv_python3=ON \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_EXAMPLES=OFF \
-D CMAKE_BUILD_TYPE=RELEASE \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=/home/knightec/.local/lib/python3.8/site-packages \
-D CMAKE_INSTALL_PREFIX=/usr \
-D CUDA_nppicom_LIBRARY="" \
-D CUDA_nppicom_LIBRARY_RELEASE="" ..

Compile OpenCV

make -j$(nproc)

Install OpenCV

sudo make install

Final Setup

Set Python Alias

This makes it so that if you write python in the CLI it actually executes python3. This is useful if you want python to use python3 instead of python2:

echo "alias python=python3" >> ~/.bashrc
source ~/.bashrc