9

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.

user3892614
  • 95
  • 1
  • 1
  • 5
  • I am not confident about how python works but I think you should add "//" in your path string. – Rahul galgali Mar 25 '15 at 07:24
  • 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 http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv. – Rahul galgali Mar 25 '15 at 07:32
  • 1
    I finally used `scikit-video` instead :) – SomethingSomething Mar 23 '17 at 15:03
  • 1
    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 Feb 26 '18 at 18:33
  • yes pip install opencv-python worked for me also. – Lanka Nov 13 '18 at 10:45

7 Answers7

5

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

Renil Joseph
  • 163
  • 3
  • 11
4

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/

Hong
  • 526
  • 6
  • 21
  • UPDATE 2018 : 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 Mar 14 '18 at 10:26
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

cod3monk3y
  • 9,508
  • 6
  • 39
  • 54
Jaime Ivan Cervantes
  • 3,579
  • 1
  • 40
  • 38
  • I tried the jaimeivancervantes version listed above. I found that it didn't work well in the PyCharm IDE. It would give me an error saying it needed to be recompiled with GTK+ gui libraries whenever I tried to use cv2.imshow(). I'm currently using Ubuntu 14.04, anaconda 2.7, PyCharm 5.0. The https://conda.binstar.org/menpo version of opencv works in PyCharm for images, but also seems to not work with ffmpeg... I'm now looking for a version that both is compiled with ffmpeg and works in the PyCharm IDE. – Phil Glau Dec 14 '15 at 05:52
  • 1
    @PhilGlau you can could try to compile it yourself, it's really not that difficult. You just have to open cmake and make sure that `ffmpeg` is enabled. It took me less than an hour. – Jaime Ivan Cervantes Dec 14 '15 at 12:15
  • @Jaime Ivan Cervantes, Hi, my `conda list | grep opencv` shows `opencv 2.4.10 np110py27_1 `. I understand anaconda2's opencv is just a python wrapper for my CentOS's opencv. Do you mean I have to recompile my CentOS opencv with ffmpeg enabled? – Chan Kim Aug 31 '16 at 14:10
  • I'm now trying to recompile Opencv2.4.10 with FFMPEG enabled. I added -D WITH_FFMPEG in cmake command but it doesn't understand.. How can I proceed? – Chan Kim Aug 31 '16 at 14:20
1

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.

shahar_m
  • 3,461
  • 5
  • 41
  • 61
0

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

otterb
  • 2,660
  • 2
  • 29
  • 48
  • 1
    You're probably only partially right. Working with `VideoCapture` requires another dependency that is not built by default, which is `ffmpeg`, a library that enables video reading and writing. Even in Windows, one should manually copy the `opencv_ffmpeg.dll` that is supplied together with the source files into Python dir or into another dir included in $PATH. An equivalent `libopencv_ffmpeg.so` is not supplied for Linux and one has to build whe whole OpenCV from scratch with FFMpeg enabled. This was not done for the Anaconda OpenCV package for Python. Hence `VideoCapture` fails – SomethingSomething Dec 27 '16 at 16:48
  • True. But you can go around it by binstar package (menpo for opencv and opencv3). http://stackoverflow.com/a/27650299/566035 – otterb Jan 03 '17 at 09:51
  • @SomethingSomething, your explain totally makes sense. Any sugguestion on links to fix the problem in anaconda? I tried a few links but they don't work unfortunately. – Hong Mar 23 '17 at 14:36
  • I finally used `scikit-video` instead :) – SomethingSomething Mar 23 '17 at 15:03
0

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.

omotto
  • 1,721
  • 19
  • 20
  • why I can't find opencv_ffmpeg*.dll file in my opencv package. I'm using anaconda. – Hong Mar 22 '17 at 20:29
  • I had downloaded opencv previously and unzipped/installed it in \opencv\ folder. In \opencv\build\bin folder I had both files: opencv_ffmpeg320*.dll – omotto Mar 23 '17 at 08:14
  • 1
    Of course a .DLL file is on Windows. Follow this link for Ubuntu [Compile opencv with ffmpeg for Ubuntu/Debian](http://blog.mycodesite.com/compile-opencv-with-ffmpeg-for-ubuntudebian/) – omotto Mar 23 '17 at 14:12
0

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')
bart cubrich
  • 1,184
  • 1
  • 14
  • 41