使用ffmpeg和python 2.7在Fedora 26 25 24 23或22上安装OpenCV 2.4.13或3.x时,VideoCapture无法正常工作 [英] VideoCapture not working error in Installing OpenCV 2.4.13 or 3.x on Fedora 26 25 24 23 or 22 with ffmpeg and python 2.7
问题描述
当我使用yum时,它将安装opencv 3.2.0.但是我特别需要opencv 2.4.13.
When I use yum, it installs opencv 3.2.0. But I specifically need opencv 2.4.13.
对于Anaconda,有一个conc-forge opencv软件包,但是安装cv2.VideoCapture()api失败.
With Anaconda, there is a conda-forge package of opencv but installing that cv2.VideoCapture() api fails.
此外,当我尝试通过opencv 2.4.13的tar文件进行安装时,我陷入了cmake,并遇到了以下错误-
Also when I am trying to install through tar file of opencv 2.4.13, I am stuck in cmake, getting below errors -
sys/videoio.h
找不到libavcodec
........
cmake还给出了奇怪的脚本错误. 请帮忙.
Also cmake is giving bizzare scripting errors. Please help.
推荐答案
Anaconda没有使用ffmpeg编译的opencv 2.4.13,因此,如果要使用VideoCapture,则需要编译opencv. 我遵循的说明性步骤列表(仅适用于fedora用户):
Anaconda does not have opencv 2.4.13 compiled with ffmpeg, so if you want to use VideoCapture then opencv needs to be compiled. Descriptive list of steps I followed (strictly for fedora users):
删除以前的opencv
sudo yum remove opencv
安装Python 2.7
sudo yum install python2.7
*请使用您的python 2.7软件包名称.
*Please use your python 2.7 package name.
安装ffmpeg
从他们的网站下载tarball并将其解压缩. https://www.ffmpeg.org/download.html
Download tarball from their site and extract it. https://www.ffmpeg.org/download.html
tar -xvf ffmpeg-3.3.0.tar.gz
cd ffmpeg-3.3.0
./configure --prefix=/usr/local --enable-gpl --enable-swscale --enable-shared --enable-postproc --enable-avfilter-lavf
*删除选项--enable-avfilter-lavf(如果出现错误).
*remove option --enable-avfilter-lavf if it gives error.
sudo make
sudo make install
安装OpenCV 2.4.13
以下软件包将解决sys/videoio.h错误:
Below packages will solve sys/videoio.h error:
sudo dnf install pjproject-devel.x86_64 2.4.5-8.fc26 ptlib-devel.x86_64 2.10.10-16.fc24 pjproject.x86_64 2.4.5-8.fc26 ptlib.x86_64 2.10.10-16.fc24
然后安装以下软件包:
sudo yum install libpng-devel libjpeg-turbo-devel jasper-devel openexr-devel libtiff-devel libwebp-devel libdc1394-devel libv4l-devel gstreamer-plugins-base-devel gtk2-devel tbb-devel eigen3-devel gstreamer1-libav gstreamer1-plugins-base-devel java-1.8.0-openjdk-devel.x86_64 python2-numpy.x86_64 ffmpeg-devel.x86_64 ffmpeg-libs.i686 ffmpeg.x86_64 libavdevice.i686 libpng-devel libjpeg-turbo-devel jasper-devel openexr-devel libtiff-devel libwebp-devel libdc1394-devel libv4l-devel gstreamer-plugins-base-devel gtk2-devel tbb-devel eigen3-devel gstreamer1-libav gstreamer1-plugins-base-devel gtk+extra-devel.x86_64 gtk+-devel.i686 cmake pkg-config libgtk libavcodec libavformat libswscale swig
下载您选择的OpenCV,例如来自github的2.4.13(从分支"->标签"->"2.4.13"中选择2.4.13,然后选择从克隆中下载):
Download OpenCV of your choice e.g. 2.4.13 from github (select 2.4.13 from Branch -> Tags -> 2.4.13 then chose download from clone):
https://github.com/opencv/opencv
http://opencv.org/releases.html
提取焦油:
tar -xvf opencv-2.4.13.3.tar.gz
cd opencv-2.4.13.3
mkdir build; cd build;
现在 cmake :
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_FFMPEG=1 -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_QT=OFF -D WITH_V4L=OFF -D CMAKE_SHARED_LINKER_FLAGS=-Wl,-Bsymbolic -DLD_LIBRARY_PATH=/usr/local/lib/ -DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -DPYTHON_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))") -DCUDA_NVCC_FLAGS="-ccbin /home/rishabh/gcc-4.9.4-x86_64/bin/gcc" -D WITH_CUDA=OFF -D BUILD_opencv_gpu=OFF -D BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=OFF -D BUILD_opencv_gpucodec=OFF -D BUILD_opencv_gpufeatures2d=OFF -D BUILD_opencv_gpufilters=OFF -D BUILD_opencv_gpuimgproc=OFF -D BUILD_opencv_gpulegacy=OFF -D BUILD_opencv_gpuoptflow=OFF -D BUILD_opencv_gpustereo=OFF -D BUILD_opencv_gpuwarping=OFF -D WITH_OPENCL=OFF ..
说明:
包含"gpu"文本的标记用于关闭gpu编译,如果要使用gpu进行编译,则将其设置为ON.但是,gpu将需要cuda软件包和图形卡才能工作.
Flags that include 'gpu' text are for turning off gpu compilation, if you want to compile with gpu then set them ON. But gpu will require cuda packages and a graphics card to work.
由于我们不使用gpu进行编译,因此也可以忽略此标志,但是保留它也不会损害我.
This flag can also be ignored since we are not compiling with gpu, but it also didn't hurt me keeping it.
-DCUDA_NVCC_FLAGS =-ccbin/home/rishabh/gcc-4.9.4-x86_64/bin/gcc"
-DCUDA_NVCC_FLAGS="-ccbin /home/rishabh/gcc-4.9.4-x86_64/bin/gcc"
如果您在cmake中遇到脚本错误,请执行以下操作-
打开文件opencv-2.4.13.3/cmake/OpenCVPackaging.cmake
open file opencv-2.4.13.3/cmake/OpenCVPackaging.cmake
-添加此
set(OPENCV_VCSVERSION "2.4.13.3")
-在此行上方
set(CPACK_PACKAGE_VERSION "${OPENCV_VCSVERSION}")
注释掉阻塞cmake的无用的行,无论在控制台上丢掉什么文件名.
Comment out respective lines that are blocking cmake and are useless, in whatever filename is thrown out on console.
例如opencv-2.4.13.3/cmake/OpenCVDetectCXXCompiler.cmake
e.g. opencv-2.4.13.3/cmake/OpenCVDetectCXXCompiler.cmake
我对80-86行有问题,所以我将其注释掉,因为这些变量在其他地方没有做任何其他事情.
I had problem with line 80-86 so I commented it out because those variables were not doing anything else elsewhere.
cmake成功后,继续执行步骤-
sudo make
sudo make install
如果遇到任何链接器错误,那么如果您不打算安装这些模块,则必须安装各自的依赖项或将cmake标志设置为OFF.
If you are facing any linker errors then you will have to install respective dependencies or set cmake flag to OFF if you do not intend to install those modules.
安装后步骤:
echo 'export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc
现在添加
/usr/local/lib/
到以下文件:
/etc/ld.so.conf
/etc/ld.so.conf.d/opencv.conf
现在运行
sudo ldconfig
对于Anaconda PATH变量冲突
从PATH变量中删除anaconda路径,并在〜/.bashrc中进行编辑
remove anaconda path from PATH variable and edit in ~/.bashrc also
export PATH =/usr/lib64/qt-3.3/bin:/usr/lib64/ccache:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/opt /cuda/bin:/home/rishabh/.local/bin:/home/rishabh/bin:/opt/cuda/bin/
export PATH=/usr/lib64/qt-3.3/bin:/usr/lib64/ccache:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/opt/cuda/bin:/home/rishabh/.local/bin:/home/rishabh/bin:/opt/cuda/bin/
现在注销并登录到fedora .
执行以下测试:
python
import cv2
cv2.__version__
video = cv2.VideoCapture("your small video.mp4")
okay, frame = video.read()
print("VideoCapture Read Success - ")
print(okay)
输出-
真实-视频读取成功
错误-opencv尚未使用ffmpeg正确编译.
False - opencv yet not compiled properly with ffmpeg.
这篇关于使用ffmpeg和python 2.7在Fedora 26 25 24 23或22上安装OpenCV 2.4.13或3.x时,VideoCapture无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!