Skip to content

Instantly share code, notes, and snippets.

@mtc-20
Last active January 15, 2024 10:38
Show Gist options
  • Save mtc-20/c1f324f70fad774ca6f381c07cb3f19a to your computer and use it in GitHub Desktop.
Save mtc-20/c1f324f70fad774ca6f381c07cb3f19a to your computer and use it in GitHub Desktop.
Basic shell script to build OpenCV 4.5.1 on Jetson TX2(Jetpack 4.6)

Build OpenCV 4.5.1 on Jetson TX2

Tested on Jetson TX2 running Jetpack 4.6

This is taken from @QEngineering shell script that was written for Jetson Nano

Requirements

Make sure you have sufficient space (atleast 6.5GB if I remember right)

  free -m

Usage

  $ wget https://gist.githubusercontent.com/mtc-20/c1f324f70fad774ca6f381c07cb3f19a/raw/bcb5ff467ae883fdacc3bad0c96372e4f2def4c1/OpenCV-4-5-1-TX2.sh

  $ sudo chmod 755 ./OpenCV-4-5-1-TX2.sh

  $ ./OpenCV-4-5-1-TX2.sh

  $ rm ./OpenCV-4-5-1-TX2.sh
#!/bin/bash
echo "Installing OpenCV 4.5.1 on your Jetson TX2"
echo "It will take 2 hours !"
# reveal the CUDA location
cd ~
sudo sh -c "echo '/usr/local/cuda/lib64' >> /etc/ld.so.conf.d/nvidia-tegra.conf"
sudo ldconfig
# install the dependencies
sudo apt-get install -y build-essential cmake git unzip pkg-config
sudo apt-get install -y libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install -y libgtk2.0-dev libcanberra-gtk*
sudo apt-get install -y python3-dev python3-numpy python3-pip
sudo apt-get install -y libxvidcore-dev libx264-dev libgtk-3-dev
sudo apt-get install -y libtbb2 libtbb-dev libdc1394-22-dev
sudo apt-get install -y gstreamer1.0-tools libv4l-dev v4l-utils
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y libavresample-dev libvorbis-dev libxine2-dev
sudo apt-get install -y libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install -y libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install -y libopenblas-dev libatlas-base-dev libblas-dev
sudo apt-get install -y liblapack-dev libeigen3-dev gfortran
sudo apt-get install -y libhdf5-dev protobuf-compiler
sudo apt-get install -y libprotobuf-dev libgoogle-glog-dev libgflags-dev
# remove old versions or previous builds
cd ~
sudo rm -rf opencv*
# download the latest version
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.1.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.1.zip
# unpack
unzip opencv.zip
unzip opencv_contrib.zip
# some administration to make live easier later on
mv opencv-4.5.1 opencv
mv opencv_contrib-4.5.1 opencv_contrib
# clean up the zip files
rm opencv.zip
rm opencv_contrib.zip
# set install dir
cd ~/opencv
mkdir build
cd build
# run cmake
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D EIGEN_INCLUDE_PATH=/usr/include/eigen3 \
-D WITH_OPENCL=OFF \
-D WITH_CUDA=ON \
-D CUDA_ARCH_BIN=6.2 \
-D CUDA_ARCH_PTX="" \
-D WITH_CUDNN=ON \
-D WITH_CUBLAS=ON \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D OPENCV_DNN_CUDA=ON \
-D ENABLE_NEON=ON \
-D WITH_QT=OFF \
-D WITH_OPENMP=ON \
-D WITH_OPENGL=ON \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=ON \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=TRUE \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF ..
# run make
FREE_MEM="$(free -m | awk '/^Swap/ {print $2}')"
# Use "-j 4" only swap space is larger than 3.5GB
if [[ "FREE_MEM" -gt "3500" ]]; then
NO_JOB=4
else
NO_JOB=1
fi
make -j ${NO_JOB}
sudo rm -r /usr/include/opencv4/opencv2
sudo make install
sudo ldconfig
# cleaning (frees 300 MB)
make clean
sudo apt-get update
echo "Congratulations!"
echo "You've successfully installed OpenCV 4.5.1 on your Jetson TX2"
@hsrwrobotics
Copy link

Stack Overflow post that might help if planning to build in virtual environments

@mtc-20
Copy link
Author

mtc-20 commented Apr 1, 2022

If you're lazy like me and want to use this built version of OpenCV in your conda environment

Just remember to replace <env_name> with env name of choice and with the corresponding version of python for which OpenCV was built

  • First create a conda environment( say cuda_cv) with the same python version (in my case it was 3.6.9), so then like so

    conda create --name <env_name> python=<version> -c conda-forge
  • Next copy cv2.so file into the virtual environment's package directory

    cd ~/miniconda3/envs/<env_name>/lib/python<version>/site-packages
    cp /usr/lib/python<version>/dist-packages/cv2/python-<version>/<cv2.so> .
  • Install Numpy on the virtual environment (might want to make this matches the version being used by the built CV)

    conda activate <env_name>
    conda install numpy
  • Now check OpenCV version inside the virtual environment python

    conda activate <env_name>
    python
    >>  import cv2
    >>  cv2.__version__
    
    >>  cv2.cuda.getCudaEnabledDeviceCount()
    
    

@Tuandn169
Copy link

Hi, thanks for your post. I followed and the program said "You have successfully installed OpenCV 4.5.1 on your Jetson TX2" but I cannot import cv2. Hope you can help me to solve this error. thank you ....

@Tuandn169
Copy link

image

@Tuandn169
Copy link

here are some errors:
image

@mtc-20
Copy link
Author

mtc-20 commented Oct 21, 2022

Hi @Tuandn169 , It will be hard to troubleshoot without more info. So, first off,

  1. This script I wrote is specifically for the Nvidia Jetson TX2. And from your photos, it looks like a desktop computer. Are you sure you are installing OpenCV for a Jetson device?
  2. My script is very basic, it does not check if OpenCV is actually installed and will always print out the You have successfully installed OpenCV 4.5.1 on your Jetson TX2. (I know this is a bad approach, but I was new to shell scripts back then)
  3. If you look through the errors photo you sent it actuall states
    Configuring incomplete, errors occurred
    No targets specified and no makefile found
    This means that the script does not work for your hardware, whatever it is
  4. In order to make it work for your device, you will need to make some changes to the script, but I am not the best person to help with that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment