我必须读取本地.avi
文件并将其放在窗口上。这是我的代码:
import os,cv2
user=os.path.expanduser('~')
capture=cv2.VideoCapture(str(user)+"/Downloads/vehicle/Sunny/april21.avi")
if(capture.isOpened()):
print "Open"
else:
print "Fail to open!"
fps=capture.get(cv2.CAP_PROP_FPS)
fourcc=capture.get(cv2.CAP_PROP_FOURCC)
print fourcc#why fourcc is return 0.0?
print "fps:%d"%fps
size=(int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))/2,
(int)(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))/2
)
isStop=1
while isStop:
grabbed,frame=capture.read()#frame is None
cv2.namedWindow("window")
img=cv2.resize(frame,size,interpolation=cv2.INTER_CUBIC)
cv2.imshow("window",img)
c=0xFF&cv2.waitKey(1)
if c ==27:
isStop=False
cv2.destroyAllWindows()
但现在的结果是:
Open
0.0
fps:29
OpenCV Error: Assertion failed (ssize.area() > 0) in resize, file /Users/tom/opencv-3.1.0/modules/imgproc/src/imgwarp.cpp, line 3229
Traceback (most recent call last):
File "/Users/tom/Documents/readVideo.py", line 20, in <module>
img=cv2.resize(frame,size,interpolation=cv2.INTER_CUBIC)
cv2.error: /Users/tom/opencv-3.1.0/modules/imgproc/src/imgwarp.cpp:3229: error: (-215) ssize.area() > 0 in function resize
我唯一确定的是这些代码是正确的,因为我已经通过cv2.VideoWriter
创建了一个.avi
文件,并且可以读取并在窗口中显示。谢谢你的回答。
OpenCV返回的数据类型很可能是一个双精度(浮点),其任意内容(FOURCC代码)无效(不符合IEEE)。
我不懂 Python,但以下 C/C++ 代码段可以完成这项工作:
union {
char c[5];
int i;
} myfourcc;
myfourcc.i = capture.get(CV_CAP_PROP_FOURCC);
myfourcc.c[4] = ' ';
std::cout << "4cc:" << myfourcc.c << std::endl;
我想 Python 有一种更惯用的方式,只是 ctypes。只是我的便士。