繁体   English   中英

我正在尝试使用python逐帧读取视频,以对帧执行一些处理

[英]I am trying to read a video frame by frame using python , to execute some processes on frames

这是我的代码

import cv2
video_capture = cv2.VideoCapture("test.mpeg")
cv2.convertMaps

while True:
    # get frame by frame
    ret, frame = video_capture.read()
    cv2.imwrite('pic.png',frame)
    cv2.imshow('Video', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

我收到以下错误:

None

Traceback (most recent call last):
  File "D:/Itellingence Transportation Systems/Material-lab8/home_work8.py", line 12, in <module>
    cv2.imshow('Video', frame)
error: ..\..\..\..\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0 in function cv::imshow

问题是什么?

我相信答案很简单。 您的循环无法检测视频何时完成。 最终,当尝试读取时,video_capture对象将返回False。 您应该在循环中检查该条件是否正常退出。

import cv2
video_capture = cv2.VideoCapture("test.mpeg")

while True:
    # get frame by frame
    ret, frame = video_capture.read()
    if not ret:
        break
    cv2.imwrite('pic.png',frame)
    cv2.imshow('Video', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM