Building OpenCV 4.5.4

By now there should be a base ZM install operating, Nvidia drivers and Cuda with cuDNN support setup and working. The next steps are to set up the build environment for OpenCV 4.5, DLib and ALPR. During face_recognition install is when DLib is built. We will start with OpenCV 4.5 and move onto DLib then finish with ALPR. There is an excellent guide that the original docs point towards and I will be paraphrasing most of it during this example. Here is the article that the original docs point you towards to build OpenCV with cuDNN.

sudo apt-get updatesudo apt-get upgradesudo apt install libopenblas64-dev libopenblas-dev build-essential git curlsudo apt-get install libjpeg-dev libpng-dev libtiff-devsudo apt-get install libavcodec-dev libavformat-dev libswscale-devsudo apt-get install libv4l-dev libxvidcore-dev libx264-devsudo apt-get install libgtk-3-devsudo apt-get install libatlas-base-dev gfortransudo apt-get install python2-devsudo pip3 install numpy

Here is where you can create a python virtualenv if you desire to keep this env separate. That is out of scope for this example.

The next thing you are going to want to do is figure out the Compute Capability (CC) of the GPU you will be using for detections. This is very important, if you compile for the wrong CC you won’t know until you go to do your first detection. You will then have to come back, rm -rf the build/ folder and recompile with the correct CC. So make sure you get the correct one.

If you have multiple GPUs you can compile for multiple CC’s but each CC added is effectively doubling the compile time of OpenCV, you’ve been warned.

https://developer.nvidia.com/cuda-gpus#compute is the page to look up CC. If you have a GTX card go to the GeForce and TITAN Products section.

If your card isn’t listed look for the series below it, for example, a 1660ti isn’t listed. The 1650ti is though, and that is the 1660ti’s CC (7.5).

Next, we need the OpenCV .zip source files and to clone the opencv_contrib repo. The source .zip is available from the OpenCV GitHub repo under the ‘Releases’ section. October 2021 has 4.5.4 as the current release.

wget https://github.com/opencv/opencv/archive/refs/tags/4.5.4.zip--2021-10-19 20:55:44--  https://github.com/opencv/opencv/archive/refs/tags/4.5.4.zipResolving github.com (github.com)... 140.82.112.3Connecting to github.com (github.com)|140.82.112.3|:443... connected.HTTP request sent, awaiting response... 302 FoundLocation: https://codeload.github.com/opencv/opencv/zip/refs/tags/4.5.4 [following]--2021-10-19 20:55:45--  https://codeload.github.com/opencv/opencv/zip/refs/tags/4.5.4Resolving codeload.github.com (codeload.github.com)... 140.82.113.9Connecting to codeload.github.com (codeload.github.com)|140.82.113.9|:443... connected.HTTP request sent, awaiting response... 200 OKLength: unspecified [application/zip]Saving to: '4.5.4.zip'4.5.4.zip                                                       [                          <=>                                                                                                             ]  89.73M  17.8MB/s    in 7.1s2021-10-19 20:55:52 (12.6 MB/s) - '4.5.4.zip' saved [94094204]# Clone the opencv_contrib repo
git clone https://github.com/opencv/opencv_contrib.git

There should now be a 4.5.4.zip file and a folder named opencv_contrib. Unzip the file and create a build folder inside the unzipped directory.

unzip 4.5.4.zip
cd opencv-4.5.4
mkdir build && cd build

Now we start configuring the build.

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D CUDA_ARCH_BIN=7.5 \
-D WITH_CUBLAS=1 \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D HAVE_opencv_python3=ON \
-D PYTHON_EXECUTABLE=/usr/bin/python3 \
-D PYTHON2_EXECUTABLE=/usr/bin/python2 \
-D BUILD_EXAMPLES=ON ..
# Change the CUDA_ARCH_BIN=7.5 to whatever your CC is# Otherwise this error will occur on your first detection ->cv2.error: OpenCV(4.5.4) /home/baudneo/opencv-4.5.4/modules/dnn/src/cuda/execution.hpp:52: error: (-217:Gpu API call) invalid device function in function 'make_policy'# Change the PYTHON_EXECUTABLE to where you have your venv python3 executable, if you are not using venv put the python3 executable (usually /usr/bin/python3). Same for PYTHON2_EXECUTABLE

Now you can check the output of CMake to double-check that cuDNN and Cuda are being used.

OpenCV modules:--     To be built:                 aruco barcode bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto# Notice all the cuda keywords!--   NVIDIA CUDA:                   YES (ver 10.2, CUFFT CUBLAS FAST_MATH)--     NVIDIA GPU arch:             75--     NVIDIA PTX archs:----   cuDNN:                         YES (ver 8.2.1)# Also check that the python3 interpreter and numpy paths point to where they should (venv if configured that way)
Python 3:
-- Interpreter: /usr/bin/python3 (ver 3.9.5)-- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.9.so (ver 3.9.5)-- numpy: /home/baudneo/.local/lib/python3.9/site-packages/numpy/core/include (ver 1.21.2)-- install path: lib/python3.9/dist-packages/cv2/python-3.9

As you can see there is Cuda 10.2 with cuDNN 8.2.1. Using GPU arch 75 (CC: 7.5) and there is CUBLAS and FAST_MATH. Python3 and NumPy point to the proper paths. This is what we want!

Next, we need to compile and that takes resources, when using the make command you can use the -j <x> flag to use <x> amount of cores. I suggest leaving 1 or 2 free and letting make use the rest. Compiling takes a while depending on your hardware. I am using a dual-socket Xeon with 19/24 cores and 32 GB of DDR3 @ 800Mhz to compile OpenCV. The system is being used to host ZM and several other LXC containers while I am compiling so my build times may not be accurate to an idle system. This test ZM/neo-ZMES/neo-MLAPI host is in a privileged LXC.

make -j 19
# Now it will compile and provide some output. Be patient! It takes my system roughly 15-20 mins or so
[100%] Built target opencv_python3# Install it
sudo make install -j 19
# Now check that the interpreter can import OpenCV by jumping into a python3 shell and importing cv2 to check its version.python3
import cv2
cv2.__version__
exit() or Ctrl+D
baudneo@ZMES-test:~/opencv-4.5.4/build$ python3Python 3.9.5 (default, May 11 2021, 08:20:37)[GCC 10.3.0] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import cv2>>> cv2.__version__'4.5.4'>>> exit()

--

--