aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/utils/demo_ar_features_run.py
blob: d7854e3df6808e93e9b3f020e61fcbfa5aa94aec (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
117
#!/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
        for scene_name, scene_looking_data in ar_environment.look(timestamp, GazeFeatures.GazePosition((x, y))):

            for frame_name, frame_looking_data in scene_looking_data:

                # Do nothing with frame looking data
                pass

    # 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
                ar_environment.draw(video_image)

                # Display environment
                cv2.imshow(ar_environment.name, video_image)

                # Draw and display each frames in separate window
                for scene_name, frame_name, frame in ar_environment.frames:

                    # Create frame image 
                    frame_image = frame.image

                    # Draw frame info
                    frame.draw(frame_image)

                    # Display frame
                    cv2.imshow(f'{scene_name}:{frame_name}', frame_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()