12
votes

I'm trying to use OpenCV's cv2 python bindings on an Amazon server running Ubuntu 14.04 and I can't seem to get VideoCapture to work properly.

I tried opening the default capture as follows:

import cv2
cap = cv2.VideoCapture(0)
cap.isOpened() #Returns false

I tested this on my local machine and it was true as expected, so there is something wrong with my open CV configuration. I've tried a variety of things:

  • Using an actual filepath that I confirmed points to an .mp4 file
  • Using -1 and 1 in place of 0 on the second line
  • Installing ffmpeg (from a ppa as it isn't available by default on Ubuntu 14.04) and rebuilding OpenCV
  • Removing my OpenCV directory entirely and rebuilding using the script here
  • Verifying and reinstalling various other libraries including x264, gstreamer, and gtk

I'm kind of out of ideas at this point. Any ideas of what could be going wrong?

Edit: OpenCV version is 2.4.9.

2
As I know VideoCapture is for capturing video from camera but Amazon servers have not camera.furas
VideoCapture is also used to capture video from files, which is what I'm trying to do.The Bearded Templar
As I see in documentations VideoCapture(0) means use default camera. Probabably need more code to do this.furas
Yes, after reading your comment I considered that, perhaps it only works locally because I have a camera on my local machine. However, switching VideoCapture(0) with VideoCapture("/path/to/video/video.mp4") returns the same error.The Bearded Templar
Thanks - it actually just started working. I'm not entirely sure why, but the most recent thing I did was reinstall ffmpeg via instructions here: trac.ffmpeg.org/wiki/CompilationGuide/UbuntuThe Bearded Templar

2 Answers

9
votes

I also faced a similar problem. Possible solutions:

  1. Check if you have given the correct path.

  2. If you have installed OpenCV using pip, it will not work. You can remove OpenCV and reinstall it looking at the official documentation.

  3. Ways to install via pip,
    pip install opencv-python Only installs main module
    pip install opencv-contrib-python Install main and contrib module, so use this command.

0
votes

I had the same problem and was solved successfully. You should to build OpenCV with WITH_FFMPEG flag:

cmake .. -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DWITH_GTK=ON -DWITH_FFMPEG=1 

And you should view "YES" everywhere in section FFMPEG:

Video I/O:
--     DC1394 1.x:                  NO
--     DC1394 2.x:                  YES (ver 2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (ver 57.107.100)
--       avformat:                  YES (ver 57.83.100)
--       avutil:                    YES (ver 55.78.100)
--       swscale:                   YES (ver 4.8.100)
--       avresample:                YES (ver 3.7.0)
--     GStreamer:                   NO
--     OpenNI:                      NO
--     OpenNI PrimeSensor Modules:  NO
--     PvAPI:                       NO
--     GigEVisionSDK:               NO
--     UniCap:                      NO
--     UniCap ucil:                 NO
--     V4L/V4L2:                    NO/YES
--     XIMEA:                       NO
--     Xine:                        NO

If you will not see it then need to install next packages via apt (if you are using Debian or Ubuntu Linux):

sudo apt-get install libav-tools libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libx264-dev