简体   繁体   English

python上的cv2.VideoCapture

[英]cv2.VideoCapture on python

I am running the following code: 我正在运行以下代码:

import numpy as np
import cv2
import os

count = 0

cap = cv2.VideoCapture("/home/simon/PROJECT/real_data/00000020.mp4")

while not cap.isOpened():
    cap = cv2.VideoCapture("./00000020.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 next frame to be ready
        cv2.waitKey(1000)

    if cv2.waitKey(10) & 0xFF == 27:
        break
    if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) ==       cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT):
    # If the number of captured frames is equal to the total number of frames,
    # we stop
    break


if ret == True:
    frame = cv2.VideoCapture.grab()
    frame = 'frame%d' % count 

    cv2.imwrite('frame%d.png', frame)

    count += 1
else:
    print 'stopped at' + count
    break

And whenever I run it, it loops on the while not loop, printing "wait for header". 每当我运行它时,它就会在while not循环中循环,并显示“ waiting header”。 There is never an error code or anything like that either. 永远不会有错误代码或类似的东西。

I have tried to run it as a more simple piece of code, where it doesnt have all these checks, and again that doesn't throw any errors. 我试图将其作为更简单的代码运行,其中没有所有这些检查,并且再次没有引发任何错误。

I am attempting to run this code to open a video, and then save the frames as png files throughout the video. 我正在尝试运行此代码以打开视频,然后在整个视频中将帧另存为png文件。

Does anyone spot any particular problems with the code? 是否有人发现代码有任何特殊问题? Or alternatively does anyone know a piece of code that would do what i want more efficiently, as I have trawled through google searches and stack overflow a lot recently and haven't found anything 或者,是否有人知道一段代码可以更有效地完成我想要的事情,因为我最近通过Google搜索进行了搜索,并且堆栈溢出很多,却一无所获

Thanks in advance Panda 在此先感谢熊猫

You need to include a couple of DLLs in your python directory in order to play videos. 您需要在python目录中包含几个DLL才能播放视频。 Please see this for details: 请查看以下详细信息:

https://li8bot.wordpress.com/2014/07/09/opencvpython-part-1-working-with-videos/ https://li8bot.wordpress.com/2014/07/09/opencvpython-part-1-working-with-videos/

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

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