From eeb95c56961578d705661e6506c98d5b6cdba996 Mon Sep 17 00:00:00 2001 From: Théo de la Hogue Date: Wed, 3 Apr 2024 11:17:26 +0200 Subject: Adding new __main__.py script to load ArContext as module function. --- docs/user_guide/utils/demonstrations_scripts.md | 6 +- docs/user_guide/utils/ready-made_scripts.md | 6 +- src/argaze/__main__.py | 73 ++++++++++++++++++++++ src/argaze/utils/context_run.py | 82 ------------------------- 4 files changed, 79 insertions(+), 88 deletions(-) create mode 100644 src/argaze/__main__.py delete mode 100644 src/argaze/utils/context_run.py diff --git a/docs/user_guide/utils/demonstrations_scripts.md b/docs/user_guide/utils/demonstrations_scripts.md index 026bbef..a162a49 100644 --- a/docs/user_guide/utils/demonstrations_scripts.md +++ b/docs/user_guide/utils/demonstrations_scripts.md @@ -14,7 +14,7 @@ Collection of command-line scripts for demonstration purpose. Load ArFrame with a single ArLayer from **demo_gaze_analysis_setup.json** file then, simulate gaze position using mouse pointer to illustrate gaze features. ```shell -python ./src/argaze/utils/context_run.py ./src/argaze/utils/demo/opencv_window_context_setup.json +python -m argaze ./src/argaze/utils/demo/opencv_window_context_setup.json ``` ## Tobii Pro Glasses 2 @@ -53,7 +53,7 @@ Edit **tobii_live_stream_context_setup.json** file as below with your own parame Then, execute this command: ```shell -python ./src/argaze/utils/context_run.py ./src/argaze/utils/demo/tobii_live_stream_context_setup.json +python -m argaze ./src/argaze/utils/demo/tobii_live_stream_context_setup.json ``` ### Tobii post-processing context @@ -80,5 +80,5 @@ Edit **tobii_post_processing_context_setup.json** file as below with your own pa Then, execute this command: ```shell -python ./src/argaze/utils/context_run.py ./src/argaze/utils/demo/tobii_post_processing_context_setup.json +python -m argaze ./src/argaze/utils/demo/tobii_post_processing_context_setup.json ``` diff --git a/docs/user_guide/utils/ready-made_scripts.md b/docs/user_guide/utils/ready-made_scripts.md index 4767969..92a4502 100644 --- a/docs/user_guide/utils/ready-made_scripts.md +++ b/docs/user_guide/utils/ready-made_scripts.md @@ -9,12 +9,12 @@ Collection of command-line scripts to provide useful features. !!! note *Use -h option to get command arguments documentation.* -## ArGaze context handler +## ArContext handler -Load and execute any ArGaze context from a JSON CONFIGURATION file. +Load and execute any ArContext from a JSON CONFIGURATION file ```shell -python ./src/argaze/utils/context_run.py CONFIGURATION +python -m argaze CONFIGURATION ``` ## ArUco markers group exporter diff --git a/src/argaze/__main__.py b/src/argaze/__main__.py new file mode 100644 index 0000000..0682583 --- /dev/null +++ b/src/argaze/__main__.py @@ -0,0 +1,73 @@ +"""Load and execute ArContext configuration. + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +__author__ = "Théo de la Hogue" +__credits__ = [] +__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)" +__license__ = "GPLv3" + +import argparse +import logging +import contextlib + +from .DataFeatures import from_json +from .ArFeatures import ArCamera + +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') + +args = parser.parse_args() + +# Manage logging +logging.basicConfig(format = '%(levelname)s: %(message)s', level = logging.DEBUG if args.verbose else logging.INFO) + +# Load context from JSON file +with from_json(args.context_file) as context: + + 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): + + # Visualisation loop + while True: + + # Display context + cv2.imshow(context.name, context.image()) + + # Head-monted eye tracker case: display environment frames image + if issubclass(type(context.pipeline), ArCamera): + + for scene_frame in context.pipeline.scene_frames(): + + cv2.imshow(scene_frame.name, scene_frame.image()) + + # Key interaction + key_pressed = cv2.waitKey(10) + + # Esc: close window + if key_pressed == 27: + + raise KeyboardInterrupt() + + # Stop frame display + cv2.destroyAllWindows() diff --git a/src/argaze/utils/context_run.py b/src/argaze/utils/context_run.py deleted file mode 100644 index ace7c54..0000000 --- a/src/argaze/utils/context_run.py +++ /dev/null @@ -1,82 +0,0 @@ -#!/usr/bin/env python - -"""Load and execute eyetracker pipeline. - -This program is free software: you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation, either version 3 of the License, or (at your option) any later -version. -This program is distributed in the hope that it will be useful, but WITHOUT -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. -You should have received a copy of the GNU General Public License along with -this program. If not, see . -""" - -__author__ = "Théo de la Hogue" -__credits__ = [] -__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)" -__license__ = "GPLv3" - -import argparse -import logging -import contextlib - -from argaze import DataFeatures, ArFeatures - -import cv2 - -# Manage arguments -parser = argparse.ArgumentParser(description=__doc__.split('-')[0]) -parser.add_argument('configuration', metavar='CONFIGURATION', type=str, help='JSON configuration filepath') -parser.add_argument('-p', '--patch', metavar='PATCH', type=str, help='JSON configuration patch filepath') -parser.add_argument('-v', '--verbose', action='store_true', default=False, help='enable verbose mode to print information in console') -parser.add_argument('-m', '--mouse', action='store_true', default=False, help='use mouse pointer as gaze position') - -args = parser.parse_args() - -# Manage logging -logging.basicConfig(format = '%(levelname)s: %(message)s', level = logging.DEBUG if args.verbose else logging.INFO) - -def main(): - - # Load ArGaze context - with DataFeatures.from_json(args.configuration, args.patch) as context: - - 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): - - # Visualisation loop - while True: - - # Display context - cv2.imshow(context.name, context.image()) - - # Head-monted eye tracker case: display environment frames image - if issubclass(type(context.pipeline), ArFeatures.ArCamera): - - for scene_frame in context.pipeline.scene_frames(): - - cv2.imshow(scene_frame.name, scene_frame.image()) - - # Key interaction - key_pressed = cv2.waitKey(10) - - # Esc: close window - if key_pressed == 27: - - raise KeyboardInterrupt() - - # Stop frame display - cv2.destroyAllWindows() - -if __name__ == '__main__': - - main() \ No newline at end of file -- cgit v1.1