Hi,
Following up my Python compilation guide for DRIVE PX2, I wanted to utilise OpenCV 4.5+ with CUDA integration. This was only possible with compiling from scratch (No cross compilation => Directly on PX2) and here is a quick command list of how I did it.
Note:
- Many dependencies required for the compilation here are maybe covered by the above Python compilation guide. So, it is advisable to do it first and then follow up on here.
- Entire Repo of OpenCV and OpenCV contrib (along with build files) can occupy a lot of storage, so it is recommended to have atleast 1.5+ GB free storage.
- During the build process, I have noticed number of hang ups and crashes caused by lack of memory (not storage). To prevent this issue, create a swap file for atleast 2 GB or use less number of cores (<4) while building
- I couldn’t get OpenCV_CuDNN working. So, it is disabled in the build.
Installation:
- Install deps
sudo apt install libjpeg-dev libpng-dev libtiff-dev
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libavresample-dev
sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt install libxvidcore-dev x264 libx264-dev libmp3lame-dev libtheora-dev
sudo apt install libmp3lame-dev libvorbis-dev
sudo apt install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install libdc1394-22 libdc1394-22-dev libxine2-dev libv4l-dev v4l-utils
cd /usr/include/linux
sudo ln -s -f ../libv4l1-videodev.h videodev.h
cd ~
sudo apt-get install libgtk-3-dev
sudo apt-get install libtbb-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install libprotobuf-dev protobuf-compiler
sudo apt-get install libgoogle-glog-dev libgflags-dev
sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
- Download OpenCV 4.5.2 and corresponding OpenCV Contrib (Adjust the paths to your specific system)
wget -O source_opencv.zip https://github.com/opencv/opencv/archive/refs/tags/4.5.5.zip
wget -O source_opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.5.zip
unzip source_opencv.zip
unzip source_opencv_contrib.zip
cd opencv-4.5.25
mkdir build
cd build
- Build (Again, change paths for Python and OpenCV Contrib)
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_TBB=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_CUDA=ON \
-D BUILD_opencv_cudacodec=ON \
-D WITH_CUDNN=OFF \
-D OPENCV_DNN_CUDA=OFF \
-D CUDA_ARCH_BIN='6.1;6.2' \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D WITH_OPENGL=ON \
-D WITH_GSTREAMER=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=/home/nvidia/venv_new/lib/python3.8/site-packages \
-D PYTHON_EXECUTABLE=/home/nvidia/venv_new/bin/python \
-D OPENCV_EXTRA_MODULES_PATH=/home/nvidia/opencv_compile/opencv_contrib-4.5.5/modules \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D BUILD_EXAMPLES=OFF ..
sudo make install
The entire build on Drive PX2 can take couple of hours.
- Add it to path, so that system can identify OpenCV
sudo /bin/bash -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
Now you can use OpenCV in your specified Python Virtual Environment or through C++ API.
These commands were tested and worked as of Mai 2022. Hope it helps !!
Any suggestions/improvements are highly appreciated.
Cheers,
Vishal Balaji
Perception Engineer@Schanzer Racing