如何在python中提供cv2.waitKey()的条件?

时间:2017-02-08 10:08:36

标签: python

我需要在10秒后捕获视频并停止视频。但是当我提供条件以及cv2.waitKey()时,视频会立即停止。当我将条件分开时,第二个条件(已过去== 10)不会work.My示例代码是

import cv2
import time
cap = cv2.VideoCapture(0)
start_time=time.time()
while(True):
  # Capture frame-by-frame
  ret, frame = cap.read()
  # Our operations on the frame come here
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  e_time = time.time()
  elapsed = e_time - start_time
  # Display the resulting frame
  cv2.imshow('frame',gray)
  if cv2.waitKey(1) or elapsed==10:
    break
cap.release()
cv2.destroyAllWindows()

如何在10秒后停止播放视频?

1 个答案:

答案 0 :(得分:1)

Try using elapsed>=10.
It's not sure your code will EXACTLY hit the 10 elapsed seconds.
If 10.1 or 10.000000000001 seconds are elapsed your program will miss the time and never stop, because the condition will never be met.