【问题标题】:To generate frames from video using opencv使用 opencv 从视频中生成帧
【发布时间】:2020-06-25 17:41:22
【问题描述】:

谁能解释一下,框架是如何使用下面的代码生成的。我使用了相同的 另一个视频上的代码,并且只创建一个帧。

count = 0
videoFile = "GOPR6351.mp4"
cap = cv2.VideoCapture(videoFile)   # capturing the video from the given path
frameRate = cap.get(5) #frame rate
x=1
while(cap.isOpened()):
    frameId = cap.get(1) #current frame number
    ret, frame = cap.read()
    if (ret != True):
        break
    if (frameId % math.floor(frameRate) == 0):
        filename ="frame%d.jpg" % count;count+=1
        cv2.imwrite(filename, frame)
cap.release()
print ("Done!")

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    您可以使用以下代码从视频中生成帧

    import cv2
    vidcap = cv2.VideoCapture('video.mp4')
    def getFrame(sec):
        vidcap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
        hasFrames,image = vidcap.read()
        if hasFrames:
            cv2.imwrite("image"+str(count)+".jpg", image)     # save frame as JPG file
        return hasFrames
    sec = 0
    frameRate = 0.5 #//it will capture image in each 0.5 second
    count=1
    success = getFrame(sec)
    while success:
        count = count + 1
        sec = sec + frameRate
        sec = round(sec, 2)
        success = getFrame(sec)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-02
      • 2012-03-04
      • 1970-01-01
      • 2011-09-09
      • 2016-12-22
      • 1970-01-01
      • 2015-08-28
      • 1970-01-01
      相关资源
      最近更新 更多