Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV VideoCapture Not Opening

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.

like image 685
The Bearded Templar Avatar asked Jul 11 '14 19:07

The Bearded Templar


People also ask

What is VideoCapture in OpenCV?

VideoCapture is a function of openCV library(used for computer vision, machine learning, and image processing) which allows working with video either by capturing via live webcam or by a video file.

How do I use VideoCapture in Python?

To capture a video in Python, use the cv2 VideoCapture class and then create an object of VideoCapture. VideoCapture has the device index or the name of a video file. The device index is just an integer to define a Camera. If we pass 0, it is for the first or primary camera, 1 for the second camera, etc.

Is VideoCapture read blocking?

VideoCapture(). read() is a blocking operation, the main program is stalled until the frame is read from the camera device and returned. A method to improve performance would be to spawn another thread to handle grabbing frames in parallel instead of relying on a single thread to grab frames in sequential order.

What does cv2 VideoCapture return?

Next, we cay cap = cv2. VideoCapture(0) . This will return video from the first webcam on your computer.


1 Answers

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
like image 152
Orlov Const Avatar answered Sep 19 '22 11:09

Orlov Const