【问题标题】:Read video file with fixed frame rate以固定帧率读取视频文件
【发布时间】:2023-03-05 04:55:02
【问题描述】:

我用相机拍摄了一段视频,帧速率固定为 25 fps,并尝试使用 OpenCV 读取它。

当我使用 OpenCV 读取视频文件时,它会播放但播放速度非常快。 我希望我的程序以 25 fps 的速度播放视频。如何配置 OpenCV 以 25 fps 读取视频文件?

我的代码:

import numpy as np
import cv2

cap = cv2.VideoCapture('vtest.avi')

while(cap.isOpened()):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

【问题讨论】:

    标签: python opencv image-processing video


    【解决方案1】:

    受您的回答启发:

    while(cap.isOpened()):
    
      ret, frame = cap.read()
    
      now = time.time()
      frameLimit = 2.0
    
      #Do your stuff
    
      timeDiff = time.time() - now
      if (timeDiff < 1.0/(frameLimit)): time.sleep( 1.0/(frameLimit) - timeDiff )
    

    【讨论】:

      【解决方案2】:

      由于这是一个现有的视频文件,您无法更改其 FPS。但是,当您阅读视频文件时,您可以更改每帧之间阅读的时间间隔。这是我以 25 fps 的固定帧速率阅读的解决方案。代码如下:

      import numpy as np
      import cv2
      import sys
      
      cap = cv2.VideoCapture('C:/Media/videos/Wildlife.wmv')
      
      # Check if camera opened successfully
      if (cap.isOpened()== False): 
          print("Error opening video stream or file")
      
      fps = 25
      #if you want to have the FPS according to the video then uncomment this code
      #fps = cap.get(cv2.CAP_PROP_FPS)
      
      #calculate the interval between frame. 
      interval = int(1000/fps) 
      print("FPS: ",fps, ", interval: ", interval)
      # Read the video
      while(cap.isOpened()):
          ret, frame = cap.read()
          if ret == True:
              gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
              cv2.imshow('Frame',gray)
              if cv2.waitKey(interval) & 0xFF == ord('q'):
                  break
          # Break the loop
          else: 
              break
      
      cap.release()
      cv2.destroyAllWindows()
      

      【讨论】:

      • 此解决方案将提供不准确的 fps。因为 fps 还取决于 cap.read()cv2.waitKey(interval) 之间的代码速度。此外,如果系统忙于做其他事情,fps 会下降。因此,interval 需要在每个循环中动态调整。
      【解决方案3】:

      这是解决此问题的有效解决方案:

      fps = vid.get(cv2.CAP_PROP_FPS)
      
      while True:
          now = time.time()
          _, frame = vid.read()
      
          #Do your thing
      
          timeDiff = time.time() - now
          if (timeDiff < 1.0/(fps)):
              time.sleep(1.0/(fps) - timeDiff)
      

      【讨论】:

        【解决方案4】:

        我找到了一些解决方案。 我放了一个延迟时间来捕获循环。我在从视频文件中捕获新图像之前检查延迟。这是我的解决方案代码。 谢谢大家。

        import numpy as np
        import cv2
        from time import time as timer
        import sys
        
            video = cv2.VideoCapture('2.avi')
            fps = video.get(cv2.CAP_PROP_FPS)
            fps /= 1000
            framerate = timer()
            elapsed = int()
            cv2.namedWindow('ca1', 0)
            while(video.isOpened()):
        
                start = timer()
                # print(start)
                ret, frame = video.read()
        
                cv2.imshow('ca1',frame)
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
        
                diff = timer() - start
                while  diff < fps:
                    diff = timer() - start
        
                elapsed += 1
                if elapsed % 5 == 0:
                    sys.stdout.write('\r')
                    sys.stdout.write('{0:3.3f} FPS'.format(elapsed / (timer() - framerate)))
                    sys.stdout.flush()
        
            video.release()
            cv2.destroyAllWindows()
        

        【讨论】:

        • 为什么不直接使用 waitKey 和正确的延迟,而不是额外的忙等待?
        【解决方案5】:

        您可以添加所需的 fps 来代替下面的“fps”

        cap.set(cv2.cv.CV_CAP_PROP_FPS, fps)
        

        【讨论】:

        • 对不起,我用的是opencv 3,找不到cv2.cv.CV_CAP_PROP_FPS。
        • 这对读取视频文件完全没有影响。 (不应该——你最不需要的就是你的 I/O 代码浪费时间什么都不做)。
        • 它是“cv2.CAP_PROP_FPS”,但在我的情况下返回 false。它正在工作但返回错误
        猜你喜欢
        • 1970-01-01
        • 2018-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-14
        • 1970-01-01
        • 2016-02-05
        • 1970-01-01
        相关资源
        最近更新 更多