我无法使用Python + OpenCV打开视频

时间:2015-10-01 10:29:18

标签: python-2.7 opencv video

我尝试打开视频,但我不能。我使用此代码,我尝试打开viedos MP4和AVI。我不知道为什么,但如果我打印cap.isOpened()的值,则返回值为False

import numpy as np
import cv2

cap = cv2.VideoCapture('test.mp4')

while(cap.isOpened()):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:0)

您可能遇到相对路径问题,请尝试以下方法:

import os
my_path=os.path.abspath(os.path.dirname("absolute path to your file"))
path = os.path.join(my_path, "relative path to video")

cap = cv2.VideoCapture(path)