pythonopencvvideoimread

python reading a video


I am using Python 2.7.11 and OpenCV 2.4.9. I cannot read a video by using cv2.imread() or cv2.VideoCapture().

import cv2

cap = cv2.VideoCapture('cam.avi')
print ("open  = ",cap.isOpened())

OR

import cv2

cap = cv2.imread('cam.avi')
print ("open  = ",cap.isOpened())

It will return false. I don't know why. I am sure that the cam.avi is here.


Solution

  • Try providing full path to video, like:

    import cv2
    
    cap = cv2.VideoCapture(r'C:\Users\e01069\Downloads\drop.avi')
    print ("open  = ",cap.isOpened())
    

    If you run following in your same file, you would know that python is looking for your file on some different location.

    import os
    print os.path.abspath(__file__)  #this is your current working directory
    

    Note: .imread wouldn't work this way.