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:
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.
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.
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.
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.
Next, we cay cap = cv2. VideoCapture(0) . This will return video from the first webcam on your computer.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With