OpenCV cmake can't find CUDA while building image for Docker - c++

I'm trying to build a docker image with the latest opencv version 4.1.0 to be able to use cudafeatures2D. I have cuda 9.0 installed but every time i try to compile opencv from source it says it doesn't find CUDA.
I'm using the build runtime image FROM nvidia/cuda:9.0-cudnn7-runtime. And i've checked in my current image the location of CUDA, /usr/local/cuda-9.0.
This is the code I'm using to install:
ENV PATH=$PATH:/usr/local/cuda/bin:$PATH
ENV LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64:$LD_LIBRARY_PATH
RUN ldconfig
RUN wget https://github.com/opencv/opencv/archive/4.1.0.tar.gz && tar xf 4.1.0.tar.gz && rm 4.1.0.tar.gz
RUN wget https://github.com/opencv/opencv_contrib/archive/4.1.0.tar.gz && tar xf 4.1.0.tar.gz && rm 4.1.0.tar.gz
RUN cd opencv-4.1.0/ && mkdir build && cd build && cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D WITH_CUBLAS=ON -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_OPENGL=ON -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=OFF -DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" -D PYTHON2_EXECUTABLE=/usr/bin/python2.7 -D PYTHON_INCLUDE_DIR=/usr/include/python2.7/ -D PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python2.7/ -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython2.7.so -D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/local/lib/python2.7/dist-packages/numpy/core/include/ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.0/modules/ -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-9.0 -D CUDA_CUDART_LIBRARY=/usr/local/cuda/lib64/libcudart.so -D CUDA_VERBOSE_BUILD=ON .. && make -j$(nproc) && make install
But I always get this error:
-- at: /app/opencv-3.4.6/build/3rdparty/ippicv/ippicv_lnx/iw
CMake Warning at cmake/OpenCVFindLibsPerf.cmake:35 (message):
OpenCV is not able to find/configure CUDA SDK (required by WITH_CUDA).
CUDA support will be disabled in OpenCV build.
To eliminate this warning remove WITH_CUDA=ON CMake configuration option.
Call Stack (most recent call first):
CMakeLists.txt:794 (include)

finally someone from the opencv repository pointed out that the problem I was having was due to the image I was using:
FROM nvidia/cuda:9.0-cudnn7-runtime
The runtime version usually doesn't contain the SDK files, the fix was to change -runtime for -develop.

Related

Link error when compiling opencv from source when including ffmpeg compiled from source

I've been stuck on this problem for weeks.
I'm trying to build opencv on a raspberry pi 4 with x264 support. To do this I need to include ffmpeg, and also build that from source.
However when compiling opencv, I'm consistently getting these linker errors:
/usr/bin/ld: ../../lib/libopencv_videoio.so.4.5.3: undefined reference to `avcodec_get_context_defaults3'
/usr/bin/ld: ../../lib/libopencv_videoio.so.4.5.3: undefined reference to `av_lockmgr_register'
/usr/bin/ld: ../../lib/libopencv_videoio.so.4.5.3: undefined reference to `av_register_all'
I'm new to Linux, so I'm not exactly sure how to get started troubleshooting this. I believe I have ffmpeg correctly compiled and installed.
Here's the steps I use:
Configure ffmpeg:
sudo ./configure \
--prefix=/usr \
--extra-ldflags="-latomic" \
--enable-shared \
--extra-libs='-lpthread -lm' \
--ld=g++ \
--enable-gpl \
--disable-debug \
--enable-nonfree \
--enable-libx264 \
--enable-omx \
--enable-omx-rpi \
--enable-gnutls \
--enable-libfreetype \
--enable-libmp3lame
Then build it:
sudo make -j4
Then install it as a package: (so opencv cmake will detect it)
sudo checkinstall -y --deldoc=yes --pkgversion=9999 --pkgname=ffmpeg
Then configure opencv (ffmpeg is detected)
sudo cmake ../opencv_sources -D CMAKE_BUILD_TYPE=RELEASE \
-D OPENCV_EXTRA_MODULES_PATH= $PWD/../opencv_contrib/modules \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D BUILD_TESTS=ON \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D CMAKE_SHARED_LINKER_FLAGS='-latomic -L/usr/lib' \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D BUILD_EXAMPLES=OFF \
-D CPU_BASELINE=NATIVE \
-D CMAKE_INSTALL_PREFIX="$HOME/opencv_build" \
-D BUILD_opencv_apps=OFF \
-D BUILD_opencv_python2=OFF \
-D BUILD_SHARED_LIBS=ON \
-D WITH_FFMPEG=ON
Then build opencv:
sudo make -j4
And this is where I get linking errors.
I checked in /usr/lib and the .so files appear to be there:
ls /usr/lib | grep libav
libavcodec.a
libavcodec.so
libavcodec.so.59
libavcodec.so.59.4.101
libavdevice.a
libavdevice.so
libavdevice.so.59
libavdevice.so.59.0.100
libavfilter.a
libavfilter.so
libavfilter.so.8
libavfilter.so.8.1.103
libavformat.a
libavformat.so
libavformat.so.59
libavformat.so.59.4.101
libavutil.a
libavutil.so
libavutil.so.57
libavutil.so.57.3.100
How do check where opencv is looking when linking? And is there a way I can check the shared libraries that ffmpeg generated to make sure they work?
The ffmpeg version is: git-2021-08-10-c245963
And opencv is 4.5.3-dev
TL;DR:
I was compiling the master branch of ffmpeg. This version removed some deprecated functions and was incompatible with my opencv version. Or at least the build tests failed. By switching to the release/4.4 branch and repeating my previous build steps I was able to successfully build opencv.
Details:
I was able to determine that it's not a linking problem. I wrote a short program that calls avcodec_get_context_defaults3(...), as well as avcodec_version(), which is another function that should be in there. Upon linking libavcodec.so, the avcodec_version() is found, but avcodec_get_context_defaults3() is not. Therefore I believe this is a version compatibility issue between ffmpeg and opencv.
I then confirmed that the function was removed from ffmpeg. The latest version does not have it, old versions do. I used git's search function and found this commit:
Author: Andreas Rheinhardt <andreas.rheinhardt#gmail.com>
Date: Thu Feb 25 20:37:24 2021 +0100
avcodec: Remove deprecated avcodec_get_context_defaults3
Deprecated in 04fc8e24a091ed1d77d7a3c0cbcfe60baec19a9f.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt#gmail.com>
Signed-off-by: James Almer <jamrial#gmail.com>
I switched to the release/3.4 branch of FFMPEG and repeated the steps. But that was missing some other things. I repeated the steps a 3rd time with release/4.4 and that worked.
The command I used to download the correct branch was this:
sudo git clone https://github.com/FFmpeg/FFmpeg --branch release/4.4 --depth 1 ffmpeg_sources

Cmake make install fails with INSTALL cannot find opencv_annotation

Problem:
I am installing OpenCV 3.1.0 with CUDA support in ubuntu 16.04 and for that I chose defined by myself destination folder instead of /usr/local .
cmake and make go on properly, however I get an error upon make install (sudo make install does not alleviate it, anyway I do not build into directory that requires root priviledges).
I don't know what I am doing wrong and which direction to go now. I basically wanted to replicate from this tutorial and I did not omit any step. Please help.
Information:
error:
CMake Error at apps/annotation/cmake_install.cmake:42 (file):
file INSTALL cannot find
"~/libraries/opencv-3.1.0/release/installed/bin/opencv_annotation".
Call Stack (most recent call first):
apps/cmake_install.cmake:39 (include)
cmake_install.cmake:84 (include)
Makefile:116: recipe for target 'install' failed
my cmake configuration:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=~/libraries/opencv-3.1.0/release/installed \
-D WITH_CUDA=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/libraries/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ~/libraries/opencv
opencv_contrib on branch 3.1.0, opencv on branch with cuda 8.0 support as suggested in here.
INSTALL_C_EXAMPLES=OFF as suggested in here.

opencv make error cmath ‘::cos’ has not been declared

I'm trying to install opencv on ubuntu. so far I have downloaded opencv 3.1 and done this:
mkdir build
cd build/
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_CUBLAS=ON -DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" ..
but when I try to make it, by using make it shows these kind of errors:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:63:16: error: expected constructor, destructor, or type conversion before ‘(’ token
__MATHCALL_VEC (cos,, (Mdouble __x));
or
/usr/include/c++/5/cmath:199:11: error: ‘::cos’ has not been declared
and so on. what can I do to fix this?

error in cmake opencv: Parse error in command line argument: -D

it is a while I am trying to install opencv but each time I have some problems with the configuration of cmake.
this is the cmake I am trying to use:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_PYTHON_SUPPORT=ON
-D WITH_XINE=ON -D WITH_OPENGL=ON -D INSTALL_C_EXAMPLES=ON
-D INSTALL_PYTHON_EXAMPLES=ON -D WITH_TBB=ON -D BUILD_EXAMPLES=ON
-D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON
-D CMAKE_INSTALL_PREFIX=/home/username/main-env/opencv3.0
-D WITH_GTK = ON -D MATLAB_ROOT_DIR=/home/MATLAB/R2015a ..
,but got this error
Parse error in command line argument: -D
Should be: VAR:type=value
CMake Error: No cmake script provided.
CMake Error: Problem processing arguments. Aborting.
P.S. I want to put it in main-env folder which is the virtual environment folder to avoid any problems with system files and less struggling with various errors encountered in previous times. not sure possible maybe it is totally wrong. please let me know if you have some knowledge about it.
the all terms should be separated by -D and shouldn't be any attached terms with -D

python HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP

I'm using python 2.7, Ubuntu 14.04 and cv2. I'm trying to run this simple code:
import cv2
cam = cv2.VideoCapture(0)
while True:
ret, frame = cam.read()
cv2.imshow("webcam", frame)
cv2.waitKey(1)
I want to take a single picture, but instead my webcam stays activated but it only returns this:
HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP
What I need to do to fix the problem?
Find your OpenCV directory in the the home folder and go there (something like the cd command I've written bellow, type your OpenCV directory there the code below is for my case!). Create a directory named Release and go inside the folder,
cd /home/user/OpenCV
mkdir Release
cd Release
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_GTK=ON -D WITH_OPENGL=ON ..
When running these commands on terminal please make sure you have permissions to those files or directories otherwise use sudo command to give them the access. After running the last one successfully type,
make
sudo make install