diff options
Diffstat (limited to 'src/argaze/utils/tobii_camera_calibrate.py')
-rw-r--r-- | src/argaze/utils/tobii_camera_calibrate.py | 140 |
1 files changed, 0 insertions, 140 deletions
diff --git a/src/argaze/utils/tobii_camera_calibrate.py b/src/argaze/utils/tobii_camera_calibrate.py deleted file mode 100644 index 24cbe5c..0000000 --- a/src/argaze/utils/tobii_camera_calibrate.py +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/env python - -import argparse -import os -import time - -from argaze.TobiiGlassesPro2 import TobiiController, TobiiVideo -from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoBoard, ArUcoDetector, ArUcoCamera - -import cv2 as cv - -def main(): - """ - Captures board pictures and finally outputs camera calibration data into a .json file. - - - Export and print a calibration board using - - Place the calibration board in order to view it entirely on screen and move the camera in many configurations (orientation and distance) : the script will automatically take pictures. Do this step with a good lighting and a clear background. - - Once enough pictures have been captured (~20), press Esc key then, wait for the camera calibration processing. - - Finally, check rms parameter: it should be between 0. and 1. if the calibration succeeded (lower is better). - - ### Reference: - - [Camera calibration using ArUco marker tutorial](https://automaticaddison.com/how-to-perform-camera-calibration-using-opencv/) - """ - - # manage arguments - parser = argparse.ArgumentParser(description=main.__doc__.split('-')[0]) - parser.add_argument('columns', metavar='COLS_NUMBER', type=int, default=7, help='number of columns') - parser.add_argument('rows', metavar='ROWS_NUMBER', type=int, default=5, help='number of rows') - parser.add_argument('square_size', metavar='SQUARE_SIZE', type=float, default=5, help='square size (cm)') - parser.add_argument('marker_size', metavar='MARKER_SIZE', type=float, default=3, help='marker size (cm)') - parser.add_argument('-t', '--tobii_ip', metavar='TOBII_IP', type=str, default=None, help='tobii glasses ip') - parser.add_argument('-o', '--output', metavar='OUT', type=str, default='camera.json', help='destination filepath') - parser.add_argument('-d', '--dictionary', metavar='DICT', type=str, default='DICT_ARUCO_ORIGINAL', help='aruco marker dictionnary (DICT_4X4_50, DICT_4X4_100, DICT_4X4_250, DICT_4X4_1000, DICT_5X5_50, DICT_5X5_100, DICT_5X5_250, DICT_5X5_1000, DICT_6X6_50, DICT_6X6_100, DICT_6X6_250, DICT_6X6_1000, DICT_7X7_50, DICT_7X7_100, DICT_7X7_250, DICT_7X7_1000, DICT_ARUCO_ORIGINAL, DICT_APRILTAG_16h5, DICT_APRILTAG_25h9, DICT_APRILTAG_36h10, DICT_APRILTAG_36h11)') - args = parser.parse_args() - - # Create tobii controller (with auto discovery network process if no ip argument is provided) - print("Looking 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() - - # Setup camera at 25 fps to work on Full HD video stream - tobii_controller.set_scene_camera_freq_25() - - # Get video stream dimension - video_width = tobii_controller.get_configuration()['sys_sc_width'] - video_height = tobii_controller.get_configuration()['sys_sc_height'] - - # Print current confirguration - print(f'Tobii Glasses Pro 2 configuration:') - for key, value in tobii_controller.get_configuration().items(): - print(f'\t{key}: {value}') - - # Enable tobii video stream - tobii_video_stream = tobii_controller.enable_video_stream() - - # Create aruco camera - aruco_camera = ArUcoCamera.ArUcoCamera(dimensions=(video_width, video_height)) - - # Create aruco board - aruco_board = ArUcoBoard.ArUcoBoard(args.columns, args.rows, args.square_size, args.marker_size, args.dictionary) - - # Create aruco detecter - aruco_detector = ArUcoDetector.ArUcoDetector(dictionary=args.dictionary, marker_size=args.marker_size) - - # Start tobii glasses streaming - tobii_controller.start_streaming() - - print("Camera calibration starts") - print("Waiting for calibration board...") - - expected_markers_number = aruco_board.markers_number - expected_corners_number = aruco_board.corners_number - - # Capture loop - try: - - while tobii_video_stream.is_alive(): - - # capture frame with a full displayed board - video_ts, video_frame = tobii_video_stream.read() - - # detect all markers in the board - aruco_detector.detect_board(video_frame.matrix, aruco_board, expected_markers_number) - - # draw only markers - aruco_detector.draw_detected_markers(video_frame.matrix) - - # draw current calibration data count - cv.putText(video_frame.matrix, f'Capture: {aruco_camera.calibration_data_count}', (50, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv.LINE_AA) - cv.imshow('Tobii Camera Calibration', video_frame.matrix) - - # if all board corners are detected - if aruco_detector.board_corners_number == expected_corners_number: - - # draw board corners to notify a capture is done - aruco_detector.draw_board(video_frame.matrix) - - # append data - aruco_camera.store_calibration_data(aruco_detector.board_corners, aruco_detector.board_corners_identifier) - - cv.imshow('Tobii Camera Calibration', video_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 tobii glasses streaming - tobii_controller.stop_streaming() - - print('\nCalibrating camera...') - aruco_camera.calibrate(aruco_board) - - print('\nCalibration succeeded!') - print(f'\nRMS:\n{aruco_camera.rms}') - print(f'\nDimensions:\n{video_width}x{video_height}') - print(f'\nCamera matrix:\n{aruco_camera.K}') - print(f'\nDistortion coefficients:\n{aruco_camera.D}') - - aruco_camera.to_json(args.output) - - print(f'\nCalibration data exported into {args.output} file') - -if __name__ == '__main__': - - main() |