anaconda 安装opencv之后:出现 opencv error: (-2:Unspecified error) The function is not implemented. ...... libgtk2.0-dev and pkg-config, ..........之类的问题
背景:anaconda + python + opencv 学习,conda使用默认的channel,安装opencv后没法打开摄像头,公司里的电脑是可以打开摄像头并打开视频文件的。所以我就:
conda remove opencv
conda upgrade --all
apt-get install libgtk2.0-dev
(直接conda remove opencv 再 conda install opencv 发现仍找不到libgtk,
怀疑是conda含有cache,并没有重新编译opencv,所以这里换源下载重新编译)
conda install --channel https://conda.anaconda.org/menpo opencv3
完成后发现摄像头可以正常工作了,但是读取视频的时候 cap = cv2.VideoCapture("./testvideo/01.BigCountry.avi") 总是返回false
所以还是选择从源码安装 opencv,下载 opencv-3.4.8.tar.gz 和 opencv_contrib-3.4.8.tar.gz (或者github下载后checkout -b),然后:
0> 根据情况安装依赖
# Build tools:
sudo apt-get install -y build-essential cmake
# GUI (if you want to use GTK instead of Qt, replace 'qt5-default' with 'libgtkglext1-dev' and remove '-DWITH_QT=ON' option in CMake):
sudo apt-get install -y qt5-default libvtk6-dev
# Media I/O:
sudo apt-get install -y zlib1g-dev libjpeg-dev libwebp-dev libpng-dev libtiff5-dev libjasper-dev libopenexr-dev libgdal-dev
# Video I/O:
sudo apt-get install -y libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libv4l-dev libxine2-dev
# Parallelism and linear algebra libraries:
sudo apt-get install -y libtbb-dev libeigen3-dev
# Python: sudo apt-get install -y python-dev python-tk python-numpy python3-dev python3-tk python3-numpy
1> 参考 https://docs.opencv.org/3.4.2/d7/d9f/tutorial_linux_install.html
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/opt/opencv/ .. ,发现log中显示并没有发现ffmpeg相关的东西。(opencv通过 pkg-config 检测包的安装状态 。pkg-config --list-all 显示所有可以检测的包。
所以过程中我不断查看cmake之后的log,检查各种包的安装状态)
2> 参考 https://blog.csdn.net/luotuo44/article/details/24836901/ 和 https://blog.csdn.net/yangzhenzhen/article/details/62417021
vim ~/.bashrc 添加 export PKG_CONFIG_PATH=/opt/ffmpeg/lib/pkgconfig:$PKG_CONFIG_PATH
这样 opencv就可以检测到ffmpeg了
3> 然后还 sudo apt-get install libv4l-dev (这样 pkg-config --cflags --libs libv4l2 就显示了)
4> 更改python版本,不然opencv编译出来的python接口可能是for python2.7的 (https://blog.csdn.net/qq_36275734/article/details/81158966)
-D BUILD_opencv_python3=yes -D BUILD_opencv_python2=no -D PYTHON3_EXECUTABLE=/home/fang/anaconda3/envs/fang/bin/python3.6m -D PYTHON3_INCLUDE_DIR=/home/fang/anaconda3/envs/fang/include/python3.6m -D PYTHON3_LIBRARY=/home/fang/anaconda3/envs/fang/lib/libpython3.6m.so PYTHON3_NUMPY_INCLUDE_DIRS=/home/fang/anaconda3/envs/fang/lib/python3.6/site-packages/numpy/core/include -D PYTHON3_PACKAGES_PATH=/home/fang/anaconda3/envs/fang/lib/python3.6/site-packages -D PYTHON_DEFAULT_EXECUTABLE=/home/fang/anaconda3/envs/fang/bin/python3.6m
5> 其他一些cmake选项解释
WITH_CUDA=ON #cuda不会默认安装,需要的话要加上,只要 pkg-config --list-all |grep cuda 正常显示
WITH_OPENGL=ON
WITH_LIBV4L=ON
WITH_V4L=ON
WITH_QT=OFF #貌似QT和gtk同时只可以开一个,同时使能则使用第一个作为gui
WITH_GTK=ON #貌似QT和gtk同时只可以开一个,同时使能则使用第一个作为gui
WITH_FFMPEG=ON #opencv会默认安装ffmpeg的,只要 avcodec 等包 出现在 pkg-config --list-all 中
OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.8/modules #最好作为最后一个-D 不然其后的-D 会作为 编译 opencv_contrib-3.4.8 的选项
6> 最终cmake命令:
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/opt/opencv/ -D BUILD_opencv_python3=yes -D BUILD_opencv_python2=no -D PYTHON3_EXECUTABLE=/home/fang/anaconda3/envs/fang/bin/python3.6m -D PYTHON3_INCLUDE_DIR=/home/fang/anaconda3/envs/fang/include/python3.6m -D PYTHON3_LIBRARY=/home/fang/anaconda3/envs/fang/lib/libpython3.6m.so PYTHON3_NUMPY_INCLUDE_DIRS=/home/fang/anaconda3/envs/fang/lib/python3.6/site-packages/numpy/core/include -D PYTHON3_PACKAGES_PATH=/home/fang/anaconda3/envs/fang/lib/python3.6/site-packages -D PYTHON_DEFAULT_EXECUTABLE=/home/fang/anaconda3/envs/fang/bin/python3.6m -D WITH_FFMPEG=ON -D WITH_CUDA=ON -D WITH_OPENGL=ON -D WITH_LIBV4L=ON -D WITH_V4L=ON -D WITH_QT=OFF -D WITH_GTK=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.8/modules ..
或者
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/opt/opencv/ -D BUILD_opencv_python3=yes -D BUILD_opencv_python2=no -D PYTHON3_EXECUTABLE=/home/fang/anaconda3/envs/pyth36/bin/python3.6m -D PYTHON3_INCLUDE_DIR=/home/fang/anaconda3/envs/pyth36/include/python3.6m -D PYTHON3_LIBRARY=/home/fang/anaconda3/envs/pyth36/lib/libpython3.6m.so PYTHON3_NUMPY_INCLUDE_DIRS=/home/fang/anaconda3/envs/pyth36/lib/python3.6/site-packages/numpy/core/include -D PYTHON3_PACKAGES_PATH=/home/fang/anaconda3/envs/pyth36/lib/python3.6/site-packages -D PYTHON_DEFAULT_EXECUTABLE=/home/fang/anaconda3/envs/pyth36/bin/python3.6m -D WITH_FFMPEG=ON -D WITH_CUDA=OFF -D WITH_OPENGL=ON -D WITH_LIBV4L=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_GTK=OFF -D BUILD_opencv_world=ON -D WITH_IPP=OFF -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.10/modules ..
(这是后来重装系统后使用的配置,最好conda deactivate 后cmake。使用了3.4.10版本,使用qt而不是gtk,-D BUILD_opencv_world=ON 将所有opencv的库集合成一个库 libopencv_world.so,方便集成。-D WITH_IPP=OFF intel的优化指令 cmake的时候会下载 ippicv_linux_20151201.tgz 速度很慢 如果性能够用可以禁止
http://cn.voidcc.com/question/p-mnpazqat-cc.html (其实很多时候这里的下载是timeout失败的) )
make && sudo make install
#vim ~/.bashrc
export PATH=$PATH:/opt/opencv/bin
export PKG_CONFIG_PATH=/opt/opencv/lib/pkgconfig:$PKG_CONFIG_PATH
export LD_LIBRARY_PATH=/opt/opencv/lib/:$LD_LIBRARY_PATH
编译时候遇到的error
下载各种模型失败,可以将别人下载好 的build目录下的downloads/目录拷贝到build下,然后删掉build目录下的其他所有文件,重新cmake && make
//home/a401/anaconda3/lib/libtiff.so.5: undefined reference to ZSTD_freeCStream' //home/a401/anaconda3/lib/libtiff.so.5: undefined reference toZSTD_maxCLevel
(解决办法 https://blog.csdn.net/Splend520/article/details/97270438)去掉bashrc中关于anaconda的环境变量。我没有去掉,直接conda deactivate 然后重新cmake也可
/home/fang/codes/test/opencv-3.4.8/modules/cudacodec/src/cuvid_video_source.cpp:44:0:
/home/fang/codes/test/opencv-3.4.8/modules/cudacodec/src/precomp.hpp:60:37: fatal error: dynlink_nvcuvid.h: 没有那个文件或目录
(解决办法 https://blog.csdn.net/u010349092/article/details/97926969)下载 Video_Codec_SDK_11.0.10 将其中 Interface 下的 两个头文件添加到/usr/local/include
cp cv2.cpython-36m-x86_64-linux-gnu.so /home/fang/anaconda3/envs/fang/lib/python3.6/site-packages/
这个是OpenCV基础库的python封装。基础库本身因为运行了sudo make install已经被安装到系统(需要最后 vim ~/.bashrc 将 /opt/opencv/lib/pkgconfig 添加到 PKG_CONFIG_PATH , 将 /opt/opencv/lib 添加到 LD_LIBRARY_PATH ,将 /opt/opencv/bin 添加到 PATH,添加 /etc/ld.so.conf.d/opencv.conf ,内容 /opt/opencv/lib)
测试:
#激活anaconda的环境
conda activate xxx
#启动Python
python
然后运行下面两行测试
import cv2
cv2.__version__
完成~~
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.4.1/modules \
-D PYTHON_EXCUTABLE=$(which python) \
-D WITH_CUDA=ON \ # 使用CUDA https://stackoverflow.com/questions/28010399/build-opencv-with-cuda-support
-D WITH_CUBLAS=ON \
-D DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" \
-D CUDA_ARCH_BIN="5.3" \ # 需要去官网确认使用的GPU所对应的compute capabilities https://developer.nvidia.com/cuda-gpus https://forum.ubuntu.org.cn/viewtopic.php?p=3153672
-D CUDA_ARCH_PTX="" \
-D CUDA_FAST_MATH=ON \ # 计算速度更快但是相对不精确
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \ # 如果qt未安装可以删去此行;若因为未正确安装qt导致的Qt5Gui报错,可将build内文件全部删除后重新cmake,具体可以参考(http://stackoverflow.com/questions/17420739/opencv-2-4-5-and-qt5-error-s)
-D WITH_GTK=ON \
-D WITH_FFMPEG=ON \
-D WITH_OPENGL=ON \
-D BUILD_EXAMPLES=OFF ..)