在 Python 中,我如何在 运行 没有 'time.sleep()' 后暂停 'if statement'
In Python how can I pause an 'if statement' after it has run without 'time.sleep()'
我创建了一个虚拟键盘,它从网络摄像头手势中获取输入,作为玩视频游戏的一种新颖方式。
我已经得到了几乎所有的工作,但是在当前的实现中,我使用 time.sleep() 来区分按住的按键和单击的按键,它会暂停视频摄像头。
我已经简要研究了多线程,但不确定在这种情况下我将如何实现它(另外,当我在网络摄像头模块中尝试多线程时,它绝对降低了我的网络摄像头 fps???)
所以我想知道是否有一种方法可以在满足 if 语句后将其阻塞一小段时间。
这是代码片段,对不起,如果我的代码是垃圾。
# Center screen
if screen_pos_left[4] or screen_pos_right[4]:
# Camera Reset - Single
if hand_sign_left == "One Finger" or hand_sign_right == "One Finger":
kb.press_and_release('u')
time.sleep(1.0)
# Move Forward - Held
if hand_sign_left == "Two Fingers" or hand_sign_right == "Two Fingers":
kb.press_and_release('w')
time.sleep(0.1)
# Move Backwards - Held
if hand_sign_left == "Three Fingers" or hand_sign_right == "Three Fingers":
kb.press_and_release('s')
time.sleep(0.1)
# Dodge - Single
if hand_sign_left == "Four Fingers" or hand_sign_right == "Four Fingers":
kb.press_and_release('space')
time.sleep(0.5)
也许这就是您要找的东西?
import asyncio
# Center screen
if screen_pos_left[4] or screen_pos_right[4]:
# Camera Reset - Single
if hand_sign_left == "One Finger" or hand_sign_right == "One Finger":
kb.press_and_release('u')
await asyncio.sleep(1.0)
# Move Forward - Held
if hand_sign_left == "Two Fingers" or hand_sign_right == "Two Fingers":
kb.press_and_release('w')
await asyncio.sleep(0.1)
# Move Backwards - Held
if hand_sign_left == "Three Fingers" or hand_sign_right == "Three Fingers":
kb.press_and_release('s')
await asyncio.sleep(0.1)
# Dodge - Single
if hand_sign_left == "Four Fingers" or hand_sign_right == "Four Fingers":
kb.press_and_release('space')
await asyncio.sleep(0.5)
基于,await asyncio.sleep()
和time.sleep()
的区别在于time.sleep()
冻结了整个脚本,而await asyncio.sleep()
则没有。
我创建了一个虚拟键盘,它从网络摄像头手势中获取输入,作为玩视频游戏的一种新颖方式。 我已经得到了几乎所有的工作,但是在当前的实现中,我使用 time.sleep() 来区分按住的按键和单击的按键,它会暂停视频摄像头。 我已经简要研究了多线程,但不确定在这种情况下我将如何实现它(另外,当我在网络摄像头模块中尝试多线程时,它绝对降低了我的网络摄像头 fps???) 所以我想知道是否有一种方法可以在满足 if 语句后将其阻塞一小段时间。 这是代码片段,对不起,如果我的代码是垃圾。
# Center screen
if screen_pos_left[4] or screen_pos_right[4]:
# Camera Reset - Single
if hand_sign_left == "One Finger" or hand_sign_right == "One Finger":
kb.press_and_release('u')
time.sleep(1.0)
# Move Forward - Held
if hand_sign_left == "Two Fingers" or hand_sign_right == "Two Fingers":
kb.press_and_release('w')
time.sleep(0.1)
# Move Backwards - Held
if hand_sign_left == "Three Fingers" or hand_sign_right == "Three Fingers":
kb.press_and_release('s')
time.sleep(0.1)
# Dodge - Single
if hand_sign_left == "Four Fingers" or hand_sign_right == "Four Fingers":
kb.press_and_release('space')
time.sleep(0.5)
也许这就是您要找的东西?
import asyncio
# Center screen
if screen_pos_left[4] or screen_pos_right[4]:
# Camera Reset - Single
if hand_sign_left == "One Finger" or hand_sign_right == "One Finger":
kb.press_and_release('u')
await asyncio.sleep(1.0)
# Move Forward - Held
if hand_sign_left == "Two Fingers" or hand_sign_right == "Two Fingers":
kb.press_and_release('w')
await asyncio.sleep(0.1)
# Move Backwards - Held
if hand_sign_left == "Three Fingers" or hand_sign_right == "Three Fingers":
kb.press_and_release('s')
await asyncio.sleep(0.1)
# Dodge - Single
if hand_sign_left == "Four Fingers" or hand_sign_right == "Four Fingers":
kb.press_and_release('space')
await asyncio.sleep(0.5)
基于await asyncio.sleep()
和time.sleep()
的区别在于time.sleep()
冻结了整个脚本,而await asyncio.sleep()
则没有。