简体   繁体   English

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

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

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. 该程序播放从第一个程序保存的视频文件output.avi。 The thing is, this video appears fast forward. 问题是,这个视频出现了快进。 So, I tried changing the delay value for cv2.waitKey(). 所以,我尝试更改cv2.waitKey()的延迟值。 The video looked fine when I put 100. How do I know which value to put there? 当我放100时,视频看起来很好。我怎么知道放在哪个值? 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的帧速率(见第二个程序中的cap.get(5)行)并得到20.但是如果我使用20作为cv2.waitKey()的延迟,则视频仍然太快。

Any help would be appreciated. 任何帮助,将不胜感激。

The function waitKey() waits for a key event infinitely (when delay <= 0) or for delay milliseconds, when it is positive. 函数waitKey()无限地等待一个键事件(当delay <= 0时)或者等待延迟毫秒,当它为正时。

If the FPS is equal to 20, then you should wait 0,05 seconds between the consecutive frames. 如果FPS等于20,那么您应该在连续帧之间等待0.05秒。 So put waitKey(50) after imshow() and it will be displayed at normal speed. 所以把waitKey(50)之后imshow()它会以正常速度显示。

For what it is worth, I have tried all sorts of tricks with setting the cv2.waitKey() delay time and they have all failed. 对于它的价值,我已经尝试了各种技巧,设置了cv2.waitKey()延迟时间,但它们都失败了。 What I have found to work is to try something like: key = cv2.waitKey(1) inside of your while(cap.isOpened()) like so: 我发现工作的是尝试类似: 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. 我希望这可以帮助那里的人。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM