我编写了以下代码来打开一个视频文件,该文件与脚本位于同一目录中,而且从摄像头向文件写入视频源的代码无效!
import numpy as np
import cv2
cap = cv2.VideoCapture('F:/vtest.avi')
print cap.isOpened()
if(cap.isOpened()== False):
cap.open('F://vtest.avi')
print cap.isOpened()
while(cap.read()):
ret,frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
但代码引发了以下错误。我尝试在Python目录中移动ffmpeg.dll文件,但无济于事。
False
False
Traceback (most recent call last):
File "F:\2.py", line 11, in <module>
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
error: ..\..\..\..\opencv\modules\imgproc\src\color.cpp:3480: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
答案 0 :(得分:0)
试试这个:
import numpy as np
import cv2
cap = cv2.VideoCapture('F:/vtest.avi')
while(cap.isOpened()):
ret,frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
此外,将视频放在与此脚本相同的目录中,并检查它是否有效。