top of page
Search

Project 4b - OpenCV with CUDA support

  • Writer: James Canova
    James Canova
  • Sep 11, 2021
  • 2 min read

Project start: 28 December 2021

Project finish: 30 December 2021


Objective: Installation of OpenCV with CUDA support in VEnv



Software installation:



outside VEnv:

sudo apt-get update

sudo apt-get upgrade

uninstall preloaded version of OpenCV which does not have CUDA enabled:


pip3 uninstall opencv-python

sudo apt purge libopencv-dev libopencv-python libopencv-samples *libopencv*

make sure CUDA can be found:

sudo sh -c "echo '/usr/local/cuda/lib64' >> /etc/ld.so.conf.d/nvidia-tegra.conf"

sudo ldconfig


install dependencies:

sudo apt-get install build-essential cmake git unzip pkg-config 
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libgtk2.0-dev libcanberra-gtk*
sudo apt-get install python3-dev python3-numpy
sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev
sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev
sudo apt-get install libv4l-dev v4l-utils
sudo apt-get install libavresample-dev libvorbis-dev libxine2-dev
sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install libopenblas-dev libatlas-base-dev libblas-dev
sudo apt-get install liblapack-dev libeigen3-dev gfortran  
sudo apt-get install libhdf5-dev protobuf-compiler
sudo apt-get install libprotobuf-dev libgoogle-glog-dev libgflags-dev


download OpenCV 4.5.2 from Github:



unzip the folders, rename them and clean up the zipped folders:

unzip opencv.zip
unzip opencv_contrib.zip
mv opencv-4.5.2 opencv
mv opencv_contrib-4.5.2 opencv_contrib
rm opencv.zip
rm opencv_contrib.zip


build OpenCV:

Set swap space to 8 GB:

sudo swapoff -a
free -h   
sudo fallocate -l 8G /var/swapfile
sudo chmod 600 /var/swapfile
sudo mkswap /var/swapfile
sudo swapon /var/swapfile
sudo bash -c 'echo "/var/swapfile swap swap defaults 0 0" >> /etc/fstab'
sudo reboot now
free -h   


jtop:


a) add 4GB of Extra swap space (in addition to 8GB aleady set)


b)activate jetson-clocks (this will provide maximim CPU performance and the fan will turn on)


outside of VEnv:


cd $HOME

create the build directory:

$ cd opencv

$ mkdir build

$ cd build

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=5.3 \ -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 CMAKE_C_COMPILER=/usr/bin/gcc-7 \ -D BUILD_EXAMPLES=OFF ..

prepare to make:

make clean

make (using all 4 processors):

make -j4  (takes over 2.6 hours)


finish build and clean-up:

sudo make install

sudo ldconfig

make clean

sudo rm -rf ~/opencv

sudo rm -rf ~/opencv_contrib


jtop:


a) disable 4GB of Extra swap space


b) deactivate jetson-clocks (note that the fan will turn off)



Outside VEnv:


copy cv2 directory :

from:

/usr/lib/python3.6/dist-packages/

to:

/home/<user_name>/Public/Projects/myPython36VEnv/lib/python3.6/site-packages/

test installation:


in VEnv:

python3
>>>import cv2
>>>print("OpenCV version: ", cv2.__version__)
->OpenCV version:  4.5.2
>>>count = cv2. cuda.getCudaEnabledDeviceCount()
print(count)
->1




If you have any problems or need clarification please contact me: jscanova@gmail.com

 
 
 

Recent Posts

See All
Introduction

I became interested in artificial intelligence in mid 2021 after watching a documentary on this subject. In particular, I'm interested in...

 
 
 
References

1.Rashid, Tariq, Make Your Own Neural Network , Amazon , 2016 2.Portilla, Jose, "Python for Computer Vision with OpenCV and Deep...

 
 
 
Status of Projects

Last updated: 30 March 2024 Project 0: Read this to get a very basic understating on machine learning and neural networks. Project 1:...

 
 
 

Comments


Post: Blog posts

©2021 by My Machine Learning Blog. Proudly created with Wix.com

bottom of page