Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenCV video capture from file fails on Linux #121

Closed
aizvorski opened this issue May 26, 2014 · 66 comments
Closed

OpenCV video capture from file fails on Linux #121

aizvorski opened this issue May 26, 2014 · 66 comments

Comments

@aizvorski
Copy link

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?

@aizvorski
Copy link
Author

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

@aizvorski
Copy link
Author

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

@shiquanwang
Copy link

I met the same issue and couldn't solve.

@aizvorski
Copy link
Author

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.

@shiquanwang
Copy link

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

@andyljones
Copy link

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)

@aizvorski
Copy link
Author

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

@JaimeIvanCervantes
Copy link

Still a problem in Conda 3.12.0.

@Strateus
Copy link

Strateus commented Dec 4, 2015

Any plans on fix?

@saif-data
Copy link

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

@aizvorski
Copy link
Author

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

@csoja csoja assigned csoja and ilanschnell and unassigned csoja Dec 7, 2015
@yingw787
Copy link

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

@kratzert
Copy link

kratzert commented Mar 2, 2016

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

@gyifan
Copy link

gyifan commented Mar 3, 2016

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.

@YuzhongHuang
Copy link

Still an issue

@dakshvar22
Copy link

Still an issue.. Any permanent solution guys?

@rahulsinghk998
Copy link

Facing the same issue . .

@pecorarista
Copy link

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.

@NMitruka
Copy link

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

@litingsjj
Copy link

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

@msarahan
Copy link
Contributor

msarahan commented Dec 9, 2016

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

@litingsjj
Copy link

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

@staticdev
Copy link

staticdev commented Dec 14, 2016

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'

@pramitsawant
Copy link

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.

@chunhanl
Copy link

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

@BinWang28
Copy link

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

@lonway
Copy link

lonway commented Dec 1, 2017

@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

@outlace
Copy link

outlace commented Dec 13, 2017

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

@oneTaken
Copy link

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

@kratzert
Copy link

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

@jjhelmus
Copy link
Contributor

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.

@khanh96le
Copy link

@jjhelmus OpenCV 3.3.1 is currently not available in pip

@jjhelmus
Copy link
Contributor

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

@WystanDeng
Copy link

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

@khanh96le
Copy link

@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

@jjhelmus
Copy link
Contributor

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.

@hanikh
Copy link

hanikh commented Dec 26, 2017

@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?

@jjhelmus
Copy link
Contributor

jjhelmus commented Jan 4, 2018

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

@khanh96le
Copy link

khanh96le commented Jan 16, 2018

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

@jayshah19949596
Copy link

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

@BinWang28
Copy link

BinWang28 commented Feb 25, 2018 via email

@csengwei
Copy link

csengwei commented Apr 1, 2018

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

@tonmoyborah
Copy link

Still an issue

@mingwandroid
Copy link

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

@think-high
Copy link

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

@kamaligit
Copy link

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.

@barkhajava
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests