【发布时间】:2021-05-03 19:37:18
【问题描述】:
我目前正在编写一个程序,该程序使用带有 haar 级联的 openCV 眼睛。如果几秒钟内没有检测到眼睛,我需要它来运行一些代码,而且我不知道如何使用时间库来检查时间。这是我尝试过的一个例子。
if len(eyes) == 0:
if noEyeTime - startTime > 2:
print("closed for more than 2 seconds")
time.sleep(2)
startTime = time.time()
noEyeTime = time.time()
# The variables are declared outside of the while loop
这是我写的检查(它在一个 while True 循环内),我尝试移动变量更新,但我不知道如何检查是否没有检测到眼睛 x 量时间。
我也读过这篇文章: Python if condition true for x amount of time
但该解决方案对我不起作用,因为程序仍需要运行,因此 time.sleep() 不是一个选项。
提前致谢。
【问题讨论】:
-
你需要在检测指令之前设置定时器
startTime = time.time(),并在循环过程中累计该时间,直到满足条件(应该在while循环中)noEyeTime - startTime > 2,然后你可以返回True或False或任何你想要的结果。
标签: python opencv if-statement