diff options
author | Théo de la Hogue | 2022-03-14 23:45:45 +0100 |
---|---|---|
committer | Théo de la Hogue | 2022-03-14 23:45:45 +0100 |
commit | 128c90e714924ac96f82ae5eace75bad09ba5471 (patch) | |
tree | b1a806d4b832315d2f8891359c2623033f34106a /src/argaze | |
download | argaze-128c90e714924ac96f82ae5eace75bad09ba5471.zip argaze-128c90e714924ac96f82ae5eace75bad09ba5471.tar.gz argaze-128c90e714924ac96f82ae5eace75bad09ba5471.tar.bz2 argaze-128c90e714924ac96f82ae5eace75bad09ba5471.tar.xz |
Pre-alpha version of ArGaze package
Diffstat (limited to 'src/argaze')
22 files changed, 1088 insertions, 0 deletions
diff --git a/src/argaze/ArUcoMarkers/ArUcoBoard.py b/src/argaze/ArUcoMarkers/ArUcoBoard.py new file mode 100644 index 0000000..fa76303 --- /dev/null +++ b/src/argaze/ArUcoMarkers/ArUcoBoard.py @@ -0,0 +1,79 @@ +import numpy +import cv2 as cv +import cv2.aruco as aruco + +# Built-in ArUco dictionaries from OpenCV library +ARUCO_DICT = { + 'DICT_4X4_50': aruco.DICT_4X4_50, + 'DICT_4X4_100': aruco.DICT_4X4_100, + 'DICT_4X4_250': aruco.DICT_4X4_250, + 'DICT_4X4_1000': aruco.DICT_4X4_1000, + 'DICT_5X5_50': aruco.DICT_5X5_50, + 'DICT_5X5_100': aruco.DICT_5X5_100, + 'DICT_5X5_250': aruco.DICT_5X5_250, + 'DICT_5X5_1000': aruco.DICT_5X5_1000, + 'DICT_6X6_50': aruco.DICT_6X6_50, + 'DICT_6X6_100': aruco.DICT_6X6_100, + 'DICT_6X6_250': aruco.DICT_6X6_250, + 'DICT_6X6_1000': aruco.DICT_6X6_1000, + 'DICT_7X7_50': aruco.DICT_7X7_50, + 'DICT_7X7_100': aruco.DICT_7X7_100, + 'DICT_7X7_250': aruco.DICT_7X7_250, + 'DICT_7X7_1000': aruco.DICT_7X7_1000, + 'DICT_ARUCO_ORIGINAL': aruco.DICT_ARUCO_ORIGINAL +} + +class ArUcoBoard(): + + # initialisation + def __init__(self, aruco_dictionary_name, columns, rows, square_size, marker_size): + + # check aruco dictionary name + if ARUCO_DICT.get(aruco_dictionary_name, None) is None: + raise NameError(f'Bad ArUco dictionnary name: {aruco_dictionary_name}') + + dict_name_split = aruco_dictionary_name.split('_') + + self.__aruco_dict_format = dict_name_split[1] + self.__aruco_dict_number = int(dict_name_split[2]) + + # load ArUco dictionary + self.__aruco_dict = aruco.Dictionary_get(ARUCO_DICT[aruco_dictionary_name]) + + # store property + self.__columns = columns + self.__rows = rows + self.__square_size = square_size # in cm + self.__marker_size = marker_size # in cm + + # create board model + self.__board = aruco.CharucoBoard_create(self.__columns, self.__rows, self.__square_size/100., self.__marker_size/100., self.__aruco_dict) + + # destruction + def __del__(self): + pass + + # access to the board model + def get_model(self): + + return self.__board + + # access to the board markers ids + def get_ids(self): + + return self.__board.ids + + # access to the number of columns and rows + def get_size(self): + + return self.__board.getChessboardSize() + + # save a picture of the calibration board + def export(self, destination_folder, dpi): + + output_filename = f'board_{self.__columns*self.__square_size}cmx{self.__rows*self.__square_size}cm_markers_{self.__aruco_dict_format}_{self.__marker_size}cm.png' + + dimension = [int(e * self.__board.getSquareLength() * 254 * dpi) for e in self.__board.getChessboardSize()] # 1 meter = 254 inches + + cv.imwrite(f'{destination_folder}/{output_filename}', self.__board.draw(dimension)) + diff --git a/src/argaze/ArUcoMarkers/ArUcoCamera.py b/src/argaze/ArUcoMarkers/ArUcoCamera.py new file mode 100644 index 0000000..163391f --- /dev/null +++ b/src/argaze/ArUcoMarkers/ArUcoCamera.py @@ -0,0 +1,72 @@ +import json +import numpy +import cv2.aruco as aruco + +class ArUcoCamera(): + + # initialisation + def __init__(self): + + self.__rms = 0 # root mean square error + self.__K = [] # camera matrix (focal lengths and principal point) + self.__D = [] # distortion coefficients + + # define calibration data + self.__corners_set_number = 0 + self.__corners_set = [] + self.__corners_set_ids = [] + + # destruction + def __del__(self): + pass + + # load camera calibration data + def load_calibration_file(self, camera_calibration_filepath): + + with open(camera_calibration_filepath) as calibration_file: + + calibration_data = json.load(calibration_file) + + self.__rms = calibration_data['rms'] + self.__K = numpy.asarray(calibration_data['camera matrix']) + self.__D = numpy.asarray(calibration_data['distortion coefficients']) + + def save_calibration_file(self, camera_calibration_filepath): + + calibration_data = {'rms': self.__rms, 'camera matrix': self.__K.tolist(), 'distortion coefficients': self.__D.tolist()} + + with open(camera_calibration_filepath, 'w', encoding='utf-8') as calibration_file: + + json.dump(calibration_data, calibration_file, ensure_ascii=False, indent=4) + + def get_rms(self): + return self.__rms + + def get_K(self): + return self.__K + + def get_D(self): + return self.__D + + def calibrate(self, board, frame_width, frame_height): + + if self.__corners_set_number > 0: + + self.__rms, self.__K, self.__D, r, t = aruco.calibrateCameraCharuco(self.__corners_set, self.__corners_set_ids, board.get_model(), [frame_width, frame_height], None, None) + + def reset_calibration_data(self, corners, corners_ids): + + self.__corners_set_number = 0 + self.__corners_set = [] + self.__corners_set_ids = [] + + def store_calibration_data(self, corners, corners_ids): + + self.__corners_set_number += 1 + self.__corners_set.append(corners) + self.__corners_set_ids.append(corners_ids) + + def get_calibration_data_count(self): + + return self.__corners_set_number + diff --git a/src/argaze/ArUcoMarkers/ArUcoMarkers.py b/src/argaze/ArUcoMarkers/ArUcoMarkers.py new file mode 100644 index 0000000..1499218 --- /dev/null +++ b/src/argaze/ArUcoMarkers/ArUcoMarkers.py @@ -0,0 +1,71 @@ +import numpy +import cv2 as cv +import cv2.aruco as aruco + +# Built-in ArUco dictionaries from OpenCV library +ARUCO_DICT = { + 'DICT_4X4_50': aruco.DICT_4X4_50, + 'DICT_4X4_100': aruco.DICT_4X4_100, + 'DICT_4X4_250': aruco.DICT_4X4_250, + 'DICT_4X4_1000': aruco.DICT_4X4_1000, + 'DICT_5X5_50': aruco.DICT_5X5_50, + 'DICT_5X5_100': aruco.DICT_5X5_100, + 'DICT_5X5_250': aruco.DICT_5X5_250, + 'DICT_5X5_1000': aruco.DICT_5X5_1000, + 'DICT_6X6_50': aruco.DICT_6X6_50, + 'DICT_6X6_100': aruco.DICT_6X6_100, + 'DICT_6X6_250': aruco.DICT_6X6_250, + 'DICT_6X6_1000': aruco.DICT_6X6_1000, + 'DICT_7X7_50': aruco.DICT_7X7_50, + 'DICT_7X7_100': aruco.DICT_7X7_100, + 'DICT_7X7_250': aruco.DICT_7X7_250, + 'DICT_7X7_1000': aruco.DICT_7X7_1000, + 'DICT_ARUCO_ORIGINAL': aruco.DICT_ARUCO_ORIGINAL +} + +class ArUcoMarkers(): + + # initialisation + def __init__(self, aruco_dictionary_name): + + # check aruco dictionary name + if ARUCO_DICT.get(aruco_dictionary_name, None) is None: + raise NameError(f'Bad ArUco dictionnary name: {aruco_dictionary_name}') + + dict_name_split = aruco_dictionary_name.split('_') + + self.__aruco_dict_format = dict_name_split[1] + self.__aruco_dict_number = int(dict_name_split[2]) + + # load ArUco dictionary + self.__aruco_dict = aruco.Dictionary_get(ARUCO_DICT[aruco_dictionary_name]) + + # destruction + def __del__(self): + pass + + # save one marker + def export(self, destination_folder, dpi, i): + + if i >= 0 and i < self.__aruco_dict_number: + + output_filename = f'marker_{self.__aruco_dict_format}_{i}.png' + + # create marker + marker = numpy.zeros((dpi, dpi, 1), dtype="uint8") + aruco.drawMarker(self.__aruco_dict, i, dpi, marker, 1) + + # save marker into destination folder + cv.imwrite(f'{destination_folder}/{output_filename}', marker) + + else: + raise ValueError(f'Bad ArUco index: {i}') + + # save all markers + def export_all(self, destination_folder, dpi): + + for i in range(self.__aruco_dict_number): + + self.export(destination_folder, dpi, i) + + diff --git a/src/argaze/ArUcoMarkers/ArUcoTracker.py b/src/argaze/ArUcoMarkers/ArUcoTracker.py new file mode 100644 index 0000000..1b05e4a --- /dev/null +++ b/src/argaze/ArUcoMarkers/ArUcoTracker.py @@ -0,0 +1,157 @@ +import numpy +import cv2 as cv +import cv2.aruco as aruco + +# Built-in ArUco dictionaries from OpenCV library +ARUCO_DICT = { + 'DICT_4X4_50': aruco.DICT_4X4_50, + 'DICT_4X4_100': aruco.DICT_4X4_100, + 'DICT_4X4_250': aruco.DICT_4X4_250, + 'DICT_4X4_1000': aruco.DICT_4X4_1000, + 'DICT_5X5_50': aruco.DICT_5X5_50, + 'DICT_5X5_100': aruco.DICT_5X5_100, + 'DICT_5X5_250': aruco.DICT_5X5_250, + 'DICT_5X5_1000': aruco.DICT_5X5_1000, + 'DICT_6X6_50': aruco.DICT_6X6_50, + 'DICT_6X6_100': aruco.DICT_6X6_100, + 'DICT_6X6_250': aruco.DICT_6X6_250, + 'DICT_6X6_1000': aruco.DICT_6X6_1000, + 'DICT_7X7_50': aruco.DICT_7X7_50, + 'DICT_7X7_100': aruco.DICT_7X7_100, + 'DICT_7X7_250': aruco.DICT_7X7_250, + 'DICT_7X7_1000': aruco.DICT_7X7_1000, + 'DICT_ARUCO_ORIGINAL': aruco.DICT_ARUCO_ORIGINAL +} + +class ArUcoTracker(): + + # initialisation + def __init__(self, aruco_dictionary_name, marker_length, camera): + + # check aruco dictionary name + if ARUCO_DICT.get(aruco_dictionary_name, None) is None: + raise NameError(f'Bad ArUco dictionnary name: {aruco_dictionary_name}') + + # load ArUco dictionary + self.__aruco_dict = aruco.Dictionary_get(ARUCO_DICT[aruco_dictionary_name]) + + # define marker length in centimeter + self.__marker_length = marker_length + + # define camera + self.__camera = camera + + # setup ArUco detection parameters + self.__aruco_param = aruco.DetectorParameters_create() + self.__aruco_param.cornerRefinementMethod = aruco.CORNER_REFINE_CONTOUR + + # define tracked markers data + self.__markers_corners = [] + self.__markers_ids = [] + self.__rvecs = [] + self.__tvecs = [] + self.__points = [] + + # define tracked board data + self.__board = None + self.__board_corners_number = 0 + self.__board_corners = [] + self.__board_corners_ids = [] + + # destruction + def __del__(self): + pass + + # track ArUco markers in frame + def track(self, frame, estimate_pose = True): + + # 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, parameters = self.__aruco_param) + + 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()) + + else: + + self.__rvecs = [] + self.__tvecs = [] + self.__points = [] + + # track ArUco markers board in frame setting up the number of detected markers needed to agree detection + def track_board(self, frame, board, expected_markers_number): + + # 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, parameters = self.__aruco_param) + + # if all board markers are detected + if self.get_markers_number() == expected_markers_number: + + self.__board = board + self.__board_corners_number, self.__board_corners, self.__board_corners_ids = aruco.interpolateCornersCharuco(self.__markers_corners, self.__markers_ids, gray, self.__board.get_model()) + + else: + + self.__board = None + self.__board_corners_number = 0 + self.__board_corners = [] + self.__board_corners_ids = [] + + # draw tracked markers in frame + def draw(self, frame): + + # draw detected markers square + if len(self.__markers_corners) > 0: + + aruco.drawDetectedMarkers(frame, self.__markers_corners, self.__markers_ids) + + # draw marker axis if pose has been estimated + if len(self.__rvecs) > 0: + + for (i, marker_id) in enumerate(self.__markers_ids): + + aruco.drawAxis(frame, self.__camera.get_K(), self.__camera.get_D(), self.__rvecs[i], self.__tvecs[i], self.__marker_length) + + # draw tracked board corners in frame + def draw_board(self, frame): + + if self.__board != None: + + cv.drawChessboardCorners(frame, ((self.__board.get_size()[0] - 1 ), (self.__board.get_size()[1] - 1)), self.__board_corners, True) + + # access to tracked markers data + def get_markers_number(self): + return len(self.__markers_corners) + + def get_markers_ids(self): + return self.__markers_ids + + def get_marker_corners(self, i): + return self.__markers_corners[i] + + def get_marker_rotation(self, i): + return self.__rvecs[i] + + def get_marker_translation(self, i): + return self.__tvecs[i] + + def get_marker_points(self, i): + return self.__points[i] + + # access to tracked board data + def get_board_corners_number(self): + return self.__board_corners_number + + def get_board_corners_ids(self): + return self.__board_corners_ids + + def get_board_corners(self): + return self.__board_corners + diff --git a/src/argaze/ArUcoMarkers/README.md b/src/argaze/ArUcoMarkers/README.md new file mode 100644 index 0000000..f79be36 --- /dev/null +++ b/src/argaze/ArUcoMarkers/README.md @@ -0,0 +1,13 @@ +# ArUcoMarkers + +_Class interface to work with OpenCV ArUco markers._ +https://docs.opencv.org/4.x/d5/dae/tutorial_aruco_detection.html + +## ArUcoBoard.py + +## ArUcoCamera.py + +## ArUcoMarkers.py + +## ArUcoTracker.py + diff --git a/src/argaze/ArUcoMarkers/__init__.py b/src/argaze/ArUcoMarkers/__init__.py new file mode 100644 index 0000000..188e407 --- /dev/null +++ b/src/argaze/ArUcoMarkers/__init__.py @@ -0,0 +1 @@ +__all__ = ['ArUcoBoard', 'ArUcoCamera', 'ArUcoMarkers', 'ArUcoTracker']
\ No newline at end of file diff --git a/src/argaze/ArUcoMarkers/utils/A3_board_35cmx25cm_markers_4X4_3cm.pdf b/src/argaze/ArUcoMarkers/utils/A3_board_35cmx25cm_markers_4X4_3cm.pdf Binary files differnew file mode 100644 index 0000000..7725730 --- /dev/null +++ b/src/argaze/ArUcoMarkers/utils/A3_board_35cmx25cm_markers_4X4_3cm.pdf diff --git a/src/argaze/ArUcoMarkers/utils/A4_markers_4x4_3cm.pdf b/src/argaze/ArUcoMarkers/utils/A4_markers_4x4_3cm.pdf Binary files differnew file mode 100644 index 0000000..412684f --- /dev/null +++ b/src/argaze/ArUcoMarkers/utils/A4_markers_4x4_3cm.pdf diff --git a/src/argaze/ArUcoMarkers/utils/_board_A3.afdesign b/src/argaze/ArUcoMarkers/utils/_board_A3.afdesign Binary files differnew file mode 100644 index 0000000..57c6588 --- /dev/null +++ b/src/argaze/ArUcoMarkers/utils/_board_A3.afdesign diff --git a/src/argaze/ArUcoMarkers/utils/_markers.afdesign b/src/argaze/ArUcoMarkers/utils/_markers.afdesign Binary files differnew file mode 100644 index 0000000..8443b9a --- /dev/null +++ b/src/argaze/ArUcoMarkers/utils/_markers.afdesign diff --git a/src/argaze/RegionOfInterest/README.md b/src/argaze/RegionOfInterest/README.md new file mode 100644 index 0000000..faeb714 --- /dev/null +++ b/src/argaze/RegionOfInterest/README.md @@ -0,0 +1,11 @@ +# RegionOfInterest + +_Class interface to manage ROIs_ +https://en.wikipedia.org/wiki/Region_of_interest#:~:text=A%20region%20of%20interest%20(often,purpose%20of%20measuring%20its%20size. + +## ROI2DScene.py + +## ROI3DScene.py + + + diff --git a/src/argaze/RegionOfInterest/ROI2DScene.py b/src/argaze/RegionOfInterest/ROI2DScene.py new file mode 100644 index 0000000..6e0faf4 --- /dev/null +++ b/src/argaze/RegionOfInterest/ROI2DScene.py @@ -0,0 +1,40 @@ +import cv2 as cv +import matplotlib.path as mpath + +class ROI2DScene(list): + + # subclass list + def __new__(cls): + return super(ROI2DScene, cls).__new__(cls) + + # initialisation + def __init__(self): + pass + + # destruction + def __del__(self): + pass + + # check if a (x, y) pointer is inside rois + def inside(self, pointer): + + for roi in self: + + roi['POINTER_INSIDE'] = mpath.Path(roi['VERTICES']).contains_points([pointer])[0] + + # draw projected polygons + def draw(self, frame): + + for roi in self: + + vertices = roi['VERTICES'] + inside = roi['POINTER_INSIDE'] + + color = (0, 255, 0) if inside else (0, 0, 255) + + if inside: + cv.putText(frame, roi['NAME'], (vertices[3][0], vertices[3][1]), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv.LINE_AA) + + cv.line(frame, vertices[-1], vertices[0], color, 1) + for A, B in zip(vertices, vertices[1:]): + cv.line(frame, A, B, color, 1) diff --git a/src/argaze/RegionOfInterest/ROI3DScene.py b/src/argaze/RegionOfInterest/ROI3DScene.py new file mode 100644 index 0000000..d292d2a --- /dev/null +++ b/src/argaze/RegionOfInterest/ROI3DScene.py @@ -0,0 +1,139 @@ +import math +import re + +from argaze.RegionOfInterest import ROI2DScene + +import numpy +import cv2 as cv +import matplotlib.path as mpath + +class ROI3DScene(list): + + # subclass list + def __new__(cls): + return super(ROI3DScene, cls).__new__(cls) + + # initialisation + def __init__(self): + + # define rotation and translation matrix + self.__rotation = [0, 0, 0] + self.__translation = [0, 0, 0] + + # define a zero distorsion matrix + self.__D0 = numpy.asarray([0.0, 0.0, 0.0, 0.0, 0.0]) + + # destruction + def __del__(self): + pass + + # load scen from .obj file + def load(self, obj_filepath): + + # regex rules for .obj file parsing + OBJ_RX_DICT = { + 'comment': re.compile(r'#(.*)\n'), + 'name': re.compile(r'o (\w+)(.*)\n'), + 'vertice': re.compile(r'v ([+-]?[0-9]*[.]?[0-9]+) ([+-]?[0-9]*[.]?[0-9]+) ([+-]?[0-9]*[.]?[0-9]+)\n'), + 'face': re.compile(r'f (.*)\n') + } + + # regex .obj line parser + def __parse_obj_line(line): + + for key, rx in OBJ_RX_DICT.items(): + match = rx.search(line) + if match: + return key, match + + # if there are no matches + return None, None + + # start parsing + try: + + roi3D = {} + vertices = [] + faces = [] + + # open the file and read through it line by line + with open(obj_filepath, 'r') as file: + + line = file.readline() + + while line: + + # at each line check for a match with a regex + key, match = __parse_obj_line(line) + + # extract comment + if key == 'comment': + pass + + # extract roi3D name + elif key == 'name': + + roi3D['NAME'] = str(match.group(1)) + + # fill vertices array + elif key == 'vertice': + + vertices.append(tuple([float(match.group(1)), float(match.group(2)), float(match.group(3))])) + + # extract roi3D vertice id + elif key == 'face': + + roi3D['FACE'] = [int(i) for i in match.group(1).split()] + + # store roi3D dict into scene array + self.append(roi3D) + + # clear roi3D dict + roi3D = {} + + # go to next line + line = file.readline() + + file.close() + + # retreive all roi3D vertices + for roi3D in self: + roi3D['VERTICES'] = [ vertices[i-1] for i in roi3D['FACE'] ] + roi3D.pop('FACE', None) + + # print scene + for roi3D in self: + name = roi3D['NAME'] + vertices = roi3D['VERTICES'] + + except IOError: + raise IOError(f'File not found: {obj_filepath}') + + def set_rotation(self, rvec): + + self.__rotation = rvec + + def set_translation(self, tvec): + + self.__translation = tvec + + # project 3D scene onto 2D scene through a camera + def project(self, frame, camera, apply_distorsion = True): + + roi2D_scene = ROI2DScene.ROI2DScene() + + for roi3D in self: + + vertices_3D = numpy.array(roi3D['VERTICES']).astype('float32') + + vertices_2D, J = cv.projectPoints(vertices_3D, self.__rotation, self.__translation, camera.get_K(), camera.get_D() if apply_distorsion else self.__D0) + vertices_2D = vertices_2D.astype('int').reshape((len(vertices_2D), 2)) + + roi2D = { + 'NAME': roi3D['NAME'], + 'VERTICES': vertices_2D + } + + roi2D_scene.append(roi2D) + + return roi2D_scene diff --git a/src/argaze/RegionOfInterest/__init__.py b/src/argaze/RegionOfInterest/__init__.py new file mode 100644 index 0000000..f81f73f --- /dev/null +++ b/src/argaze/RegionOfInterest/__init__.py @@ -0,0 +1 @@ +__all__ = ['ROI2DScene', 'ROI3DScene']
\ No newline at end of file diff --git a/src/argaze/TobiiGlassesPro2/README.md b/src/argaze/TobiiGlassesPro2/README.md new file mode 100644 index 0000000..3352274 --- /dev/null +++ b/src/argaze/TobiiGlassesPro2/README.md @@ -0,0 +1,112 @@ +# TobiiGlassesPro2 + +_This module provides a class interface to handle TobbiGlassesPro2 data and video stream. +It is based on TobiiGlassesPyController package (https://github.com/ddetommaso/TobiiGlassesPyController)._ + +## To connect Tobii glasses on Mac OS + +* Install DHCP server: brew install isc-dhcp + +* Setup DHCP server: + * /usr/local/etc/dhcpd.conf + * /usr/local/etc/master.conf + +* Setup USB LAN Interface: + * ip: 192.168.1.1 + * subnet: 255.255.255.0 + * router: 192.168.1.254 + +* Launch Tobii glasses interface to enable USB LAN Interface before to Launch DHCP server. + +* Launch DHCP server: sudo /usr/local/sbin/dhcpd + +* Kill DHCP server: sudo kill `cat /usr/local/var/dhcpd/dhcpd.pid` + +* Watch DHCP server: sudo log stream --info --debug --predicate "process == 'dhcpd'" + +## Tobii data structure + +Each data frame have the following structure: +``` + { + 'mems': { + 'ac': { + 'ts': 1042711827, + 's': 0, + 'ac': [0.549, -9.868, 2.203] + }, + 'gy': { + 'ts': 1042723807, + 's': 0, + 'gy': [2.94, -2.384, 1.428] + } + }, + 'right_eye': { + 'pc': { + 'ts': 1042743553, + 's': 0, 'gidx': 39971, + 'pc': [-35.24, -25.51, -31.66], + 'eye': 'right' + }, + 'pd': { + 'ts': 1042743553, + 's': 0, + 'gidx': 39971, + 'pd': 3.72, + 'eye': 'right' + }, + 'gd': { + 'ts': 1042743553, + 's': 0, + 'gidx': 39971, + 'gd': [0.1833, 0.307, 0.9339], + 'eye': 'right' + } + }, + 'left_eye': { + 'pc': { + 'ts': 1042743553, + 's': 0, + 'gidx': 39971, + 'pc': [29.96, -27.92, -40.9], + 'eye': 'left' + }, + 'pd': { + 'ts': 1042743553, + 's': 0, + 'gidx': 39971, + 'pd': 4.42, + 'eye': 'left' + }, + 'gd': { + 'ts': 1042743553, + 's': 0, + 'gidx': 39971, + 'gd': [0.1528, 0.2977, 0.9423], + 'eye': 'left' + } + }, + 'gp': { + 'ts': 1042743553, + 's': 0, + 'gidx': 39971, + 'l': 82832, + 'gp': [0.3975, 0.2228] + }, + 'gp3': { + 'ts': 1042743553, + 's': 0, + 'gidx': 39971, + 'gp3': [313.64, 542.2, 1728.85] + }, + 'pts': { + 'ts': 1042123917, + 's': 0, + 'pts': 93695, # Presentation Time Stamp (PTS) value to synchronise with live video stream frame PTS + 'pv': 7 + }, + 'vts': { + 'ts': -1 + } + } +```
\ No newline at end of file diff --git a/src/argaze/TobiiGlassesPro2/TobiiController.py b/src/argaze/TobiiGlassesPro2/TobiiController.py new file mode 100644 index 0000000..aafa225 --- /dev/null +++ b/src/argaze/TobiiGlassesPro2/TobiiController.py @@ -0,0 +1,25 @@ +import tobiiglassesctrl + +class TobiiController(tobiiglassesctrl.TobiiGlassesController): + + # initialisation + def __init__(self, ip_address, project_name, participant_id): + + super().__init__(ip_address, video_scene = True) + + # edit project and participant + project_id = super().create_project(project_name) + participant_id = super().create_participant(project_id, project_name) + ''' + # start calibration + input("Position Tobbi glasses calibration target then presse 'Enter' to start calibration.") + calibration_id = super().create_calibration(project_id, participant_id) + super().start_calibration(calibration_id) + + if not super().wait_until_calibration_is_done(calibration_id): + ogging.error('TobiiController.__init__() : Calibration has failed') + ''' + + # destruction + def __del__(self): + pass diff --git a/src/argaze/TobiiGlassesPro2/TobiiData.py b/src/argaze/TobiiGlassesPro2/TobiiData.py new file mode 100644 index 0000000..6bfc89e --- /dev/null +++ b/src/argaze/TobiiGlassesPro2/TobiiData.py @@ -0,0 +1,271 @@ +import threading +import time + +class TobiiDataThread(threading.Thread): + + # initialisation + def __init__(self, controller): + + threading.Thread.__init__(self) + self.stop_event = threading.Event() + self.read_lock = threading.Lock() + + self.controller = controller + + self.fps = self.controller.get_et_freq() + self.sleep = 1./self.fps + + self.__ac_buffer = [] # accelerometer + self.__gy_buffer = [] # gyroscope + self.__gp_buffer = [] # gaze point + self.__pts_buffer = [] # presentation timestamp + + self.__start_ts = 0 + + # destruction + def __del__(self): + pass + + # extract ac data + def __get_ac(self, data): + + ac_value = data['mems']['ac']['ac'] + ac_ts = data['mems']['ac']['ts'] + ac_data = { + 'TIMESTAMP': ac_ts, + 'TIME': (ac_ts - self.__start_ts) / 1000000., + 'X': ac_value[0], + 'Y': ac_value[1], + 'Z': ac_value[2] + } + + return ac_data + + # extract gy data + def __get_gy(self, data): + + gy_value = data['mems']['gy']['gy'] + gy_ts = data['mems']['gy']['ts'] + gy_data = { + 'TIMESTAMP': gy_ts, + 'TIME': (gy_ts - self.__start_ts) / 1000000., + 'X': gy_value[0], + 'Y': gy_value[1], + 'Z': gy_value[2] + } + + return gy_data + + # extract gp data + def __get_gp(self, data): + + gp_value = data['gp']['gp'] + gp_ts = data['gp']['ts'] + gp_data = { + 'TIMESTAMP': gp_ts, + 'TIME': (gp_ts - self.__start_ts) / 1000000., + 'X': gp_value[0], + 'Y': gp_value[1] + } + + return gp_data + + # extract pts data + def __get_pts(self, data): + + pts_value = data['pts']['pts'] + pts_ts = data['pts']['ts'] + pts_data = { + 'TIMESTAMP': pts_ts, + 'TIME': (pts_ts - self.__start_ts) / 1000000., + 'PTS': pts_value + } + + return pts_data + + # thread start + def run(self): + + while not self.stop_event.isSet(): + + time.sleep(self.sleep) + + self.read_lock.acquire() + + data = self.controller.get_data() + + # store only timestamped datas + if 'pts' in data: + + pts_data = data['pts'] + + if 'pts' in pts_data: + + ac_ts = data['mems']['ac']['ts'] + gy_ts = data['mems']['gy']['ts'] + gp_ts = data['gp']['ts'] + pts_ts = pts_data['ts'] + + # get start timestamp + if self.__start_ts == 0: + + # ignore -1 timestamp + valid_ts = [] + for ts in [ac_ts, gy_ts, gp_ts, pts_ts]: + if ts > 0: + valid_ts.append(ts) + + self.__start_ts = min(valid_ts) + #print(f'Tobii Data Frame: __start_ts = {self.__start_ts}') + + #print(f'Tobii Data Frame: ac_ts = {ac_ts}, gy_ts = {gy_ts}, gp_ts = {gp_ts}, pts_ts = {pts_ts}') + + # ignore -1 timestamp and filter repetitions + + if ac_ts != -1: + if len(self.__ac_buffer) == 0: + self.__ac_buffer.append(self.__get_ac(data)) + elif ac_ts != self.__ac_buffer[-1]['TIMESTAMP']: + self.__ac_buffer.append(self.__get_ac(data)) + + if gy_ts != -1: + if len(self.__gy_buffer) == 0: + self.__gy_buffer.append(self.__get_gy(data)) + elif gy_ts != self.__gy_buffer[-1]['TIMESTAMP']: + self.__gy_buffer.append(self.__get_gy(data)) + + if gp_ts != -1: + if len(self.__gp_buffer) == 0: + self.__gp_buffer.append(self.__get_gp(data)) + elif gp_ts != self.__gp_buffer[-1]['TIMESTAMP']: + self.__gp_buffer.append(self.__get_gp(data)) + + if pts_ts != -1: + if len(self.__pts_buffer) == 0: + self.__pts_buffer.append(self.__get_pts(data)) + elif pts_ts != self.__pts_buffer[-1]['TIMESTAMP']: + self.__pts_buffer.append(self.__get_pts(data)) + + self.read_lock.release() + + # read ac data + def read_accelerometer_data(self, timestamp): + + if len(self.__ac_buffer): + + self.read_lock.acquire() + + # TODO : find closest timestamp data + ac_data = self.__ac_buffer[-1].copy() + + self.read_lock.release() + + return ac_data + + else: + + return {} + + # read ac buffer + def read_accelerometer_buffer(self): + + self.read_lock.acquire() + + ac_buffer = self.__ac_buffer.copy() + + self.read_lock.release() + + return ac_buffer + + # read gy data + def read_gyroscope_data(self, timestamp): + + if len(self.__gy_buffer): + + self.read_lock.acquire() + + # TODO : find closest timestamp data + gy_data = self.__gy_buffer[-1].copy() + + self.read_lock.release() + + return gy_data + + else: + + return {} + + # read gy buffer + def read_gyroscope_buffer(self): + + self.read_lock.acquire() + + gy_buffer = self.__gy_buffer.copy() + + self.read_lock.release() + + return gy_buffer + + # read gp data + def read_gaze_data(self, timestamp): + + if len(self.__gp_buffer): + + self.read_lock.acquire() + + # TODO : find closest timestamp data + gp_data = self.__gp_buffer[-1].copy() + + self.read_lock.release() + + return gp_data + + else: + + return {} + + # read gp buffer + def read_gaze_buffer(self): + + self.read_lock.acquire() + + gp_buffer = self.__gp_buffer.copy() + + self.read_lock.release() + + return gp_buffer + + # read pts data + def read_pts_data(self, timestamp): + + if len(self.__pts_buffer): + + self.read_lock.acquire() + + # TODO : find closest timestamp data + pts_data = self.__pts_buffer[-1].copy() + + self.read_lock.release() + + return pts_data + + else: + + return {} + + # read pts buffer + def read_pts_buffer(self): + + self.read_lock.acquire() + + pts_buffer = self.__pts_buffer.copy() + + self.read_lock.release() + + return pts_buffer + + # thread stop + def stop(self): + + self.stop_event.set() + threading.Thread.join(self) diff --git a/src/argaze/TobiiGlassesPro2/TobiiVideo.py b/src/argaze/TobiiGlassesPro2/TobiiVideo.py new file mode 100644 index 0000000..8777a02 --- /dev/null +++ b/src/argaze/TobiiGlassesPro2/TobiiVideo.py @@ -0,0 +1,95 @@ +import threading + +import av +import numpy + +class TobiiVideoThread(threading.Thread): + + # initialisation + def __init__(self, controller): + + threading.Thread.__init__(self) + self.stop_event = threading.Event() + self.read_lock = threading.Lock() + + self.controller = controller + + self.fps = self.controller.get_video_freq() + + self.read_lock.acquire() + + self.__frame = numpy.zeros((1, 1, 3), numpy.uint8) + self.__width = 0 + self.__height = 0 + self.__pts_buffer = [] + + self.read_lock.release() + + # destruction + def __del__(self): + pass + + # thread start + def run(self): + + # start Tobii glasses stream capture + self.__container = av.open(f'rtsp://{self.controller.get_address()}:8554/live/scene', options={'rtsp_transport': 'tcp'}) + self.__stream = self.__container.streams.video[0] + + for f in self.__container.decode(self.__stream): + + if self.stop_event.isSet(): + break + + self.read_lock.acquire() + + self.__frame = f.to_ndarray(format='bgr24') + self.__width = f.width + self.__height = f.height + self.__pts_buffer.append({'TIME':f.time, 'PTS': f.pts}) + + #print(f'Tobii Video Frame: pts = {f.pts}, time = {f.time}, format = {f.width}, {f.height}') + + self.read_lock.release() + + # read frame + def read(self) : + + # if stopped, return blank frame + if self.stop_event.isSet(): + return numpy.zeros((1, 1, 3), numpy.uint8) + + # else + self.read_lock.acquire() + + frame_copy = self.__frame.copy() + width_copy = self.__width + height_copy = self.__height + + if len(self.__pts_buffer): + time_copy = self.__pts_buffer[-1]['TIME'] + pts_copy = self.__pts_buffer[-1]['PTS'] + else: + time_copy = -1 + pts_copy = -1 + + self.read_lock.release() + + return frame_copy, width_copy, height_copy, time_copy, pts_copy + + # read pts buffer + def read_pts_buffer(self): + + self.read_lock.acquire() + + pts_buffer = self.__pts_buffer.copy() + + self.read_lock.release() + + return pts_buffer + + # thread stop + def stop(self): + + self.stop_event.set() + threading.Thread.join(self) diff --git a/src/argaze/TobiiGlassesPro2/__init__.py b/src/argaze/TobiiGlassesPro2/__init__.py new file mode 100644 index 0000000..3884106 --- /dev/null +++ b/src/argaze/TobiiGlassesPro2/__init__.py @@ -0,0 +1 @@ +__all__ = ['TobiiController', 'TobiiData', 'TobiiVideo']
\ No newline at end of file diff --git a/src/argaze/TobiiGlassesPro2/utils/_calibration_target.afdesign b/src/argaze/TobiiGlassesPro2/utils/_calibration_target.afdesign Binary files differnew file mode 100644 index 0000000..5578e0b --- /dev/null +++ b/src/argaze/TobiiGlassesPro2/utils/_calibration_target.afdesign diff --git a/src/argaze/TobiiGlassesPro2/utils/calibration_target_A4.pdf b/src/argaze/TobiiGlassesPro2/utils/calibration_target_A4.pdf Binary files differnew file mode 100644 index 0000000..dfdbe0a --- /dev/null +++ b/src/argaze/TobiiGlassesPro2/utils/calibration_target_A4.pdf diff --git a/src/argaze/__init__.py b/src/argaze/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/argaze/__init__.py |