From 46a2f8306181ad2da9aafeadea276ede9f6640bc Mon Sep 17 00:00:00 2001 From: Théo de la Hogue Date: Mon, 11 Apr 2022 16:44:55 +0200 Subject: Renaming utils script --- src/argaze/utils/README.md | 2 +- .../utils/export_tobii_segment_aruco_markers.py | 123 --------------------- .../utils/export_tobii_segment_aruco_rois.py | 123 +++++++++++++++++++++ 3 files changed, 124 insertions(+), 124 deletions(-) delete mode 100644 src/argaze/utils/export_tobii_segment_aruco_markers.py create mode 100644 src/argaze/utils/export_tobii_segment_aruco_rois.py diff --git a/src/argaze/utils/README.md b/src/argaze/utils/README.md index 7f0e904..99b8717 100644 --- a/src/argaze/utils/README.md +++ b/src/argaze/utils/README.md @@ -75,7 +75,7 @@ python ./src/argaze/utils/export_tobii_segment_fixations.py -s SEGMENT_PATH - Track ArUco markers into a Tobii camera video segment (replace SEGMENT_PATH). Load an roi scene (replace ROI_SCENE) .obj file, position it virtually relatively to any detected ArUco markers and project the scene into camera frame. Then, detect if Tobii gaze point is inside any ROI. ``` -python ./src/argaze/utils/export_tobii_segment_aruco_markers.py -s SEGMENT_PATH -c export/tobii_camera.json -m 7.5 -r ROI_SCENE +python ./src/argaze/utils/export_tobii_segment_aruco_rois.py -s SEGMENT_PATH -c export/tobii_camera.json -m 7.5 -r ROI_SCENE ``` - Track ArUco markers into Tobii camera video stream (replace IP_ADDRESS). Load an roi scene (replace ROI_SCENE) .obj file, position it virtually relatively to any detected ArUco markers and project the scene into camera frame. Then, detect if Tobii gaze point is inside any ROI. diff --git a/src/argaze/utils/export_tobii_segment_aruco_markers.py b/src/argaze/utils/export_tobii_segment_aruco_markers.py deleted file mode 100644 index f9c4cb2..0000000 --- a/src/argaze/utils/export_tobii_segment_aruco_markers.py +++ /dev/null @@ -1,123 +0,0 @@ -#!/usr/bin/env python - -import argparse -import bisect - -from argaze import GazeFeatures -from argaze.TobiiGlassesPro2 import TobiiEntities, TobiiVideo -from argaze.ArUcoMarkers import * -from argaze.RegionOfInterest import * - -import numpy - -import cv2 as cv - -def main(): - """ - Replay Tobii segment video - """ - - # manage arguments - parser = argparse.ArgumentParser(description=main.__doc__.split('-')[0]) - parser.add_argument('-s', '--segment_path', metavar='SEGMENT_PATH', type=str, default=None, help='segment path') - parser.add_argument('-c', '--camera_calibration', metavar='CAM_CALIB', type=str, default='tobii_camera.json', help='json camera calibration filepath') - parser.add_argument('-r', '--roi_scene', metavar='ROI_SCENE', type=str, default='roi3D_scene.obj', help='obj roi scene filepath') - parser.add_argument('-d', '--dictionary', metavar='DICT', type=str, default='DICT_ARUCO_ORIGINAL', help='aruco marker dictionnary') - parser.add_argument('-m', '--marker_size', metavar='MKR', type=float, default=6, help='aruco marker size (cm)') - args = parser.parse_args() - - if args.segment_path != None: - - # Load a tobii segment - tobii_segment = TobiiEntities.TobiiSegment(args.segment_path) - - # Load a tobii segment video - tobii_segment_video = tobii_segment.load_video() - print(f'Video duration: {tobii_segment_video.get_duration()}, frame number: {tobii_segment_video.get_frame_number()}, width: {tobii_segment_video.get_width()}, height: {tobii_segment_video.get_height()}') - - # Load a tobii segment data - tobii_segment_data = tobii_segment.load_data() - print(f'Data keys: {tobii_segment_data.keys()}') - - # Access to timestamped gaze position data buffer - tobii_ts_gaze_positions = tobii_segment_data.gidx_l_gp - print(f'{len(tobii_ts_gaze_positions)} gaze positions loaded') - - # Create aruco camera - aruco_camera = ArUcoCamera.ArUcoCamera() - aruco_camera.load_calibration_file(args.camera_calibration) - - # Create aruco tracker - aruco_tracker = ArUcoTracker.ArUcoTracker(args.dictionary, args.marker_size, aruco_camera) - - # Create ROIs 3D scene - roi3D_scene = ROI3DScene.ROI3DScene() - roi3D_scene.load(args.roi_scene) - - # Video and data replay loop - try: - - # Iterate on video frames activating video / data synchronisation through vts data buffer - for video_ts, video_frame in tobii_segment_video.frames(tobii_segment_data.vts): - - try: - - # Get closest gaze position before video timestamp and remove all gaze positions before - closest_gaze_ts, closest_gaze_position = tobii_ts_gaze_positions.pop_first_until(video_ts) - - # Draw video synchronized gaze pointer - pointer = (int(closest_gaze_position.gp[0] * video_frame.width), int(closest_gaze_position.gp[1] * video_frame.height)) - cv.circle(video_frame.matrix, pointer, 4, (0, 255, 255), -1) - - # When expected values can't be found - except (KeyError, AttributeError, ValueError): - - pass # keep last pointer position - - # Track markers with pose estimation and draw them - aruco_tracker.track(video_frame.matrix) - aruco_tracker.draw(video_frame.matrix) - - # Project 3D scenes related to each aruco markers - if aruco_tracker.get_markers_number(): - - for (i, marker_id) in enumerate(aruco_tracker.get_markers_ids()): - - # TODO : Select different 3D scenes depending on aruco id - - marker_rotation = aruco_tracker.get_marker_rotation(i) - marker_translation = aruco_tracker.get_marker_translation(i) - - roi3D_scene.set_rotation(marker_rotation) - roi3D_scene.set_translation(marker_translation) - - # Edit Zero distorsion matrix - D0 = numpy.asarray([0.0, 0.0, 0.0, 0.0, 0.0]) - - # DON'T APPLY CAMERA DISTORSION : it projects points which are far from the frame into it - # This hack isn't realistic but as the gaze will mainly focus on centered ROI, where the distorsion is low, it is acceptable. - roi2D_scene = roi3D_scene.project(aruco_camera.get_K(), D0) - - # Check if gaze is inside 2D rois - if pointer != None: - roi2D_scene.inside(pointer) - - # Draw 2D rois - roi2D_scene.draw(video_frame.matrix) - - # Close window using 'Esc' key - if cv.waitKey(1) == 27: - break - - cv.imshow(f'Segment {tobii_segment.get_id()} video', video_frame.matrix) - - # Exit on 'ctrl+C' interruption - except KeyboardInterrupt: - pass - - # Stop frame display - cv.destroyAllWindows() - -if __name__ == '__main__': - - main() \ No newline at end of file diff --git a/src/argaze/utils/export_tobii_segment_aruco_rois.py b/src/argaze/utils/export_tobii_segment_aruco_rois.py new file mode 100644 index 0000000..f9c4cb2 --- /dev/null +++ b/src/argaze/utils/export_tobii_segment_aruco_rois.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python + +import argparse +import bisect + +from argaze import GazeFeatures +from argaze.TobiiGlassesPro2 import TobiiEntities, TobiiVideo +from argaze.ArUcoMarkers import * +from argaze.RegionOfInterest import * + +import numpy + +import cv2 as cv + +def main(): + """ + Replay Tobii segment video + """ + + # manage arguments + parser = argparse.ArgumentParser(description=main.__doc__.split('-')[0]) + parser.add_argument('-s', '--segment_path', metavar='SEGMENT_PATH', type=str, default=None, help='segment path') + parser.add_argument('-c', '--camera_calibration', metavar='CAM_CALIB', type=str, default='tobii_camera.json', help='json camera calibration filepath') + parser.add_argument('-r', '--roi_scene', metavar='ROI_SCENE', type=str, default='roi3D_scene.obj', help='obj roi scene filepath') + parser.add_argument('-d', '--dictionary', metavar='DICT', type=str, default='DICT_ARUCO_ORIGINAL', help='aruco marker dictionnary') + parser.add_argument('-m', '--marker_size', metavar='MKR', type=float, default=6, help='aruco marker size (cm)') + args = parser.parse_args() + + if args.segment_path != None: + + # Load a tobii segment + tobii_segment = TobiiEntities.TobiiSegment(args.segment_path) + + # Load a tobii segment video + tobii_segment_video = tobii_segment.load_video() + print(f'Video duration: {tobii_segment_video.get_duration()}, frame number: {tobii_segment_video.get_frame_number()}, width: {tobii_segment_video.get_width()}, height: {tobii_segment_video.get_height()}') + + # Load a tobii segment data + tobii_segment_data = tobii_segment.load_data() + print(f'Data keys: {tobii_segment_data.keys()}') + + # Access to timestamped gaze position data buffer + tobii_ts_gaze_positions = tobii_segment_data.gidx_l_gp + print(f'{len(tobii_ts_gaze_positions)} gaze positions loaded') + + # Create aruco camera + aruco_camera = ArUcoCamera.ArUcoCamera() + aruco_camera.load_calibration_file(args.camera_calibration) + + # Create aruco tracker + aruco_tracker = ArUcoTracker.ArUcoTracker(args.dictionary, args.marker_size, aruco_camera) + + # Create ROIs 3D scene + roi3D_scene = ROI3DScene.ROI3DScene() + roi3D_scene.load(args.roi_scene) + + # Video and data replay loop + try: + + # Iterate on video frames activating video / data synchronisation through vts data buffer + for video_ts, video_frame in tobii_segment_video.frames(tobii_segment_data.vts): + + try: + + # Get closest gaze position before video timestamp and remove all gaze positions before + closest_gaze_ts, closest_gaze_position = tobii_ts_gaze_positions.pop_first_until(video_ts) + + # Draw video synchronized gaze pointer + pointer = (int(closest_gaze_position.gp[0] * video_frame.width), int(closest_gaze_position.gp[1] * video_frame.height)) + cv.circle(video_frame.matrix, pointer, 4, (0, 255, 255), -1) + + # When expected values can't be found + except (KeyError, AttributeError, ValueError): + + pass # keep last pointer position + + # Track markers with pose estimation and draw them + aruco_tracker.track(video_frame.matrix) + aruco_tracker.draw(video_frame.matrix) + + # Project 3D scenes related to each aruco markers + if aruco_tracker.get_markers_number(): + + for (i, marker_id) in enumerate(aruco_tracker.get_markers_ids()): + + # TODO : Select different 3D scenes depending on aruco id + + marker_rotation = aruco_tracker.get_marker_rotation(i) + marker_translation = aruco_tracker.get_marker_translation(i) + + roi3D_scene.set_rotation(marker_rotation) + roi3D_scene.set_translation(marker_translation) + + # Edit Zero distorsion matrix + D0 = numpy.asarray([0.0, 0.0, 0.0, 0.0, 0.0]) + + # DON'T APPLY CAMERA DISTORSION : it projects points which are far from the frame into it + # This hack isn't realistic but as the gaze will mainly focus on centered ROI, where the distorsion is low, it is acceptable. + roi2D_scene = roi3D_scene.project(aruco_camera.get_K(), D0) + + # Check if gaze is inside 2D rois + if pointer != None: + roi2D_scene.inside(pointer) + + # Draw 2D rois + roi2D_scene.draw(video_frame.matrix) + + # Close window using 'Esc' key + if cv.waitKey(1) == 27: + break + + cv.imshow(f'Segment {tobii_segment.get_id()} video', video_frame.matrix) + + # Exit on 'ctrl+C' interruption + except KeyboardInterrupt: + pass + + # Stop frame display + cv.destroyAllWindows() + +if __name__ == '__main__': + + main() \ No newline at end of file -- cgit v1.1