相关疑难解决方法(0)

从python中的VideoCapture opencv获取特定帧

我有以下代码,它通过在python中的opencv中使用VideoCapture库连续从视频中获取所有帧:

import cv2

def frame_capture:
        cap = cv2.VideoCapture("video.mp4")
        while not cap.isOpened():
                cap = cv2.VideoCapture("video.mp4")
                cv2.waitKey(1000)
                print "Wait for the header"

        pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
        while True:
                flag, frame = cap.read()
                if flag:
                        # The frame is ready and already captured
                        cv2.imshow('video', frame)
                        pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
                        print str(pos_frame)+" frames"
                else:
                        # The next frame is not ready, so we try to read it again
                        cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos_frame-1)
                        print "frame is not ready"
                        # It is better to wait for a while for the …
Run Code Online (Sandbox Code Playgroud)

python opencv video-capture

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

opencv ×1

python ×1

video-capture ×1