VideoCapture not working error in Installing OpenC

2019-08-24 07:42发布

问题:

When I use yum, it installs opencv 3.2.0. But I specifically need opencv 2.4.13.

With Anaconda, there is a conda-forge package of opencv but installing that cv2.VideoCapture() api fails.

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 not found

libavcodec not found

........

Also cmake is giving bizzare scripting errors. Please help.

回答1:

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):

Remove previous opencv

sudo yum remove opencv 

Install Python 2.7

sudo yum install python2.7

*Please use your python 2.7 package name.

Installation of ffmpeg

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

*remove option --enable-avfilter-lavf if it gives error.

sudo make
sudo make install  

Installation of OpenCV 2.4.13

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

Then install these packages:

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

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

Extract tar:

tar -xvf opencv-2.4.13.3.tar.gz
cd opencv-2.4.13.3
mkdir build; cd build;

Now 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 ..

Explanation :

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.

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"

If you are facing script errors in cmake then do these-

open file opencv-2.4.13.3/cmake/OpenCVPackaging.cmake

-- add this

set(OPENCV_VCSVERSION "2.4.13.3")

-- above this line

set(CPACK_PACKAGE_VERSION "${OPENCV_VCSVERSION}")

Comment out respective lines that are blocking cmake and are useless, in whatever filename is thrown out on console.

e.g. opencv-2.4.13.3/cmake/OpenCVDetectCXXCompiler.cmake

I had problem with line 80-86 so I commented it out because those variables were not doing anything else elsewhere.

After cmake is successful, proceed to make step -

sudo make 
sudo make install

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.

Post installation steps:

echo 'export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH' >> ~/.bashrc 
source ~/.bashrc

Now add

/usr/local/lib/

to following files:

/etc/ld.so.conf
/etc/ld.so.conf.d/opencv.conf

Now run

sudo ldconfig

For Anaconda PATH variable Conflict

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/

Now logout and login to fedora.

Execute below for Testing:

python
import cv2
cv2.__version__
video = cv2.VideoCapture("your small video.mp4")
okay, frame = video.read()
print("VideoCapture Read Success - ")
print(okay)

Output-

True - video read success

False - opencv yet not compiled properly with ffmpeg.