diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/argaze/ArUcoMarkers/ArUcoCube.py | 401 | ||||
-rw-r--r-- | src/argaze/ArUcoMarkers/ArUcoMarker.py | 48 | ||||
-rw-r--r-- | src/argaze/ArUcoMarkers/ArUcoMarkersDictionary.py | 12 | ||||
-rw-r--r-- | src/argaze/ArUcoMarkers/ArUcoTracker.py | 229 | ||||
-rw-r--r-- | src/argaze/ArUcoMarkers/__init__.py | 2 | ||||
-rw-r--r-- | src/argaze/utils/tobii_stream_arcube_display.py | 390 |
6 files changed, 901 insertions, 181 deletions
diff --git a/src/argaze/ArUcoMarkers/ArUcoCube.py b/src/argaze/ArUcoMarkers/ArUcoCube.py new file mode 100644 index 0000000..e2983df --- /dev/null +++ b/src/argaze/ArUcoMarkers/ArUcoCube.py @@ -0,0 +1,401 @@ +#!/usr/bin/env python + +from dataclasses import dataclass, field +import json +import math +import itertools + +from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoMarker + +import numpy +import cv2 as cv +import cv2.aruco as aruco + +@dataclass +class ArUcoCubeFace(): + """Define cube face pose and marker.""" + + translation: numpy.array + """Position in cube referential.""" + + rotation: numpy.array + """Rotation in cube referential.""" + + marker: dict + """ArUco marker linked to the face """ + +@dataclass +class ArUcoCube(): + """Define a cube with ArUco markers on each face and estimate its pose.""" + + dictionary: ArUcoMarkersDictionary.ArUcoMarkersDictionary + """ArUco dictionary of cube markers.""" + + marker_size: int = field(init=False) + """Size of markers in centimeter.""" + + edge_size: int = field(init=False) + """Size of the cube edges in centimeter.""" + + faces: dict = field(init=False, default_factory=dict) + """All named faces of the cube and their ArUco markers.""" + + angle_tolerance: float = field(init=False) + """Angle error tolerance allowed to validate face pose in degree.""" + + distance_tolerance: float = field(init=False) + """Distance error tolerance allowed to validate face pose in centimeter.""" + + def __init__(self, configuration_filepath): + """Define cube from a .json file.""" + + with open(configuration_filepath) as configuration_file: + + # Deserialize .json + # TODO find a better way + configuration = json.load(configuration_file) + + # Load dictionary + self.dictionary = ArUcoMarkersDictionary.ArUcoMarkersDictionary(configuration['dictionary']) + + # Load marker size + self.marker_size = configuration['marker_size'] + + # Load edge size + self.edge_size = configuration['edge_size'] + + # Load faces + self.faces = {} + for name, face in configuration['faces'].items(): + marker = ArUcoMarker.ArUcoMarker(self.dictionary, face['marker'], self.marker_size) + self.faces[name] = ArUcoCubeFace(numpy.array(face['translation']).astype(numpy.float32), numpy.array(face['rotation']).astype(numpy.float32), marker) + + # Load angle tolerance + self.angle_tolerance = configuration['angle_tolerance'] + + # Load distance tolerance + self.distance_tolerance = configuration['distance_tolerance'] + + # Init pose data + self.__translation = numpy.zeros(3) + self.__rotation = numpy.zeros(3) + self.__succeded = False + self.__validity = 0 + + # Process markers ids to speed up further calculations + self.__identifier_cache = {} + for name, face in self.faces.items(): + self.__identifier_cache[face.marker.identifier] = name + + # Process each face pose to speed up further calculations + self.__translation_cache = {} + for name, face in self.faces.items(): + self.__translation_cache[name] = face.translation * self.edge_size / 2 + + # Process each face rotation matrix to speed up further calculations + self.__rotation_cache = {} + for name, face in self.faces.items(): + + # Create intrinsic rotation matrix + R = self.__make_rotation_matrix(*face.rotation) + + assert(self.__is_rotation_matrix(R)) + + # Store rotation matrix + self.__rotation_cache[name] = R + + # Process each axis-angle face combination to speed up further calculations + self.__angle_cache = {} + for (A_name, A_face), (B_name, B_face) in itertools.combinations(self.faces.items(), 2): + + A = self.__rotation_cache[A_name] + B = self.__rotation_cache[B_name] + + # Rotation matrix from A face to B face + AB = B.dot(A.T) + + assert(self.__is_rotation_matrix(AB)) + + # Calculate axis-angle representation of AB rotation matrix + angle = numpy.rad2deg(numpy.arccos((numpy.trace(AB) - 1) / 2)) + + try: + self.__angle_cache[A_name][B_name] = angle + except: + self.__angle_cache[A_name] = {B_name: angle} + + try: + self.__angle_cache[B_name][A_name] = angle + except: + self.__angle_cache[B_name] = {A_name: angle} + + # Process distance between face combination to speed up further calculations + self.__distance_cache = numpy.linalg.norm(numpy.array([0, 0, self.edge_size/2]) - numpy.array([0, self.edge_size/2, 0])) + + def print_cache(self): + """Print pre-processed data.""" + + print('\nIdentifier cache:') + for i, name in self.__identifier_cache.items(): + print(f'- {i}: {name}') + + print('\nTranslation cache:') + for name, item in self.__translation_cache.items(): + print(f'- {name}: {item}') + + print('\nRotation cache:') + for name, item in self.__rotation_cache.items(): + print(f'- {name}:\n{item}') + + print('\nAngle cache:') + for A_name, A_angle_cache in self.__angle_cache.items(): + for B_name, angle in A_angle_cache.items(): + print(f'- {A_name}/{B_name}: {angle:3f}') + + print(f'\nDistance cache: {self.__distance_cache}') + + def __make_rotation_matrix(self, x, y, z): + + # Create rotation matrix around x axis + c = numpy.cos(numpy.deg2rad(x)) + s = numpy.sin(numpy.deg2rad(x)) + Rx = numpy.array([[1, 0, 0], [0, c, -s], [0, s, c]]) + + # Create rotation matrix around y axis + c = numpy.cos(numpy.deg2rad(y)) + s = numpy.sin(numpy.deg2rad(y)) + Ry = numpy.array([[c, 0, s], [0, 1, 0], [-s, 0, c]]) + + # Create rotation matrix around z axis + c = numpy.cos(numpy.deg2rad(z)) + s = numpy.sin(numpy.deg2rad(z)) + Rz = numpy.array([[c, -s, 0], [s, c, 0], [0, 0, 1]]) + + # Return intrinsic rotation matrix + return Rx.dot(Ry.dot(Rz)) + + def __is_rotation_matrix(self, R): + """Checks if a matrix is a valid rotation matrix.""" + + I = numpy.identity(3, dtype = R.dtype) + return numpy.linalg.norm(I - numpy.dot(R.T, R)) < 1e-6 + + def __normalise_face_pose(self, name, face, F): + + # Transform face rotation into cube rotation vector + R = self.__rotation_cache[name] + rvec, _ = cv.Rodrigues(F.dot(R)) + + #print(f'{name} rotation vector: {rvec[0][0]:3f} {rvec[1][0]:3f} {rvec[2][0]:3f}') + + # Transform face translation into cube translation vector + OF = face.translation + T = self.__translation_cache[name] + FC = F.dot(R.dot(T)) + + tvec = OF + FC + + #print(f'{name} translation vector: {tvec[0]:3f} {tvec[1]:3f} {tvec[2]:3f}') + + return rvec, tvec + + def estimate_pose(self, tracked_markers): + + # Init pose data + self.__translation = numpy.zeros(3) + self.__rotation = numpy.zeros(3) + self.__succeded = False + self.__validity = 0 + + # Don't try to estimate pose if there is no tracked markers + if len(tracked_markers) == 0: + + return self.get_pose() + + # Look for faces related to tracked markers + tracked_faces = {} + for (marker_id, marker) in tracked_markers.items(): + + try: + name = self.__identifier_cache[marker_id] + tracked_faces[name] = marker + + except KeyError: + continue + + #print('-------------- ArUcoCube pose estimation --------------') + + # Pose validity checking is'nt possible when only one face of the cube is tracked + if len(tracked_faces.keys()) == 1: + + # Get arcube pose from to the unique face pose + name, face = tracked_faces.popitem() + F, _ = cv.Rodrigues(face.rotation) + + self.__rotation, self.__translation = self.__normalise_face_pose(name,face, F) + self.__succeded = True + self.__validity = 1 + + #print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') + #print(f'arcube rotation vector: {self.__rotation[0][0]:3f} {self.__rotation[1][0]:3f} {self.__rotation[2][0]:3f}') + #print(f'arcube translation vector: {self.__translation[0]:3f} {self.__translation[1]:3f} {self.__translation[2]:3f}') + + # Pose validity checking processes faces two by two + else: + + valid_faces = [] + valid_rvecs = [] + valid_tvecs = [] + + for (A_name, A_face), (B_name, B_face) in itertools.combinations(tracked_faces.items(), 2): + + #print(f'** {A_name} > {B_name}') + + # Get face rotation estimation + # Use rotation matrix instead of rotation vector + A, _ = cv.Rodrigues(A_face.rotation) + B, _ = cv.Rodrigues(B_face.rotation) + + # Rotation matrix from A face to B face + AB = B.dot(A.T) + + assert(self.__is_rotation_matrix(AB)) + + # Calculate axis-angles representation of AB rotation matrix + angle = numpy.rad2deg(numpy.arccos((numpy.trace(AB) - 1) / 2)) + + #print('rotation angle:') + #print(angle) + + expected_angle = self.__angle_cache[A_name][B_name] + + #print('expected angle:') + #print(expected_angle) + + # Calculate distance between A face center and B face center + distance = numpy.linalg.norm(A_face.translation - B_face.translation) + expected_distance = self.__distance_cache + + # Check angle and distance according given tolerance then normalise face pose + valid_angle = math.isclose(angle, expected_angle, abs_tol=self.angle_tolerance) + valid_distance = math.isclose(distance, expected_distance, abs_tol=self.distance_tolerance) + + if valid_angle and valid_distance: + + if A_name not in valid_faces: + + # Remember this face is already validated + valid_faces.append(A_name) + + rvec, tvec = self.__normalise_face_pose(A_name, A_face, A) + + # Store normalised face pose + valid_rvecs.append(rvec) + valid_tvecs.append(tvec) + + if B_name not in valid_faces: + + # Remember this face is already validated + valid_faces.append(B_name) + + rvec, tvec = self.__normalise_face_pose(B_name, B_face, B) + + # Store normalised face pose + valid_rvecs.append(rvec) + valid_tvecs.append(tvec) + + if len(valid_faces) > 1: + + # Consider arcube rotation as the mean of all valid translations + # !!! WARNING !!! This is a bad hack : processing rotations average is a very complex problem that needs to well define the distance calculation method before. + self.__rotation = numpy.mean(numpy.array(valid_rvecs), axis=0) + + # Consider arcube translation as the mean of all valid translations + self.__translation = numpy.mean(numpy.array(valid_tvecs), axis=0) + + #print(':::::::::::::::::::::::::::::::::::::::::::::::::::') + #print(f'arcube rotation vector: {self.__rotation[0][0]:3f} {self.__rotation[1][0]:3f} {self.__rotation[2][0]:3f}') + #print(f'arcube translation vector: {self.__translation[0]:3f} {self.__translation[1]:3f} {self.__translation[2]:3f}') + + self.__succeded = True + self.__validity = len(valid_faces) + + #print('----------------------------------------------------') + + return self.get_pose() + + def get_pose(self): + + return self.__translation, self.__rotation, self.__succeded, self.__validity + + def set_pose(self, tvec = numpy.array([]), rvec = numpy.array([])): + + if tvec.size == 3: + self.__translation = tvec + + if rvec.size == 3: + self.__rotation = rvec + + self.__succeded = True + self.__validity = 0 + + def draw(self, frame, K, D): + + l = self.edge_size / 2 + ll = self.edge_size + + # Select color according validity score + n = 95 * self.__validity if self.__validity < 2 else 0 + f = 159 * self.__validity if self.__validity < 2 else 255 + + try: + + # Draw axis + axisPoints = numpy.float32([[ll, 0, 0], [0, ll, 0], [0, 0, ll], [0, 0, 0]]).reshape(-1, 3) + axisPoints, _ = cv.projectPoints(axisPoints, self.__rotation, self.__translation, K, D) + axisPoints = axisPoints.astype(int) + + frame = cv.line(frame, tuple(axisPoints[3].ravel()), tuple(axisPoints[0].ravel()), (n,n,f), 5) # X (red) + frame = cv.line(frame, tuple(axisPoints[3].ravel()), tuple(axisPoints[1].ravel()), (n,f,n), 5) # Y (green) + frame = cv.line(frame, tuple(axisPoints[3].ravel()), tuple(axisPoints[2].ravel()), (f,n,n), 5) # Z (blue) + + # Draw left face + leftPoints = numpy.float32([[-l, l, l], [-l, -l, l], [-l, -l, -l], [-l, l, -l]]).reshape(-1, 3) + leftPoints, _ = cv.projectPoints(leftPoints, self.__rotation, self.__translation, K, D) + leftPoints = leftPoints.astype(int) + + frame = cv.line(frame, tuple(leftPoints[0].ravel()), tuple(leftPoints[1].ravel()), (n,n,f), 2) + frame = cv.line(frame, tuple(leftPoints[1].ravel()), tuple(leftPoints[2].ravel()), (n,n,f), 2) + frame = cv.line(frame, tuple(leftPoints[2].ravel()), tuple(leftPoints[3].ravel()), (n,n,f), 2) + frame = cv.line(frame, tuple(leftPoints[3].ravel()), tuple(leftPoints[0].ravel()), (n,n,f), 2) + + # Draw top face + topPoints = numpy.float32([[l, l, l], [-l, l, l], [-l, l, -l], [l, l, -l]]).reshape(-1, 3) + topPoints, _ = cv.projectPoints(topPoints, self.__rotation, self.__translation, K, D) + topPoints = topPoints.astype(int) + + frame = cv.line(frame, tuple(topPoints[0].ravel()), tuple(topPoints[1].ravel()), (n,f,n), 2) + frame = cv.line(frame, tuple(topPoints[1].ravel()), tuple(topPoints[2].ravel()), (n,f,n), 2) + frame = cv.line(frame, tuple(topPoints[2].ravel()), tuple(topPoints[3].ravel()), (n,f,n), 2) + frame = cv.line(frame, tuple(topPoints[3].ravel()), tuple(topPoints[0].ravel()), (n,f,n), 2) + + # Draw front face + frontPoints = numpy.float32([[l, l, l], [-l, l, l], [-l, -l, l], [l, -l, l]]).reshape(-1, 3) + frontPoints, _ = cv.projectPoints(frontPoints, self.__rotation, self.__translation, K, D) + frontPoints = frontPoints.astype(int) + + frame = cv.line(frame, tuple(frontPoints[0].ravel()), tuple(frontPoints[1].ravel()), (f,n,n), 2) + frame = cv.line(frame, tuple(frontPoints[1].ravel()), tuple(frontPoints[2].ravel()), (f,n,n), 2) + frame = cv.line(frame, tuple(frontPoints[2].ravel()), tuple(frontPoints[3].ravel()), (f,n,n), 2) + frame = cv.line(frame, tuple(frontPoints[3].ravel()), tuple(frontPoints[0].ravel()), (f,n,n), 2) + + except Exception as e: + + print(e) + print(self.__translation) + print(self.__rotation) + print(self.__succeded) + print(self.__validity) + print(axisPoints) + + return frame diff --git a/src/argaze/ArUcoMarkers/ArUcoMarker.py b/src/argaze/ArUcoMarkers/ArUcoMarker.py new file mode 100644 index 0000000..4f716bb --- /dev/null +++ b/src/argaze/ArUcoMarkers/ArUcoMarker.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python + +from dataclasses import dataclass, field + +from argaze.ArUcoMarkers import ArUcoMarkersDictionary + +import numpy +import cv2 as cv +import cv2.aruco as aruco + +@dataclass +class ArUcoMarker(): + """Define ArUco marker class.""" + + dictionary: ArUcoMarkersDictionary.ArUcoMarkersDictionary + """ """ + + identifier: int + """ """ + + size: float + """Size of marker in centimeters.""" + + corners: numpy.array = field(init=False, repr=False) + """Estimated 2D corner positions in camera image referential.""" + + translation: numpy.array = field(init=False, repr=False) + """Estimated 3D center position in camera referential.""" + + rotation: numpy.array = field(init=False, repr=False) + """Estimated 3D marker rotation in camera referential.""" + + points: numpy.array = field(init=False, repr=False) + """Estimated 3D corners positions in camera referential.""" + + def center(self, i): + """Get 2D center position in camera image referential.""" + return self.corners[0].mean(axis=0) + + def draw(self, frame, K, D): + """Draw marker in frame.""" + + # Draw marker axis if pose has been estimated + if self.translation.size == 3 and self.rotation.size == 3: + + cv.drawFrameAxes(frame, K, D, self.rotation, self.translation, self.size) + + aruco.drawDetectedMarkers(frame, [self.corners], numpy.array([self.identifier]))
\ No newline at end of file diff --git a/src/argaze/ArUcoMarkers/ArUcoMarkersDictionary.py b/src/argaze/ArUcoMarkers/ArUcoMarkersDictionary.py index 58b55b8..84a78b9 100644 --- a/src/argaze/ArUcoMarkers/ArUcoMarkersDictionary.py +++ b/src/argaze/ArUcoMarkers/ArUcoMarkersDictionary.py @@ -32,12 +32,14 @@ all_aruco_markers_dictionaries = { class ArUcoMarkersDictionary(): """Handle an ArUco markers dictionary.""" - def __init__(self, aruco_dictionary_name): + def __init__(self, name): - if all_aruco_markers_dictionaries.get(aruco_dictionary_name, None) is None: - raise NameError(f'Bad ArUco markers dictionary name: {aruco_dictionary_name}') + if all_aruco_markers_dictionaries.get(name, None) is None: + raise NameError(f'Bad ArUco markers dictionary name: {name}') - dict_name_split = aruco_dictionary_name.split('_') + self.name = name + + dict_name_split = name.split('_') self.__format = dict_name_split[1] @@ -77,7 +79,7 @@ class ArUcoMarkersDictionary(): self.__number = int(dict_name_split[2]) - self.__aruco_dict = aruco.Dictionary_get(all_aruco_markers_dictionaries[aruco_dictionary_name]) + self.__aruco_dict = aruco.Dictionary_get(all_aruco_markers_dictionaries[self.name]) def create_marker(self, i, dpi=300): """Create a marker image.""" diff --git a/src/argaze/ArUcoMarkers/ArUcoTracker.py b/src/argaze/ArUcoMarkers/ArUcoTracker.py index b887812..f5b1715 100644 --- a/src/argaze/ArUcoMarkers/ArUcoTracker.py +++ b/src/argaze/ArUcoMarkers/ArUcoTracker.py @@ -3,7 +3,7 @@ import json from collections import Counter -from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoCamera +from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoMarker, ArUcoCamera import numpy import cv2 as cv @@ -45,14 +45,14 @@ ArUcoTrackerParameters = [ class ArUcoTracker(): """Track ArUco markers into a frame.""" - def __init__(self, aruco_dictionary_name: str, marker_length: float, camera: ArUcoCamera.ArUcoCamera): + def __init__(self, dictionary: ArUcoMarkersDictionary.ArUcoMarkersDictionary, marker_size: float, camera: ArUcoCamera.ArUcoCamera): """Define which markers library to track and their size""" # load ArUco markers dictionary - self.__aruco_dict = ArUcoMarkersDictionary.ArUcoMarkersDictionary(aruco_dictionary_name) + self.__dictionary = dictionary # define marker length in centimeter - self.__marker_length = marker_length + self.__marker_size = marker_size # define camera self.__camera = camera @@ -62,19 +62,8 @@ class ArUcoTracker(): self.__detector_parameters.cornerRefinementMethod = aruco.CORNER_REFINE_CONTOUR # to get a better pose estimation self.__detector_parameters_loaded = {} - # define tracked markers data - self.__markers_corners = [] - self.__markers_ids = [] - self.__rvecs = [] - self.__tvecs = [] - self.__points = [] - - # define rejected markers data - self.__rejected_markers_corners = [] - self.__rejected_markers_ids = [] - self.__rejected_rvecs = [] - self.__rejected_tvecs = [] - self.__rejected_points = [] + # init tracked markers data + self.__tracked_markers = {} # define tracked board data self.__board = None @@ -84,7 +73,7 @@ class ArUcoTracker(): # define track metrics data self.__track_count = 0 - self.__tracked_markers = [] + self.__tracked_ids = [] self.__rejected_markers = [] def load_configuration_file(self, configuration_filepath): @@ -115,98 +104,56 @@ class ArUcoTracker(): def track(self, frame, estimate_pose = True, check_rotation = False): """Track ArUco markers in frame.""" + self.__tracked_markers = {} + markers_corners, markers_ids, markers_rvecs, markers_tvecs, markers_points = [], [], [], [], [] + # DON'T MIRROR FRAME : it makes the markers detection to fail - # detect markers from gray picture - gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) - self.__markers_corners, self.__markers_ids, rejectedPoints = aruco.detectMarkers(gray, self.__aruco_dict.get_markers(), parameters = self.__detector_parameters) - self.__rejected_markers_corners, __rejected_markers_ids = [], [] - - if len(self.__markers_corners) > 0 and estimate_pose: - - # markers pose estimation - self.__rvecs, self.__tvecs, self.__points = aruco.estimatePoseSingleMarkers(self.__markers_corners, self.__marker_length, self.__camera.get_K(), self.__camera.get_D()) - - # optional: check marker rotation as described in [this issue](https://github.com/opencv/opencv/issues/8813) - if check_rotation: - - valid_rotation_markers = [] - bad_rotation_markers = [] - for i, rvec in enumerate(self.__rvecs): - - tvec = self.__tvecs[i][0] - R, _ = cv.Rodrigues(rvec) - - zAxisPoint = (tvec.dot(R) + numpy.array([0., 0., 1.])).dot(R.T) - zAxis = zAxisPoint - tvec - - # TODO: How to describe the expected Z axis orientation ? - # In some situations, you can't provide such information. - - # !!! Here a description specific to SimOne cockpit !!! - zAxisExpectedDict = { - 1: numpy.array([0.5, 0.5, -1]), - 2: numpy.array([0.5, 0.5, -1]), - 3: numpy.array([1, -1, -1]), - 4: numpy.array([1, -1, -1]), - 5: numpy.array([1, -1, -1]), - 6: numpy.array([1, 1, -1]), - 7: numpy.array([1, 1, -1]), - 8: numpy.array([1, -1, -1]) - } - - zAxisExpected = zAxisExpectedDict[self.__markers_ids[i][0]] - - cosine_angle = numpy.dot(zAxis/numpy.linalg.norm(zAxis), zAxisExpected) - degree_angle = numpy.rad2deg(numpy.arccos(cosine_angle)) - ''' - print(self.__markers_ids[i][0]) - print('marker position: ', tvec) - print('zAxisPoint: ', zAxisPoint) - print('zAxis: ', zAxis) - print('zAxisExpected: ', zAxisExpected) - print('cosine_angle: ', cosine_angle) - print('degree_angle: ', degree_angle) - ''' - # Is the marker oriented as expected ? - if cosine_angle < 0 or cosine_angle > 1: - - #print('valid') - valid_rotation_markers.append(i) - - else: - - #print('bad') - bad_rotation_markers.append(i) - - # update track metrics - self.__rejected_markers.append(self.__markers_ids[i][0]) - - # keep markers with bad rotation - self.__rejected_markers_corners = tuple([self.__markers_corners[i] for i in bad_rotation_markers]) - self.__rejected_markers_ids = self.__markers_ids[bad_rotation_markers] - self.__rejected_rvecs = self.__rvecs[bad_rotation_markers] - self.__rejected_tvecs = self.__tvecs[bad_rotation_markers] - self.__rejected_points = self.__points[bad_rotation_markers] - - # keep markers with valid rotation - self.__markers_corners = tuple([self.__markers_corners[i] for i in valid_rotation_markers]) - self.__markers_ids = self.__markers_ids[valid_rotation_markers] - self.__rvecs = self.__rvecs[valid_rotation_markers] - self.__tvecs = self.__tvecs[valid_rotation_markers] - self.__points = self.__points[valid_rotation_markers] + # Track markers into gray picture + markers_corners, markers_ids, _ = aruco.detectMarkers(cv.cvtColor(frame, cv.COLOR_BGR2GRAY), self.__dictionary.get_markers(), parameters = self.__detector_parameters) - else: + if len(markers_corners) > 0: + + # Pose estimation is optional + if estimate_pose: + + markers_rvecs, markers_tvecs, markers_points = aruco.estimatePoseSingleMarkers(markers_corners, self.__marker_size, self.__camera.get_K(), self.__camera.get_D()) + + # Gather tracked markers data and update metrics + self.__track_count += 1 + + for i, marker_id in enumerate(markers_ids.T[0]): + + marker = ArUcoMarker.ArUcoMarker(self.__dictionary, marker_id, self.__marker_size) - self.__rvecs = [] - self.__tvecs = [] - self.__points = [] + marker.corners = markers_corners[i] - # update track metrics - self.__track_count += 1 - for marker_id in self.get_markers_ids(): - self.__tracked_markers.append(marker_id) + if estimate_pose: + marker.translation = markers_tvecs[i][0] + marker.rotation = markers_rvecs[i] + marker.points = markers_points[i] + self.__tracked_markers[marker_id] = marker + + self.__tracked_ids.append(marker_id) + + def get_tracked_markers(self): + """Access to tracked markers dictionary.""" + + return self.__tracked_markers + + def get_tracked_markers_number(self): + """Return tracked markers number.""" + + return len(list(self.__tracked_markers.keys())) + + def draw_tracked_markers(self, frame): + """Draw traked markers.""" + + for marker_id, marker in self.__tracked_markers.items(): + + marker.draw(frame, self.__camera.get_K(), self.__camera.get_D()) + def track_board(self, frame, board, expected_markers_number): """Track ArUco markers board in frame setting up the number of detected markers needed to agree detection.""" @@ -214,7 +161,7 @@ class ArUcoTracker(): # detect markers from gray picture gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) - self.__markers_corners, self.__markers_ids, rejectedPoints = aruco.detectMarkers(gray, self.__aruco_dict.get_markers(), parameters = self.__detector_parameters) + self.__markers_corners, self.__markers_ids, rejectedPoints = aruco.detectMarkers(gray, self.__dictionary.get_markers(), parameters = self.__detector_parameters) # if all board markers are detected if self.get_markers_number() == expected_markers_number: @@ -229,33 +176,6 @@ class ArUcoTracker(): self.__board_corners = [] self.__board_corners_ids = [] - def draw(self, frame): - """Draw tracked markers in frame.""" - - # draw detected markers square - if len(self.__markers_corners) > 0: - - # draw marker axis if pose has been estimated - if len(self.__rvecs) > 0: - - for (i, marker_id) in enumerate(self.__markers_ids): - - cv.drawFrameAxes(frame, self.__camera.get_K(), self.__camera.get_D(), self.__rvecs[i], self.__tvecs[i], self.__marker_length) - - aruco.drawDetectedMarkers(frame, self.__markers_corners, self.__markers_ids) - - # draw rejected markers square - if len(self.__rejected_markers_corners) > 0: - - # draw marker axis if pose has been estimated - if len(self.__rejected_rvecs) > 0: - - for (i, marker_id) in enumerate(self.__rejected_markers_ids): - - cv.drawFrameAxes(frame, self.__camera.get_K(), self.__camera.get_D(), self.__rejected_rvecs[i], self.__rejected_tvecs[i], self.__marker_length) - - aruco.drawDetectedMarkers(frame, self.__rejected_markers_corners, self.__rejected_markers_ids, borderColor=(0, 255, 255)) - def draw_board(self, frame): """Draw tracked board corners in frame.""" @@ -266,52 +186,11 @@ class ArUcoTracker(): def reset_track_metrics(self): """Enable marker tracking metrics.""" self.__track_count = 0 - self.__tracked_markers = [] + self.__tracked_ids = [] def get_track_metrics(self): """Get marker tracking metrics.""" - return self.__track_count, Counter(self.__tracked_markers), Counter(self.__rejected_markers) - - def get_markers_dictionay(self): - """Get tracked aruco markers dictionary.""" - return self.__aruco_dict - - def get_markers_number(self): - """Get tracked markers number.""" - return len(self.__markers_corners) - - def get_markers_ids(self): - """Get tracked markers identifers.""" - if self.__markers_ids is not None: - return [i[0] for i in self.__markers_ids] - else: - return [] - - def get_marker_index(self, marker_id): - """Retreive marker index of a given merker id. - Raise ValueError if not found.""" - - return list(self.__markers_ids).index(marker_id) - - def get_marker_corners(self, i): - """Get marker i corners.""" - return self.__markers_corners[i][0] - - def get_marker_center(self, i): - """Get marker i center coordinates.""" - return self.__markers_corners[i][0].mean(axis=0) - - def get_marker_rotation(self, i): - """Get marker i rotation vector.""" - return self.__rvecs[i] - - def get_marker_translation(self, i): - """Get marker i translation vector.""" - return self.__tvecs[i] - - def get_marker_points(self, i): - """Get marker i points.""" - return self.__points[i] + return self.__track_count, Counter(self.__tracked_ids), Counter(self.__rejected_markers) def get_board_corners_number(self): """Get tracked board corners number.""" diff --git a/src/argaze/ArUcoMarkers/__init__.py b/src/argaze/ArUcoMarkers/__init__.py index 28386d2..3a74eeb 100644 --- a/src/argaze/ArUcoMarkers/__init__.py +++ b/src/argaze/ArUcoMarkers/__init__.py @@ -2,4 +2,4 @@ .. include:: README.md """ __docformat__ = "restructuredtext" -__all__ = ['ArUcoMarkersDictionary', 'ArUcoBoard', 'ArUcoCamera', 'ArUcoTracker']
\ No newline at end of file +__all__ = ['ArUcoMarkersDictionary', 'ArUcoMarker', 'ArUcoBoard', 'ArUcoCamera', 'ArUcoTracker', 'ArUcoCube']
\ No newline at end of file diff --git a/src/argaze/utils/tobii_stream_arcube_display.py b/src/argaze/utils/tobii_stream_arcube_display.py new file mode 100644 index 0000000..d1dc734 --- /dev/null +++ b/src/argaze/utils/tobii_stream_arcube_display.py @@ -0,0 +1,390 @@ +#!/usr/bin/env python + +import argparse +import os, json +import math + +from argaze import DataStructures +from argaze import GazeFeatures +from argaze.TobiiGlassesPro2 import * +from argaze.ArUcoMarkers import * +from argaze.AreaOfInterest import * +from argaze.utils import MiscFeatures + +import cv2 as cv +import numpy + +def make_rotation_matrix(x, y, z): + + # Create rotation matrix around x axis + c = numpy.cos(numpy.deg2rad(x)) + s = numpy.sin(numpy.deg2rad(x)) + Rx = numpy.array([[1, 0, 0], [0, c, -s], [0, s, c]]) + + # Create rotation matrix around y axis + c = numpy.cos(numpy.deg2rad(y)) + s = numpy.sin(numpy.deg2rad(y)) + Ry = numpy.array([[c, 0, s], [0, 1, 0], [-s, 0, c]]) + + # Create rotation matrix around z axis + c = numpy.cos(numpy.deg2rad(z)) + s = numpy.sin(numpy.deg2rad(z)) + Rz = numpy.array([[c, -s, 0], [s, c, 0], [0, 0, 1]]) + + # Return intrinsic rotation matrix + return Rx.dot(Ry.dot(Rz)) + +def main(): + """ + Track ArUcoCube into Tobii Glasses Pro 2 camera video stream. + """ + + # Manage arguments + parser = argparse.ArgumentParser(description=main.__doc__.split('-')[0]) + parser.add_argument('-t', '--tobii_ip', metavar='TOBII_IP', type=str, default=None, help='tobii glasses ip') + parser.add_argument('-c', '--camera_calibration', metavar='CAM_CALIB', type=str, default=None, help='json camera calibration filepath') + parser.add_argument('-p', '--aruco_tracker_configuration', metavar='TRACK_CONFIG', type=str, default=None, help='json aruco tracker configuration filepath') + parser.add_argument('-ac', '--aruco_cube', metavar='ARUCO_CUBE', type=str, help='json aruco cube description filepath') + parser.add_argument('-s', '--aoi_scene', metavar='AOI_SCENE', type=str, help='obj aoi 3D scene description filepath') + parser.add_argument('-w', '--window', metavar='DISPLAY', type=bool, default=True, help='enable window display', action=argparse.BooleanOptionalAction) + args = parser.parse_args() + + # Create tobii controller (with auto discovery network process if no ip argument is provided) + print('\nLooking for a Tobii Glasses Pro 2 device ...') + + try: + + tobii_controller = TobiiController.TobiiController(args.tobii_ip) + print(f'Tobii Glasses Pro 2 device found at {tobii_controller.address} address.') + + except ConnectionError as e: + + print(e) + exit() + + # Enable tobii data stream + tobii_data_stream = tobii_controller.enable_data_stream() + + # Enable tobii video stream + tobii_video_stream = tobii_controller.enable_video_stream() + + # Load aruco cube description + aruco_cube = ArUcoCube.ArUcoCube(args.aruco_cube) + aruco_cube.print_cache() + + # Load AOI 3D scene centered onto aruco cube + aoi3D_scene = AOI3DScene.AOI3DScene() + aoi3D_scene.load(args.aoi_scene) + + print(f'\nAOI in {os.path.basename(args.aoi_scene)} scene related to ArCube:') + for aoi in aoi3D_scene.keys(): + print(f'\t{aoi}') + + # Create aruco camera + aruco_camera = ArUcoCamera.ArUcoCamera() + + # Load calibration file + if args.camera_calibration != None: + + aruco_camera.load_calibration_file(args.camera_calibration) + + else: + + raise UserWarning('.json camera calibration filepath required. Use -c option.') + + # Create aruco tracker + aruco_tracker = ArUcoTracker.ArUcoTracker(aruco_cube.dictionary, aruco_cube.marker_size, aruco_camera) + + # Load specific configuration file + if args.aruco_tracker_configuration != None: + + aruco_tracker.load_configuration_file(args.aruco_tracker_configuration) + + print(f'\nArUcoTracker configuration for markers detection:') + aruco_tracker.print_configuration() + + # Init gyroscope processing to track head rotation changes when arcuco cube pose can't be estimated + # So, the resulting head rotation is relative to last pose estimation (it's not an absolute orientation) + last_gyroscope = numpy.zeros(3) + last_gyroscope_ts_ms = 0 + gyroscope_offset = numpy.zeros(3) + gyroscope_offset_smooth = 0.5 + head_rotation = numpy.zeros(3) + + + # Init accelerotmeter processing to track head translation changes when arcuco cube pose can't be estimated + # So, the resulting head translation is relative to last pose estimation (it's not an absolute position) + last_accelerometer = numpy.zeros(3) + last_accelerometer_ts_ms = 0 + accelerometer_offset = numpy.zeros(3) + accelerometer_offset_smooth = 0.2 + earth_gravity = numpy.array([0, -9.81, 0]) + head_plumb = numpy.zeros(3) + head_translation_speed = numpy.zeros(3) + last_head_translation_speed = numpy.zeros(3) + head_translation = numpy.zeros(3) + + # Init data timestamped in millisecond + data_ts_ms = 0 + + # Assess temporal performance + loop_chrono = MiscFeatures.TimeProbe() + loop_ps = 0 + + def data_stream_callback(data_ts, data_object, data_object_type): + + nonlocal data_ts_ms + + data_ts_ms = data_ts / 1e3 + + match data_object_type: + + case 'Gyroscope': + + nonlocal last_gyroscope + nonlocal last_gyroscope_ts_ms + nonlocal gyroscope_offset + nonlocal head_rotation + + # Convert deg/s into deg/ms + current_gyroscope = numpy.array(data_object.value) * 1e-3 + + # Init gyroscope derivation and integration + if last_gyroscope_ts_ms == 0: + + last_gyroscope = current_gyroscope + last_gyroscope_ts_ms = data_ts_ms + + # Calculate elapsed time + delta_time = data_ts_ms - last_gyroscope_ts_ms + + # Derivate gyroscope + gyroscope_derivation = (current_gyroscope - last_gyroscope) / delta_time if delta_time > 0 else numpy.zeros(3) + + # Update gyroscope offset smoothly and reset head rotation when gyroscope is stable + if numpy.linalg.norm(gyroscope_derivation) < 1e-5: + + gyroscope_offset = gyroscope_offset * gyroscope_offset_smooth + current_gyroscope * (1 - gyroscope_offset_smooth) + head_rotation = numpy.zeros(3) + + # Integrate gyroscope with offset compensation + head_rotation += (last_gyroscope - gyroscope_offset) * delta_time + + # Store current as last + last_gyroscope = current_gyroscope + last_gyroscope_ts_ms = data_ts_ms + + case 'Accelerometer': + + nonlocal last_accelerometer + nonlocal last_accelerometer_ts_ms + nonlocal accelerometer_offset + nonlocal earth_gravity + nonlocal head_plumb + nonlocal head_translation_speed + nonlocal last_head_translation_speed + nonlocal head_translation + + # Check head plumb as we need to know it to remove earth gravity + + #print('head_plumb=', head_plumb) + #print('numpy.linalg.norm(head_plumb)=', numpy.linalg.norm(head_plumb)) + #print('numpy.linalg.norm(earth_gravity)=', numpy.linalg.norm(earth_gravity)) + + if math.isclose(numpy.linalg.norm(head_plumb), numpy.linalg.norm(earth_gravity), abs_tol=1e-3): + + #print('raw accelerometer=',numpy.array(data_object.value)) + #print('head_plumb=', head_plumb) + + # Remove gravity along head plumb to accelerometer + current_accelerometer = numpy.array(data_object.value) - head_plumb + + # Convert m/s2 into cm/ms2 + current_accelerometer = numpy.array(data_object.value) * 1e-4 + + # Init accelerometer integration + if last_accelerometer_ts_ms == 0: + + last_accelerometer = current_accelerometer + last_accelerometer_ts_ms = data_ts_ms + + # Calculate elapsed time + delta_time = data_ts_ms - last_accelerometer_ts_ms + + # Update accelerometer offset smoothly and reset head translation speed when head translation speed is close to zero + # Note : head translation speed is simultaneously estimated thanks to cube pose (see below) + print('numpy.linalg.norm(head_translation_speed)=',numpy.linalg.norm(head_translation_speed)) + + if numpy.linalg.norm(head_translation_speed) < 1e-3: + + accelerometer_offset = accelerometer_offset * accelerometer_offset_smooth + current_accelerometer * (1 - accelerometer_offset_smooth) + print('> accelerometer_offset=',accelerometer_offset) + head_translation_speed = numpy.zeros(3) + + # Integrate accelerometer with offset compensation + head_translation_speed += (last_accelerometer - accelerometer_offset) * delta_time + head_translation += last_head_translation_speed * delta_time + + print('current_accelerometer(cm/ms2)=',current_accelerometer) + print('head_translation_speed(cm/s)=',head_translation_speed) + print('head_translation(cm)=',head_translation) + + # Store current as last + last_accelerometer = current_accelerometer + last_accelerometer_ts_ms = data_ts_ms + last_head_translation_speed = head_translation_speed + + #else: + + # print('no valid head plumb') + + tobii_data_stream.reading_callback = data_stream_callback + + # Start streaming + tobii_controller.start_streaming() + + # Live video stream capture loop + try: + + # Assess loop performance + loop_chrono = MiscFeatures.TimeProbe() + fps = 0 + + # Track aruco cube pose + aruco_cube_tvec = numpy.zeros(3) + aruco_cube_rvec = numpy.zeros(3) + aruco_cube_success = False + aruco_cube_validity = False + aruco_cube_ts_ms = 0 + + while tobii_video_stream.is_alive(): + + # Read video stream + video_ts, video_frame = tobii_video_stream.read() + video_ts_ms = video_ts / 1e3 + + # Copy video frame to edit visualisation on it without disrupting aruco tracking + visu_frame = video_frame.copy() + + # Process video and data frame + try: + + # Track markers with pose estimation + aruco_tracker.track(video_frame.matrix) + + # Estimate cube pose from tracked markers + tvec, rvec, success, validity = aruco_cube.estimate_pose(aruco_tracker.get_tracked_markers()) + + # Cube pose estimation succeed and is validated by 2 faces at least + if success and validity >= 2: + + # Reset head rotation, translation and translation speed (cm/s) + # Note : head translation speed is simultaneously estimated thanks to accelerometer sensor (see upward) + head_rotation = numpy.zeros(3) + head_translation = numpy.zeros(3) + head_translation_speed = (tvec - aruco_cube_tvec) / (video_ts_ms - aruco_cube_ts_ms) + + # Rotate head plumb orientation + C, _ = cv.Rodrigues(rvec) + head_plumb = -C.dot(earth_gravity) + + # Store cube pose + aruco_cube_tvec = tvec + aruco_cube_rvec = rvec + aruco_cube_success = success + aruco_cube_validity = validity + aruco_cube_ts_ms = video_ts_ms + + # Cube pose estimation fails + elif aruco_cube_success: + + # Use tobii glasses inertial sensors to estimate cube pose from last estimated pose + + # Translate cube according head translation + new_tvec = aruco_cube_tvec + head_translation + + #if numpy.linalg.norm(head_rotation) > 0: + # print(f'X={head_rotation[0]:3f}, Y={head_rotation[1]:3f}, Z={head_rotation[2]:3f}') + + # Rotate cube around origin according head rotation + R = make_rotation_matrix(*head_rotation) + + # rotate tvec ??? + #new_tvec = aruco_cube_tvec.dot(R.T) + + # rotate rvec + C, _ = cv.Rodrigues(aruco_cube_rvec) + C = C.dot(R) + new_rvec, _ = cv.Rodrigues(C) + + # Set cube pose estimation + aruco_cube.set_pose(tvec = new_tvec, rvec = new_rvec) + + else: + + raise UserWarning('Cube pose estimation fails.') + + # Project AOI 3 scene onto camera frame + + # 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 AOI, where the distorsion is low, it is acceptable. + aoi2D_scene = aoi3D_scene.project(aruco_cube_tvec, aruco_cube_rvec, aruco_camera.get_K()) + + # Draw projected scene + aoi2D_scene.draw(visu_frame.matrix) + + # Draw markers pose estimation + aruco_tracker.draw_tracked_markers(visu_frame.matrix) + + # Draw cube pose estimation (without camera distorsion) + aruco_cube.draw(visu_frame.matrix, aruco_camera.get_K(), (0, 0, 0, 0)) + + # Warn about cube pose validity + if not aruco_cube_validity: + + raise UserWarning('Cube pose estimation is not validated.') + + # Write warning + except UserWarning as w: + + cv.rectangle(visu_frame.matrix, (0, 100), (600, 150), (127, 127, 127), -1) + cv.putText(visu_frame.matrix, str(w), (20, 140), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 1, cv.LINE_AA) + + # Assess loop performance + lap_time, lap_counter, elapsed_time = loop_chrono.lap() + + # Update fps each 10 loops + if lap_counter >= 10: + + loop_ps = 1e3 * lap_counter / elapsed_time + loop_chrono.restart() + + # Draw center + cv.line(visu_frame.matrix, (int(visu_frame.width/2) - 50, int(visu_frame.height/2)), (int(visu_frame.width/2) + 50, int(visu_frame.height/2)), (255, 150, 150), 1) + cv.line(visu_frame.matrix, (int(visu_frame.width/2), int(visu_frame.height/2) - 50), (int(visu_frame.width/2), int(visu_frame.height/2) + 50), (255, 150, 150), 1) + + # Write stream timing + cv.rectangle(visu_frame.matrix, (0, 0), (1100, 50), (63, 63, 63), -1) + cv.putText(visu_frame.matrix, f'Data stream time: {int(data_ts_ms)} ms', (20, 40), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv.LINE_AA) + cv.putText(visu_frame.matrix, f'Video delay: {int(data_ts_ms - video_ts_ms)} ms', (550, 40), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv.LINE_AA) + cv.putText(visu_frame.matrix, f'Fps: {int(loop_ps)}', (950, 40), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv.LINE_AA) + + cv.imshow(f'Stream ArUcoCube', visu_frame.matrix) + + # Close window using 'Esc' key + if cv.waitKey(1) == 27: + break + + # Exit on 'ctrl+C' interruption + except KeyboardInterrupt: + pass + + # Stop frame display + cv.destroyAllWindows() + + # Stop streaming + tobii_controller.stop_streaming() + +if __name__ == '__main__': + + main()
\ No newline at end of file |