VideoCapture Does Not Work in Anaconda

会有一股神秘感。 提交于 2020-01-09 09:53:26

问题


I am using ubuntu 14.04, and have anaconda python installed. I used conda install opencv and conda install cv2 to install opencv. However I am unable to use the VideoCapture at all (I need to process videos frames by frames). I need to use anaconda for the rest of the project.

Here is my code:

import cv2
import os
capture = cv2.VideoCapture('/home/Downloads/data/zfH2XdRcH14.mp4')
while not capture.isOpened():
    print 'noob'
while True:
    ret, frame = capture.read()
    cv2.imwrite('~/Downloads/data/pic.png',frame)
    cv2.imshow('Video', frame)
    count += 1
    print count

The code keeps printing noob. I have checked the location multiple times and it is correct. I have no clue what the issue is and I have been stuck on this for hours.


回答1:


The solution is to compile ffmpeg with opencv. For opencv3, refer to https://github.com/menpo/conda-opencv3

For opencv2, refer to http://dhaneshr.net/2016/06/03/installing-opencv-2-4-x-with-ffmpeg-python-on-anaconda/




回答2:


ffmpeg is not present in default conda channel.

You need to download opencv from conda-forge channel which contains latest and additional packages and dependencies for video processing. Try the following:

conda install -c conda-forge ffmpeg
conda install -c conda-forge opencv

Here -c tells the channel to use as 'conda-forge'.




回答3:


I ran into the same problem. VideoCapture doesn't work with Conda's default version of OpenCV because ffmpeg is not enabled. In order for VideoCapture to work, you have to enable ffmpeg in the Cmake GUI and compile. You can also install my version of OpenCV which has ffmpeg enabled:

conda install -c https://conda.binstar.org/jaimeivancervantes opencv




回答4:


I believe I had the same problem. I fixed it by adding lib folder to the PATH. For example,

export PATH="/home/iori/anaconda3/bin:$PATH"
export PATH="/home/iori/anaconda3/lib:$PATH"

My .bachrc now has this 2nd line. The first line is added by anaconda and source activate command switches this bin folder but I think it does not take care of lib folder which I found annoying because that means opencv cannot find the lib_opencv_*.so files in there and of course cv2.VideoCapture fails.

The above example will fix the problem for the default conda env. For other envs, I still need to manually add lib folder to the PATH. So, I want to know how to customize source activate command to do this automatically for me...




回答5:


I had the same problem and after look on the internet I found a solution that worked. In the command line with administrator access:

conda install conda-build
conda install cmake
conda config --add channels menpo

Edit the following file:

C:\Program Files\Anaconda2\pkgs\cmake-3.6.3-vc9_0\info\recipe\buil.sh

addign the following flag:

-DWITH_FFMPEG = 1

For instance, in my case:

#!/bin/bash

LDFLAGS=$LDFLAGS" -Wl,-rpath,$PREFIX/lib" \
  ./bootstrap \
             --verbose \
             --prefix="${PREFIX}" \
             --system-libs \
             --no-qt-gui \
             --no-system-libarchive \
             --no-system-jsoncpp \
             -- \
             -DWITH_FFMPEG = 1 \
             -DCMAKE_BUILD_TYPE:STRING=Release \
             -DCMAKE_FIND_ROOT_PATH="${PREFIX}"

make
make install

Finally:

conda build /conda

It worked to me.

On the other hand I had previously copied opencv_ffmpegxyz.dll file from \opencv\build\bin to \Program Files\Anaconda2; in my case opencv_ffmpe320_64.dll (64 bit version) and I added a new environment variable called ffmpeg with the path where opencv_ffmpeg.dll files are placed.

Regards.




回答6:


Use conda-recipes to install ffmpeg.

git clone https://github.com/conda/conda-recipes.git

cd conda-recipes

conda build x264

conda build ffmpeg

See also here.




回答7:


I found another way to get the video writer to work. I actually installed ImageMagik on my computer, and then I set the animation path to where I installed ImageMagik. I wonder if the same could be done for ffmpeg?

plt.rcParams["animation.convert_path"] = "C:\ProgramFiles\ImageMagick\magick.exe"
#Here I am loading a matplotlib.plot as the animation, so it could be different than for you, but the point is that by specifying the path to imagemagik I was able to write a video to a file.
anim = animation.FuncAnimation(fig, animate, frames=len(mylist), init_func=init, interval=300, blit=True)
anim.save('output.gif', dpi=80, writer='imagemagik')


来源:https://stackoverflow.com/questions/29249611/videocapture-does-not-work-in-anaconda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!