aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/utils/demo_ar_features_run.py
blob: 39c69e745981def45e9977005fddf5978cd26cb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python

""" """

__author__ = "Théo de la Hogue"
__credits__ = []
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "BSD"

import argparse
import os
import time

from argaze import ArFeatures, GazeFeatures

import cv2
import numpy

def main():
    """
    Load AR environment from .json file, detect ArUco markers into camera device images and project it.
    """

    current_directory = os.path.dirname(os.path.abspath(__file__))

    # Manage arguments
    parser = argparse.ArgumentParser(description=main.__doc__.split('-')[0])
    parser.add_argument('environment', metavar='ENVIRONMENT', type=str, help='ar environment filepath')
    parser.add_argument('-d', '--device', metavar='DEVICE', type=int, default=0, help='video capture device id')
    args = parser.parse_args()

    # Load AR enviroment
    ar_environment = ArFeatures.ArEnvironment.from_json(args.environment)

    # Create a window to display AR environment
    cv2.namedWindow(ar_environment.name, cv2.WINDOW_AUTOSIZE)

    # Init timestamp
    start_time = time.time()

    # Fake gaze position with mouse pointer
    def on_mouse_event(event, x, y, flags, param):

        # Edit millisecond timestamp
        timestamp = int((time.time() - start_time) * 1e3)

        # Project gaze position into environment
        ar_environment.look(timestamp, GazeFeatures.GazePosition((x, y)))

    # Attach mouse callback to window
    cv2.setMouseCallback(ar_environment.name, on_mouse_event)

    # Enable camera video capture
    video_capture = cv2.VideoCapture(args.device)

    # Waiting for 'ctrl+C' interruption
    try:

        # Capture images
        while video_capture.isOpened():

            # Read video image
            success, video_image = video_capture.read()

            if success:

                # Try to detect and project environment
                try:

                    ar_environment.detect_and_project(video_image)

                # Catch errors
                except (ArFeatures.PoseEstimationFailed, ArFeatures.SceneProjectionFailed) as e:

                    cv2.rectangle(video_image, (0, 50), (700, 100), (127, 127, 127), -1)
                    cv2.putText(video_image, f'Error: {e}', (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 1, cv2.LINE_AA)
            
                # Draw environment
                cv2.imshow(ar_environment.name, ar_environment.image)

                # Draw each screens
                for scene_name, screen_name, screen in ar_environment.screens:

                    image = screen.background.copy()
                    
                    screen.aoi_2d_scene.draw(image, color=(255, 255, 255))
                    screen.current_gaze_position.draw(image, color=(255, 255, 255))

                    screen.current_gaze_movement.draw(image, color=(0, 255, 255))
                    screen.current_gaze_movement.draw_positions(image)

                    # Check screen fixation
                    if GazeFeatures.is_fixation(screen.current_gaze_movement):

                        # Draw looked AOI
                        screen.aoi_2d_scene.draw_circlecast(image, screen.current_gaze_movement.focus, screen.current_gaze_movement.deviation_max, base_color=(0, 0, 0), matching_color=(255, 255, 255))

                    cv2.imshow(f'{scene_name}:{screen_name}', image)

            # Stop by pressing 'Esc' key
            if cv2.waitKey(10) == 27:
                break

    # Stop on 'ctrl+C' interruption
    except KeyboardInterrupt:
        pass

    # Close camera video capture
    video_capture.release()

    # Stop image display
    cv2.destroyAllWindows()

if __name__ == '__main__':

    main()