【问题标题】:OpenCV Python Video playback - How to set the right delay for cv2.waitKey()OpenCV Python 视频播放 - 如何为 cv2.waitKey() 设置正确的延迟
【发布时间】:2014-09-30 16:20:26
【问题描述】:

我使用以下代码捕获视频文件,翻转并保存。

#To save a Video File

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

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

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

这个程序将输出保存为 output.avi

现在,为了播放视频文件,我使用了以下程序

#Playing Video from File

import numpy as np
import cv2

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

print cap.get(5) #to display frame rate of video
#print cap.get(cv2.cv.CV_CAP_PROP_FPS)

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

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #convert to grayscale

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

cap.release()
cv2.destroyAllWindows()

此程序播放从第一个程序保存的视频文件 output.avi。问题是,这段视频看起来快进了。因此,我尝试更改 cv2.waitKey() 的延迟值。当我输入 100 时,视频看起来不错。我怎么知道该输入哪个值?应该和帧率有关吗?我检查了 output.avi 的帧速率(参见第二个程序中的 cap.get(5) 行),得到了 20。但是如果我使用 20 作为 cv2.waitKey() 的延迟,视频仍然太快了。

任何帮助将不胜感激。

【问题讨论】:

    标签: python opencv video delay frame-rate


    【解决方案1】:

    来自OpenCV documentation

    函数cv.waitKey([, delay])无限等待按键事件 (当delay <= 0 时)或delay 毫秒,当它为正时。

    如果 FPS 等于 20,那么您应该在显示连续帧之间等待 0.05 秒。因此,只需将waitKey(50) 放在imshow() 之后即可获得所需的播放速度。

    【讨论】:

    • 所以公式应该是 (1 / fpm) * 1000 吗?
    • 或者实际上,也许它必须是一个整数,所以 int( (1 / int(fps)) * 1000)
    • @Kornel,我怎么知道 FPS 是什么?
    • fps = cap.get(cv2.CAP_PROP_FPS)
    【解决方案2】:

    为了它的价值,我尝试了各种设置 cv2.waitKey() 延迟时间的技巧,但都失败了。我发现的工作是在你的 while(cap.isOpened()) 中尝试类似:key = cv2.waitKey(1) 之类的东西:

    import numpy as np
    import cv2
    
    cap = cv2.VideoCapture(0)
    
    # Define the codec and create VideoWriter object
    fourcc = cv2.cv.CV_FOURCC(*'XVID')
    out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
    
    while(cap.isOpened()):
      ret, frame = cap.read()
      if ret==True:
          key = cv2.waitKey(1)
          frame = cv2.flip(frame,0)
    
          # write the flipped frame
          out.write(frame)
    
          cv2.imshow('frame',frame)
          if key & 0xFF == ord('q'):
              break
        else:
            break
    
    # Release everything if job is finished
    cap.release()
    out.release()
    cv2.destroyAllWindows()
    

    我希望这对那里的人有所帮助。

    【讨论】:

      【解决方案3】:

      waitKey(60)放在imshow()之后,它会以正常速度显示。

      【讨论】:

        猜你喜欢
        • 2021-03-11
        • 1970-01-01
        • 2020-05-02
        • 1970-01-01
        • 1970-01-01
        • 2021-07-24
        • 1970-01-01
        • 2014-01-08
        • 1970-01-01
        相关资源
        最近更新 更多