【问题标题】:Cutting Video on frames, save only every 3rd?在帧上切割视频,仅每 3 次保存一次?
【发布时间】:2017-08-29 02:27:37
【问题描述】:

我试图考虑它,但由于新的 - 它对我不起作用。 有没有人可以帮忙,我们应该在这里添加什么以便能够保存,例如,每第 3 帧或第 5 帧? 这是代码

    import cv2
    vidcap = cv2.VideoCapture('myvid.mp4')
    success,image = vidcap.read()
    count = 0;
    print "I am in success"
    while success:
      success,image = vidcap.read()
      if count % 3 == 0:
      cv2.imwrite("img_%3d.jpg" % count, image)     
      if cv2.waitKey(10) == 27:                     
          break
      count += 1

非常感谢您对这么愚蠢的问题的帮助^^'

跳过 n 帧的代码并保存您需要的内容。每第三帧的示例:

import cv2
vidcap = cv2.VideoCapture('myvid.mp4')
success,image = vidcap.read()
count = 0;
print "I am in success"
while success:
  success,image = vidcap.read()
  if count % 3 == 0:
  cv2.imwrite("img_%3d.jpg" % count, image)     
  if cv2.waitKey(10) == 27:                     
      break
  count += 1

【问题讨论】:

    标签: python image opencv video


    【解决方案1】:
    import cv2
    vidcap = cv2.VideoCapture('/content/drive/MyDrive/Front-Dash-Cam-1.m4v')
    length = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
    print("Total frames in this vid :", length )
    success,image = vidcap.read()
    count = 0
    skipframes = 5 # no of frames to skip
    print('started slicing..')
    success = True
    while success:
      success,image = vidcap.read()
      if (count % skipframes == 0):
        # print('slicing...')
        image  = cv2.resize(image, (256,256)) # resize frame
        cv2.imwrite("frames/frame_%3d.png" % count, image)     # save frame as JPEG file
      if cv2.waitKey(10) == 27:                     # exit if Escape is hit
          break
      count += 1
    print('finished..') 
    

    【讨论】:

      【解决方案2】:

      您需要做的就是检查是否count % 3 == 0。但是,您的代码中还有另一个问题

      import cv2
      vidcap = cv2.VideoCapture('myvid.mp4')
      success,image = vidcap.read()
      count = 0;
      
      # number of frames to skip
      numFrameToSave = 3
      
      print "I am in success"
      while success: # check success here might break your program
        success,image = vidcap.read() #success might be false and image might be None
        #check success here
        if not success:
          break
      
        # on every numFrameToSave 
        if (count % numFrameToSave ==0):
          cv2.imwrite("img_%3d.jpg" % count, image)   
      
        if cv2.waitKey(10) == 27:                     
            break
        count += 1
      

      【讨论】:

      • 非常感谢!老实说,一分钟。之前找到了解决这个问题的方法,就像你写的 count % 3 == 0。
      • success,image = vidcap.read() 从 while 循环放在循环结束时,无需在循环内检查是否成功,因为它将检查它是否进入循环。另一个优点是你没有像现在这样的第一帧丢失,因为双重阅读(在第 3 行和第 11 行)。
      猜你喜欢
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      相关资源
      最近更新 更多