diff options
Diffstat (limited to 'src/argaze/__main__.py')
-rw-r--r-- | src/argaze/__main__.py | 58 |
1 files changed, 55 insertions, 3 deletions
diff --git a/src/argaze/__main__.py b/src/argaze/__main__.py index c80657e..9bcbe5a 100644 --- a/src/argaze/__main__.py +++ b/src/argaze/__main__.py @@ -18,7 +18,9 @@ __license__ = "GPLv3" import argparse import logging +import json import contextlib +import os from . import load from .ArFeatures import ArCamera, ArContext @@ -28,33 +30,64 @@ import cv2 # Manage arguments parser = argparse.ArgumentParser(description=__doc__.split('-')[0]) parser.add_argument('context_file', metavar='CONTEXT_FILE', type=str, help='JSON context filepath') -parser.add_argument('-v', '--verbose', action='store_true', default=False, - help='enable verbose mode to print information in console') +parser.add_argument('-v', '--verbose', action='store_true', default=False, help='enable verbose mode to print information in console') +parser.add_argument('-p', '--pipe_path', metavar='PIPE_PATH', type=str, default=None, help='enable pipe communication at given path to execute external commands') args = parser.parse_args() # Manage logging logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG if args.verbose else logging.INFO) +# Manage pipe communication +if args.pipe_path is not None: + + if not os.path.exists(args.pipe_path): + + os.mkfifo(args.pipe_path) + + # Open the fifo in non-blocking mode or it will stalls until someone opens it for writting + pipe_file = os.open(args.pipe_path, os.O_RDONLY | os.O_NONBLOCK) + + logging.info('%s pipe opened', args.pipe_path) + # Load context from JSON file with load(args.context_file) as context: # Loaded object must be a subclass of ArContext if not issubclass(type(context), ArContext): + raise TypeError('Loaded object is not a subclass of ArContext') if args.verbose: + print(context) # Create a window to display context cv2.namedWindow(context.name, cv2.WINDOW_AUTOSIZE) # Waiting for 'ctrl+C' interruption - with contextlib.suppress(KeyboardInterrupt): + with contextlib.suppress(KeyboardInterrupt), os.fdopen(pipe_file) if args.pipe_path is not None else contextlib.nullcontext() as pipe: # Visualization loop while True: + # Read message from pipe if required + if args.pipe_path is not None: + + message = pipe.read().rstrip('\n') + + if message: + + logging.info('%s: %s', args.pipe_path, message) + + try: + + exec(message) + + except Exception as e: + + logging.error('%s', e) + # Display context cv2.imshow(context.name, context.image()) @@ -62,6 +95,7 @@ with load(args.context_file) as context: if issubclass(type(context.pipeline), ArCamera): for scene_frame in context.pipeline.scene_frames(): + cv2.imshow(scene_frame.name, scene_frame.image()) # Key interaction @@ -69,7 +103,25 @@ with load(args.context_file) as context: # Esc: close window if key_pressed == 27: + raise KeyboardInterrupt() + # Space bar: pause/resume pipeline processing + if key_pressed == 32: + + try: + + if context.is_paused(): + + context.resume() + + else: + + context.pause() + + except NotImplementedError: + + pass + # Stop frame display cv2.destroyAllWindows() |