简体   繁体   English

如何在python中与cv2.waitKey()一起提供条件?

[英]How to give a condition along with cv2.waitKey() in python?

I need to capture video and stop video after 10 seconds.But when i give condition along with cv2.waitKey() video stops instantly.When i separate the condition the second condition(elapsed==10) doesn't work.My sample code is 我需要捕获视频并在10秒后停止视频。但是当我给出条件以及cv2.waitKey()时视频立即停止。当我分离条件时,第二个条件(elapsed == 10)不起作用。我的示例代码是

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

How to stop video after 10 seconds? 10秒后如何停止播放视频?

Try using elapsed>=10 . 尝试使用elapsed>=10
It's not sure your code will EXACTLY hit the 10 elapsed seconds. 不确定您的代码是否会准确地达到10秒。
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. 如果经过了10.110.000000000001秒,您的程序将错过时间并且永远不会停止,因为将永远不会满足该条件。

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

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