Python OpenCV -- 如何打开文件?

0 投票
1 回答
3327 浏览
提问于 2025-04-18 00:26

在openCV中,用Python怎么打开一个视频文件呢?

我现在有这样的代码:

cap = cv2.VideoCapture('Superman-01-The_Mad_Scientist.mp4')

而我的.mp4文件和这个脚本在同一个文件夹里。当我运行print cap.isOpened()时,结果是false。这怎么才能正确打开这个文件呢?

我还尝试了另外一种方法:

BASE = os.path.dirname(os.path.abspath(__file__))
the_file = open(os.path.join(BASE, 'sample_video','Superman-01-The_Mad_Scientist.mp4'))

print the_file.__str__()
cap = cv2.VideoCapture(the_file)
print cap.isOpened()

输出结果是:

Traceback (most recent call last):
<open file 'C:\dev\sample_video\Superman-01-The_Mad_Scientist.mp4', mode 'r' at 0x02482288>
  File "C:/dev/test.py", line 9, in <module>
    cap = cv2.VideoCapture(the_file)
TypeError: an integer is required

这意味着它在寻找一个摄像头,但教程和API都说可以输入文件名。

1 个回答

-1

试着添加字符串转换

cap = cv2.VideoCapture(str(the_file))

撰写回答