简体   繁体   中英

Import CV2 in python give undefined symbol error

I have installed opencv 3.4.0 with both python2.7 and python3.5 bindings. I am able to import and use opencv successfully in python2 but not python3. I get the following error:

ImportError: /usr/local/lib/libopencv_text.so.3.4: undefined symbol: _ZNK2cv3dnn19experimental_dnn_v33Net14getLayerShapesERKSt6vectorIiSaIiEEiRS3_IS5_SaIS5_EESA

This worked transiently until I had to upgrade CUDNN for CUDA compatibility purposes. I have cleaned, uninstalled and reinstalled opencv but this issue is not resolved.

My additional setup is as follows: Ubuntu 16.04 cuDNN 7.6.1 (I have tried with 7.6.3 as well) CUDA 10.0

I am limited to keeping the above versions because of other dependencies.

Thanks in advance.

A real linux user compile the software by itself :P

Clone opencv and contrib dependencies:

git clone https://github.com/opencv/opencv.git --depth=1
git clone --depth=1 https://github.com/opencv/opencv_contrib.git

In order to reduce by a huge factor the compilation time (that can be more than 8h in the old environment), you can specify the architecture of the GPU. (here an example for compile opencv from a source related to the hardware only)

NOTE:
CUDA_GENERATION is related to your GPU ARCH.
Change path related to your python libs (replace /opt/XXXXXXXX)

> export JAVA_HOME=/opt/DEVOPS/SDK/jvm/jdk-11.0.4/ ; cmake3 .. -D \
INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D WITH_CUDA=ON \
-D WITH_GTK=ON -D WITH_OPENGL=ON -D ENABLE_CXX11=ON -D BUILD_EXAMPLES=ON \
 -D OPENCV_EXTRA_MODULES_PATH=/opt/SP/packages/opencv_contrib/modules -D \
BUILD_opencv_java=ON -D BUILD_opencv_python2=ON -D BUILD_opencv_python3=ON \ 
-D CUDA_FAST_MATH=ON -D CUDA_GENERATION="Maxwell" -D WITH_OPENMP=ON -D \ 
BUILD_TESTS=OFF -D OPENCV_ENABLE_NONFREE=ON -D BUILD_TIFF=ON -D WITH_OPENCL=ON \ 
-D WITH_IPP=ON -D WITH_TBB=ON -D WITH_EIGEN=ON -D WITH_V4L=ON -D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D \ 
CMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") \ 
-D PYTHON3_EXECUTABLE=$(which python3) -D PYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
 -D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
 -D PYTHON3_LIBRARY=/opt/DEVOPS/SDK/anaconda3/lib \ 
-D OPENCV_PYTHON3_INSTALL_PATH=/opt/DEVOPS/SDK/anaconda3/lib/python3.7/site-packages \
 -D PYTHON3_DEFAULT_EXECUTABLE=/opt/DEVOPS/SDK/anaconda3/bin/python \
 -D OPENCV_ENABLE_NONFREE=ON

you should download the ANACONDA python environment, and install opencv through pip using the terminal.

Enter the below command into a python terminal:

python -m pip install opencv-python

This should install the latest version of cv2.

Goodluck friend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM