9
votes

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.

7
I am not confident about how python works but I think you should add "//" in your path string.Rahul galgali
Add C:\OpenCV\3rdparty\ffmpeg\ to the Windows PATH environment variable or copy opencv_ffmpeg.dll from that directory to C:\Python27\ or to a directory that is in the PATH. Alternatively, use the OpenCV binaries from lfd.uci.edu/~gohlke/pythonlibs/#opencv.Rahul galgali
I finally used scikit-video instead :)SomethingSomething
UPDATE : opencv-python 3.4.0.12 has supported video related functions, so we can use pip install opencv-python as normal. . Just "pip install opencv-python" and it worked :)Dang Manh Truong
yes pip install opencv-python worked for me also.Lanka

7 Answers

5
votes

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 which channel to use. In our case we need 'conda-forge'.

4
votes

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/

3
votes

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

1
votes

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.

0
votes

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

0
votes

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.

0
votes

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