user3892614
user3892614

Reputation: 95

VideoCapture Does Not Work in Anaconda

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.

Upvotes: 9

Views: 21819

Answers (7)

Renil Joseph
Renil Joseph

Reputation: 163

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

Upvotes: 5

bart cubrich
bart cubrich

Reputation: 1254

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

Upvotes: 0

Dawn
Dawn

Reputation: 3628

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.

Upvotes: 1

Hong
Hong

Reputation: 546

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/

Upvotes: 4

omotto
omotto

Reputation: 1879

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.

Upvotes: 0

otterb
otterb

Reputation: 2710

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

Upvotes: 0

Jaime Ivan Cervantes
Jaime Ivan Cervantes

Reputation: 3697

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

Upvotes: 3

Related Questions