The goal of this post is to document how to (attempt to) set up an Ubuntu environment with GPU OpenCV and allow for real-time object recognition on Real Time Streaming Protocol (RTSP) cameras. This would provide a solution for processing home security video feed or other video streaming sources.
Step 1: Installation Process
Creating the appropriate environment and setting up the software has been extremely tedious so far and motivated me to create this post. Essentially it comes down to the need for the OpenCV software to be compiled from source to turn on the GPU/CUDA flags to leverage a GPU rather than being dependent upon CPU processing, which is the default for the Ubuntu repo versions.
Dependencies
First there is a need to install video, imaging, and audio processing for Ubuntu. The following dependencies have been listed as required for OpenCV from other blog posts.
sudo apt-get update
sudo apt-get upgrade
Then install the dependencies for image and video processing.
sudo apt install gcc-8 g++-8
sudo apt install build-essential cmake pkg-config unzip yasm git checkinstall
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 libfaac-dev libmp3lame-dev libtheora-dev
sudo apt install libfaac-dev 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
sudo apt install python3-testresources
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
Install Tesseract for OCR
sudo apt-get install libtesseract-dev
One issue is to ensure also that libcublas10 is version 10.1 and not 10.0 or 10.2 as it corresponds to your CUDA version.
sudo add-apt-repository multiverse
sudo apt-cache madison libcublas10
sudo apt-get install libcublas10=10.1.[243-3] -V
I am using anaconda and am installing to the base installation environment but you can also create an anaconda environment for OpenCV specifically. See this link for examples on using custom anaconda environment: https://medium.com/machine-learning-mindset/opencv-anaconda-installation-in-ubuntu-98e4707ef611
Getting and Installing
I am creating a temporary directory for installing OpenCV.
mkdir ~/tmp/
cd ~/tmp
wget https://github.com/opencv/opencv/archive/master.zip
unzip master.zip
rm master.zip
wget https://github.com/opencv/opencv_contrib/archive/master.zip
unzip opencv_contrib.zip
rm master.zip
Then compile OpenCV (Note: you have to set the CUDA_ARCH_BIN=X.X based on your graphics card, information on the NVidia Dev website).
cd opencv-4.2.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_C_COMPILER=/usr/bin/gcc-8 \
-D CMAKE_INSTALL_PREFIX=/home/john/anaconda3 \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D WITH_TBB=ON \
-D WITH_CUDA=ON \
-D BUILD_opencv_cudacodec=OFF \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-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_EXTRA_MODULES_PATH=~/tmp/opencv_contrib-master/modules \
-D OPENCV_PYTHON3_INSTALL_PATH=/home/john/anaconda3/lib/python3.8/site-packages \
-D PYTHON_DEFAULT_EXECUTABLE=/home/john/anaconda3/bin/python \
-D PYTHON_EXECUTABLE=/home/john/anaconda3/bin/python3 \
-D PYTHON2_EXECUTABLE=/home/john/anaconda3/bin/python2 \
-D PYTHON3_EXECUTABLE=/home/john/anaconda3/bin/python3 \
-D PYTHON_INCLUDE_DIR=/home/john/anaconda3/include/python3.8 \
-D PYTHON_PACKAGES_PATH=/home/john/anaconda3/lib/python3.8/site-packages \
-D PYTHON3_LIBRARY=/home/john/anaconda3/lib/libpython3.8.so \
-D ZLIB_LIBRARY_RELEASE=/home/john/anaconda3/lib/libz.so \
-D PNG_LIBRARY_RELEASE=/home/john/anaconda3/lib/libpng.so \
-D JPEG_LIBRARY=/home/john/anaconda3/lib/libjpeg.so \
-D TIFF_LIBRARY_RELEASE=/home/john/anaconda3/lib/libtiff.so \
-D BUILD_EXAMPLES=ON \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D CUDNN_LIBRARY=/usr/local/cuda/lib64/libcudnn.so.7.6.5 \
-D CUDNN_INCLUDE_DIR=/usr/local/cuda/include \
-D CUDA_ARCH_BIN=7.5 ..
After generating the make files for the build,
nproc
make -j12 VERBOSE=1
My make would often run but ultimately fail randomly. I found a work around is to clear the make cache file and rerun it.
rm CMakeCache.txt
make -j12 VERBOSE=1
sudo make install
sudo ldconfig
With the sudo ldconfig to update the maintained the shared library cache.
After the make and make install, OpenCV is installed but you need to manually go to /usr/local/lib/python3.8/site-packages/cv2/python-3.8/ and rename the OpenCV library file and move it to the anaconda library path for the base python environment:
sudo cp cv2.cpython-38-x86_64-linux-gnu.so cv2.so
cd ~/anaconda3/lib/python3.8/site-packages/
ln -s /usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.so cv2.so
Then test it by loading python and loading the module.
$ python
Python 3.8.5 (default, Sep 4 2020, 07:30:14)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.5.0-pre'
Issues:
There are issues specific to my experiences installing OpenCV with Anaconda. First, I had issues with libcublas10 with the package installing 10.2 rather than the 10.1 version, which corresponds to the CUDA version. I had to downgrade the libcublas version with apt
sudo apt-get install libcublas10=10.1.243-3 -V
After downgrading I think I had an issue with CUDA so verify your CUDA version.
During cmake install, the process would fail and found a solution of removing the tiff package from anaconda (conda uninstall libtiff) but also just deleting the CMakeCache.txt file and rerunning the make install.