cli: reuse gesture instance; cancel pending tasks on shutdown
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Id1471dd8fb5a56d3cfa514d236624d686a6a6964
This commit is contained in:
parent
fcef0593eb
commit
82f84beb2a
1 changed files with 9 additions and 3 deletions
|
|
@ -14,13 +14,13 @@ from flipoff.gesture import GestureRegistry
|
||||||
|
|
||||||
def _get_callback(
|
def _get_callback(
|
||||||
loop: asyncio.AbstractEventLoop,
|
loop: asyncio.AbstractEventLoop,
|
||||||
gesture_cls: type[Gesture],
|
gesture_instance: Gesture,
|
||||||
event_instance: object,
|
event_instance: object,
|
||||||
cooldown: float,
|
cooldown: float,
|
||||||
last_trigger: list[float],
|
last_trigger: list[float],
|
||||||
) -> callable:
|
) -> callable:
|
||||||
def callback(hand: object) -> bool:
|
def callback(hand: object) -> bool:
|
||||||
gesture_detected = gesture_cls().detect(hand)
|
gesture_detected = gesture_instance.detect(hand)
|
||||||
if gesture_detected:
|
if gesture_detected:
|
||||||
now = time.time()
|
now = time.time()
|
||||||
if now - last_trigger[0] > cooldown:
|
if now - last_trigger[0] > cooldown:
|
||||||
|
|
@ -60,7 +60,7 @@ def run(
|
||||||
gesture_instance = gesture_cls()
|
gesture_instance = gesture_cls()
|
||||||
last_trigger = [0.0]
|
last_trigger = [0.0]
|
||||||
|
|
||||||
callback = _get_callback(loop, gesture_cls, event_instance, cooldown, last_trigger)
|
callback = _get_callback(loop, gesture_instance, event_instance, cooldown, last_trigger)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
ret, frame = camera.read()
|
ret, frame = camera.read()
|
||||||
|
|
@ -91,6 +91,12 @@ def run(
|
||||||
if not headless:
|
if not headless:
|
||||||
cv2.destroyAllWindows()
|
cv2.destroyAllWindows()
|
||||||
detector.close()
|
detector.close()
|
||||||
|
|
||||||
|
pending = asyncio.all_tasks(loop)
|
||||||
|
for task in pending:
|
||||||
|
task.cancel()
|
||||||
|
loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
|
||||||
|
|
||||||
loop.close()
|
loop.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue