ContinuumIO/anaconda-issues

OpenCV video capture from file fails on Linux

aizvorski opened this issue ยท 66 comments

The following fails on Anaconda 1.9 on Linux only:

import cv2
import sys
cap = cv2.VideoCapture(sys.argv[1])

ret, frame = cap.read()
if not ret:
    print "capture failed"

Tested with Anaconda-1.9.2-Linux-x86_64 on Ubuntu 12.04LTS, Debian Wheezy. The same code using the same input file works in Anaconda on Windows 7, and also using the python-opencv 2.3.1-7 package (and stock python) on Ubuntu. The input file is H.264 video in a MP4 container. Anaconda is installed system-wide under /opt/anaconda, and /opt/anaconda/bin is in the path.

It's not just one particular file: I've tried many video files, none work on Linux (including different containers avi ts mp4, and different codecs H.264 DIVX etc). Writing files also fails.

Is there a trick to getting Anaconda cv2 video read/write fully working on Linux?

Update: tested with Anaconda-2.0.0-Linux-x86_64 on Ubuntu 14.04LTS, same problem.

Small video file that can be used to test this: http://techslides.com/demos/sample-videos/small.mp4

I met the same issue and couldn't solve.

This is a workaround rather than a solution, but it does help: install scikit-video (for example with pip install scikit-video) and either avconv or ffmpeg, then use skvideo.io.VideoCapture in place of cv2.VideoCapture.

import sys
import skvideo.io
cap = skvideo.io.VideoCapture(sys.argv[1])
ret, frame = cap.read()

API is a drop-in replacement.

The opencv installed from binstar may be compiled with WITH_FFMPEG=OFF so no video i/o.

I've compiled OpenCV 3 on 14.04 with WITH_FFMPEG both on and off, and neither worked. @aizvorski 's workaround worked for me too though, so thanks! For anyone who wants a drop-in fix,

sudo apt-get install libav-tools
pip install scikit-video

(I suspect the root problem might be that Ubuntu moved from ffmpeg to libav in 14.04, but that should be qualified by the fact that I know nothing about the OpenCV internals)

Still a problem in Anaconda-2.1.0-Linux-x86_64 (Ubuntu 14.04 LTS)

Still a problem in Conda 3.12.0.

Any plans on fix?

Thanks @aizvorski and @andyljones, that worked! Will have to do until an official fix comes around.

@Strateus This is not quite the oldest open Anaconda issue, but it is getting there. Btw, I am told this works on at least some Linux distros which are not related to Debian, for example Arch. On Debian and Ubuntu (and friends) it still doesn't work, a year and a half later.

Continuum guys, can you please tag this as a bug? It is confirmed by a bunch of people already, and there is a simple way to reproduce it.

Same here; running .mp4 and .avi files and it didn't work. Latest versions of anaconda, opencv3, and python3 as of this post.

same problem here.
with anaconda2, opencv 2.4.10 (installed via conda install opencv), and python 2.7

I got ffmpeg working by following instruction on this website: https://github.com/menpo/conda-opencv

Rebuild the opencv with -DWITH_FFMPEG = 1 in build.sh. It should work.

Still an issue

Still an issue.. Any permanent solution guys?

Facing the same issue . .

Removing opencv from Anaconda, and using cv.py which OpenCV generated while it was compiled worked for me.
PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH python <python-file>.
I know people here are mainly concerned about opencv for Anaconda, but I report this for those who want to use cv module anyway.

Same problem here... thanks @aizvorski for the workaround

@gyifan ,where is DWITH_FFMPEG = 1 in build.sh???thank you!

The issue here is that the Anaconda opencv package is not compiled with ffmpeg support currently. ffmpeg is difficult to support across platforms. If anyone is interested in commercially sponsoring this support in Anaconda, we can rebuild opencv so that it supports ffmpeg. Please contact sales@continuum.io

@msarahan hello, if i want to use opencv capture video ,which package i can use? thank you!!

Same problem using Python 3.5.2, Anaconda 4.2.0 (64-bit), OpenCV 3.1.0 installed with conda install -c menpo opencv3=3.1.0.

We don't have the workaround from @aizvorski and @andyljones anymore! VideoCapture was dropped in their video API and also the package was substituted for sk-video instead of scikit-video.
For more info: http://www.scikit-video.org/stable/faq.html

You should do:
sudo pip uninstall scikit-video
sudo pip install sk-video

If you use this on current scikit-video you get an error:

cap = VideoCapture('2538-5_70133.avi')
Traceback (most recent call last):
File "", line 1, in
File "/home/01677387637/anaconda3/lib/python3.5/site-packages/skvideo/io/init.py", line 26, in init
self.info = self.get_info()
File "/home/01677387637/anaconda3/lib/python3.5/site-packages/skvideo/io/init.py", line 119, in get_info
info = json.loads(output)
File "/home/01677387637/anaconda3/lib/python3.5/json/init.py", line 312, in loads
s.class.name))
TypeError: the JSON object must be str, not 'bytes'

I'm also having the same problem. installed opencv using[ conda install -c menpo opencv3=3.1.0 ]

any solution for this?
i want to use video capture to load and save videos.

I installed the opencv with conda install opencv on the Ubuntu 16.04 machine.

But I found that I cannot handle the video files, then, I installed ffmpeg with conda install -c menpo ffmpeg=3.1.3 After that, I can use cv2.VideoCapture to read and cv2.VideoWriter to write the '.avi' video files with codec "*'MJPG'", but I still cannot use the functions above to read or write the ".mp4" video files.

Last, @aizvorski @staticdev solution works fine to me.
So, I can read the ".mp4" video files with skvideo.io.vreader and use the cv2 to write to the ".avi" files.

Is there any more elegant solution to the problem?

@staticdev The problem is, that other packages, like sk-video can only be used to load videos and pass them frame by frame (if loaded with ffmpeg reader) or have to be loaded entirely, which takes time for long videos. Still don't know any library that offers this much video functionality like opencv and is also fast..

@pecorarista I tried the same under ubuntu 14.04 and 16.04 but couldn't get it running. Can you confirm that you can use Anaconda2/3 with OpenCV 3.X.X and VideoCapture working?

None of the solutions above worked for me...

The problem is, that the OpenCV linux package in the forge is build without ffmpeg support (at the moment). If you want to use the video functionality you have to build it on your own.
I wrote some "guide" how to build OpenCV 3 for Python 2/3 with ffmpeg support here. It's written for Miniconda3 but you can substitute Miniconda3 with Anaconda3 in all the paths and it should work without problems

Same issue here :(

So what's needed for this issue to be solved? Add ffmpeg as dependency of opencv?

@ccordoba12 If I'm right, OpenCV for Linux must be rebuild with the ffmpeg flag set to 1

The following worked for me,
I am able to read from .mp4 and .avi files on MacOS.

brew install ffmpeg
brew uninstall opencv3 (If already installed)
brew install opencv3 --with-ffmpeg -v (Python 2.7)
brew install opencv3 --with-python3 --with-ffmpeg -v (Python 3.6)

@csoja, should we recompile opencv with ffmpeg support and add ffmpeg to our list of supporrted packages?

That's the only solution to this problem.

csoja commented

@ccordoba12 that would definitely be the ideal path to take. Unfortunately, there is higher priority work that must be done first - so I'm not sure when we would have someone available to take this on. As @msarahan mentioned earlier in this issue, if there was someone willing to sponsor the support - we'd be able to bump up the priority.

still a problem yeah

@csoja I would happily help you out but don't know really how. If you can more specifically tell me what to do I would try my best.

@kratzert, she meant that this work needs to be sponsored by hiring Continuum to do it.

@ccordoba12 Hi! This seems still unsolved as of today (Anaconda3-4.3.1-Linux-x86_64 Python3.6, conda install opencv has no ffmpeg support). Is there a way such that we can feed flags to conda install (something analogous to setting WITH_FFMPEG while building by hand)?

Also, what's the problem with

should we recompile opencv with ffmpeg support and add ffmpeg to our list of supporrted packages?

Thanks a lot!

For me it worked to install libraries, which are checked in

cmake/OpenCVFindLibsVideo.cmake:225
  • libavcodec
  • libavformat
  • libavutil
  • libswscale
  • libavresample

each with eg.

apt-get install libavresample-dev

PS: Then recompile opencv with

cmake ..
make

Any updates on getting this to work with Anaconda?

I've tried a rather simple way and fixed this problem.
1.Remove opencv on anaconda
2.Install ffmpeg
3.Install opencv (Build it from source, DO NOT install it through anaconda)
4.Link the cv2.so to the anaconda lib :
ln -s /usr/local/lib/python2.7/site-packages/cv2.so ~/anaconda2/envs/YOUR_ENV/lib/python2.7/cv2.so
5.Done

Kratzert's method works fine for me. I am not using annocoda. My linux version is 16.04. Wish this helps. https://github.com/kratzert/Ubuntu_from_scratch/blob/master/Ubuntu_16_04LTS.md#installing-opencv3

@kratzert your method works for me, thanks a lot. I'm using anaconda3 in Ubuntu 16.04. In the cmake step, remember to replace USER with your username, and replace miniconda3 with anaconda3 if you use anaconda3.
https://github.com/kratzert/Ubuntu_from_scratch/blob/master/Ubuntu_16_04LTS.md#installing-opencv3

any updates on this? its causing me a lot of trouble. on ubuntu, cant read avi with opencv

There years has passed, and I have same issue. anaconda 4.3.29, python3.6.3

@outlace and @oneTaken you can still use opencv with any python version if you install opencv from source, see my explanation here.

Please try the latest builds of OpenCV, version 3.3.1, which were released last week. These were build with ffmpeg and should be able to read video from a file.

@jjhelmus OpenCV 3.3.1 is currently not available in pip

@khanh96le We do not publish packages on pip, please use conda to install OpenCV.

@jjhelmus Good! Finally solving the problem in easy way. Find it here.
conda install -c anaconda opencv

@jjhelmus How can I select opencv package installed by conda instead of opencv installed by pip?
Because I had installed opencv by pip before, so when I check my virtual environment's packages, there are 2 opencv packgages:

$ source activate myenv
(myenv) $ conda list | grep opencv
opencv                    3.3.1            py36h6cbbc71_1    anaconda  
opencv-python             3.3.0.10                  <pip>

And when I import cv2 and check version, it's still 3.3.0

import cv2
print(cv2.__version__)
>> 3.3.0

Only a single OpenCV package should be installed in an environment, installing from both pip and conda will likely result in one package overwriting the other.

My suggestions would be to uninstall both OpenCV installs using pip uninstall opencv-python and conda remove opencv and then re-installing the conda provided package using conda install opencv. Alternatively you could create a new conda environment and install only the conda provided opencv.

@jjhelmus I installed opencv using : conda install opencv.

I got this error:

OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvShowImage, file /tmp/build/80754af9/opencv_1512687413662/work/modules/highgui/src/window.cpp, line 611
Traceback (most recent call last):
File "test_image.py", line 104, in
args.stride, input_image=args.input_image)
File "test_image.py", line 62, in test_net
cv2.imshow("detection result", draw)
cv2.error: /tmp/build/80754af9/opencv_1512687413662/work/modules/highgui/src/window.cpp:611: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvShowImage

there is something wrong with cv2.imshow. would you please help me fix it?

@hanikh Our OpenCV package does not include GTK support so cv2.imshow will not work. We are looking into adding this into a future build.

@jjhelmus I'm so supprised when you said cv2.imshow not work. Showing image is the feature that everyone uses OpenCv needs. Is there any workarounds here?
UPDATE: I've checked opencv-python, it has already upgraded to version 3.4.0.12, and the video related functions issues have been solved.

I am facing the same issue when reading .avi files.... cv2.VideoCapture fails reading the video

installing opencv with 'pip3 install opencv-python' works for me

Still an issue

We will not be providing h264 or h265 support for licensing reasons. That is the issue here.

Finally worked.. Used the solution mentioned by @WystanDeng . As mentioned here: [Link]

conda install -c anaconda opencv

Using Ubuntu 16.04 and Anaconda2-5.2.0-Linux-x86_64.sh

I have the same issue with opencv VideoCapture, and tried all the mentioned solutions here. None worked for me so far. Did someone find a better way to solve this issue?

Using skvideo.io.VideCapture I get the following error: module 'skvideo.io' has no attribute 'VideoCapture'. I could not solve this either.

I have the same issue with opencv VideoCapture, and tried all the mentioned solutions here. None worked for me so far. Did someone find a better way to solve this issue?

Using skvideo.io.VideCapture I get the following error: module 'skvideo.io' has no attribute 'VideoCapture'. I could not solve this either.

Facing the same issue, were you able to resolve this?