OpenCV Python 视频播放 - 如何为 cv2.waitKey() 设置正确的延迟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25182278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:50:18  来源:igfitidea点击:

OpenCV Python Video playback - How to set the right delay for cv2.waitKey()

pythonopencvvideodelayframe-rate

提问by Clive

I used the following code to capture a video file, flip it and save it.

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

#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()

This program saves the output as output.avi

该程序将输出保存为 output.avi

Now, to playback the video file I used the following program

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

#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()

This program plays the video file output.avi saved from the first program. The thing is, this video appears fast forward. So, I tried changing the delay value for cv2.waitKey(). The video looked fine when I put 100. How do I know which value to put there? Should it be related to the frame rate? I checked the frame rate of output.avi (see line cap.get(5) in second program) and got 20. But if I use 20 as delay for cv2.waitKey() the video is still too fast.

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

Any help would be appreciated.

任何帮助,将不胜感激。

采纳答案by Kornel

From the OpenCV documentation:

OpenCV 文档

The function cv.waitKey([, delay])waits for a key event infinitely (when delay <= 0) or for delaymilliseconds, when it is positive.

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

If the FPS is equal to 20, then you should wait 0,05 seconds between displaying the consecutive frames. So just put waitKey(50)after imshow()in order to have the desired speed for the playback.

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

回答by jpxrc

For what it is worth, I have tried all sorts of tricks with setting the cv2.waitKey() delay time and they have all failed. What I have found to work is to try something like:key = cv2.waitKey(1)inside of your while(cap.isOpened()) like so:

值得一提的是,我尝试了各种设置 cv2.waitKey() 延迟时间的技巧,但都失败了。我发现可以尝试的方法是:key = cv2.waitKey(1)在你的 while(cap.isOpened()) 中,像这样:

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()

I hope this helps someone out there.

我希望这可以帮助那里的人。

回答by Pravin Desai

put waitKey(60)after imshow()and it will be displayed at normal speed.

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