diff options
24 files changed, 170 insertions, 148 deletions
diff --git a/docs/user_guide/ar_environment/environment_setup.md b/docs/user_guide/ar_environment/environment_setup.md index 367030c..bbfdbd6 100644 --- a/docs/user_guide/ar_environment/environment_setup.md +++ b/docs/user_guide/ar_environment/environment_setup.md @@ -15,7 +15,7 @@ Here is JSON environment file example where it is assumed that mentioned .obj fi "name": "DICT_APRILTAG_16h5" } "marker_size": 5, - "camera": { + "optic_parameters": { "rms": 0.6, "dimensions": [ 1920, diff --git a/docs/user_guide/areas_of_interest/aoi_scene_projection.md b/docs/user_guide/areas_of_interest/aoi_scene_projection.md index a23d069..4d06e87 100644 --- a/docs/user_guide/areas_of_interest/aoi_scene_projection.md +++ b/docs/user_guide/areas_of_interest/aoi_scene_projection.md @@ -14,8 +14,8 @@ An [AOI3DScene](/argaze/#argaze.AreaOfInterest.AOI3DScene) can be rotated and tr # Assuming pose estimation is done (tvec and rmat) -# Project AOI 3D scene according pose estimation and camera intrinsic parameters -aoi2D_scene = aoi3D_scene.project(tvec, rmat, aruco_camera.K) +# Project AOI 3D scene according pose estimation and optic parameters +aoi2D_scene = aoi3D_scene.project(tvec, rmat, optic_parameters.K) # Draw AOI 2D scene aoi2D_scene.draw(frame) diff --git a/docs/user_guide/aruco_markers/camera_calibration.md b/docs/user_guide/aruco_markers/camera_calibration.md index c8a0be9..ea2c51a 100644 --- a/docs/user_guide/aruco_markers/camera_calibration.md +++ b/docs/user_guide/aruco_markers/camera_calibration.md @@ -5,7 +5,7 @@ Any camera device have to be calibrated to compensate its optical distorsion. ![Camera calibration](../../img/camera_calibration.png) -The first step to calibrate a [ArUcoCamera](/argaze/#argaze.ArUcoMarkers.ArUcoCamera) is to create an [ArUcoBoard](/argaze/#argaze.ArUcoMarkers.ArUcoBoard) like in the code below: +The first step to calibrate a camera is to create an [ArUcoBoard](/argaze/#argaze.ArUcoMarkers.ArUcoBoard) like in the code below: ``` python from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoBoard @@ -20,20 +20,20 @@ aruco_board = ArUcoBoard.ArUcoBoard(7, 5, 5, 3, aruco_dictionary) aruco_board.save('./calibration_board.png', 300) ``` -Then, the calibration process needs to make many different captures of an [ArUcoBoard](/argaze/#argaze.ArUcoMarkers.ArUcoBoard) through the camera and then, pass them to an [ArUcoDetector](/argaze/#argaze.ArUcoMarkers.ArUcoDetector.ArUcoDetector) instance. +Then, the calibration process needs to make many different captures of an [ArUcoBoard](/argaze/#argaze.ArUcoMarkers.ArUcoBoard) through the camera and then, pass them to an [ArUcoDetector](/argaze/#argaze.ArUcoMarkers.ArUcoDetector.ArUcoDetector) instance to detect board corners and store them as calibration data to an [ArUcoOpticCalibrator](/argaze/#argaze.ArUcoMarkers.ArUcoOpticCalibrator) for final calibration process. ![Calibration step](../../img/camera_calibration_step.png) The sample of code below shows how to detect board corners into camera frames, store detected corners then process them to build calibration data and, finally, save it into a JSON file: ``` python -from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoCamera, ArUcoBoard, ArUcoDetector +from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoOpticCalibrator, ArUcoBoard, ArUcoDetector # Create ArUco dictionary aruco_dictionary = ArUcoMarkersDictionary.ArUcoMarkersDictionary('DICT_APRILTAG_16h5') -# Create ArUco camera -aruco_camera = ArUcoCamera.ArUcoCamera(dimensions=(1920, 1080)) +# Create ArUco optic calibrator +aruco_optic_calibrator = ArUcoOpticCalibrator.ArUcoOpticCalibrator() # Create ArUco board of 7 columns and 5 rows with 5 cm squares with 3cm aruco markers inside # Note: This board is the one expected during further board tracking @@ -57,27 +57,35 @@ while video_stream.is_alive(): aruco_detector.draw_board(frame) # Append tracked board data for further calibration processing - aruco_camera.store_calibration_data(aruco_detector.board_corners, aruco_detector.board_corners_identifier) + aruco_optic_calibrator.store_calibration_data(aruco_detector.board_corners, aruco_detector.board_corners_identifier) # Start camera calibration processing for Full HD image resolution print('Calibrating camera...') -aruco_camera.calibrate(expected_aruco_board) +optic_parameters = aruco_optic_calibrator.calibrate(aruco_board, dimensions=(1920, 1080)) -# Print camera calibration data -print('Calibration succeeded!') -print(f'RMS:{aruco_camera.rms}') -print(f'Camera matrix:{aruco_camera.K}') -print(f'Distortion coefficients:{aruco_camera.D}') +if optic_parameters: -# Save camera calibration data -aruco_camera.to_json('calibration.json') + print('\nCalibration succeeded!') + + print(f'\nRMS:\n{optic_parameters.rms}') + print(f'\nDimensions:\n{optic_parameters.dimensions[0]}x{optic_parameters.dimensions[1]}') + print(f'\nCamera matrix:\n{optic_parameters.K}') + print(f'\nDistortion coefficients:\n{optic_parameters.D}') + + optic_parameters.to_json(f'{args.output}/calibration.json') + + print(f'\ncalibration.json file exported into {args.output} folder') + +else: + + print('\nCalibration error.') ``` Then, the camera calibration data are loaded to compensate optical distorsion during [ArUcoMarkers](/argaze/#argaze.ArUcoMarkers.ArUcoMarker) detection: ``` python -from argaze.ArUcoMarkers import ArUcoCamera +from argaze.ArUcoMarkers import ArUcoOpticCalibrator -# Load camera calibration data -aruco_camera = ArUcoCamera.ArUcoCamera.from_json('./calibration.json') +# Load camera optic parameters +optic_parameters = ArUcoOpticCalibrator.OpticParameters.from_json('./calibration.json') ``` diff --git a/docs/user_guide/aruco_markers/introduction.md b/docs/user_guide/aruco_markers/introduction.md index fbf01cf..7da045c 100644 --- a/docs/user_guide/aruco_markers/introduction.md +++ b/docs/user_guide/aruco_markers/introduction.md @@ -10,6 +10,6 @@ The ArGaze [ArUcoMarkers submodule](/argaze/#argaze.ArUcoMarkers) eases markers * [ArUcoMarkersDictionary](/argaze/#argaze.ArUcoMarkers.ArUcoMarkersDictionary) * [ArUcoMarkers](/argaze/#argaze.ArUcoMarkers.ArUcoMarker) * [ArUcoBoard](/argaze/#argaze.ArUcoMarkers.ArUcoBoard) -* [ArUcoCamera](/argaze/#argaze.ArUcoMarkers.ArUcoCamera) +* [ArUcoOpticCalibrator](/argaze/#argaze.ArUcoMarkers.ArUcoOpticCalibrator) * [ArUcoDetector](/argaze/#argaze.ArUcoMarkers.ArUcoDetector) * [ArUcoScene](/argaze/#argaze.ArUcoMarkers.ArUcoScene)
\ No newline at end of file diff --git a/docs/user_guide/aruco_markers/markers_detection.md b/docs/user_guide/aruco_markers/markers_detection.md index d962b7b..f8a23f9 100644 --- a/docs/user_guide/aruco_markers/markers_detection.md +++ b/docs/user_guide/aruco_markers/markers_detection.md @@ -8,7 +8,7 @@ Firstly, the [ArUcoDetector](/argaze/#argaze.ArUcoMarkers.ArUcoDetector.ArUcoDet Notice that extra parameters are passed to detector: see [OpenCV ArUco markers detection parameters documentation](https://docs.opencv.org/4.x/d1/dcd/structcv_1_1aruco_1_1DetectorParameters.html) to know more. ``` python -from argaze.ArUcoMarkers import ArUcoDetector, ArUcoCamera +from argaze.ArUcoMarkers import ArUcoDetector, ArUcoOpticCalibrator # Assuming camera calibration data are loaded @@ -16,7 +16,7 @@ from argaze.ArUcoMarkers import ArUcoDetector, ArUcoCamera extra_parameters = ArUcoDetector.DetectorParameters.from_json('./detector_parameters.json') # Create ArUco detector to track DICT_APRILTAG_16h5 5cm length markers -aruco_detector = ArUcoDetector.ArUcoDetector(camera=aruco_camera, dictionary='DICT_APRILTAG_16h5', marker_size=5, parameters=extra_parameters) +aruco_detector = ArUcoDetector.ArUcoDetector(optic_parameters=optic_parameters, dictionary='DICT_APRILTAG_16h5', marker_size=5, parameters=extra_parameters) ``` Here is [DetectorParameters](/argaze/#argaze.ArUcoMarkers.ArUcoDetector.DetectorParameters) JSON file example: diff --git a/docs/user_guide/utils/demonstrations_scripts.md b/docs/user_guide/utils/demonstrations_scripts.md index 5c4ef2b..adcc8b3 100644 --- a/docs/user_guide/utils/demonstrations_scripts.md +++ b/docs/user_guide/utils/demonstrations_scripts.md @@ -18,7 +18,7 @@ python ./src/argaze/utils/demo_ar_features_run.py -d DEVICE ``` !!! warning - This demonstration assumes that camera calibration step is done and a **calibration.json** has been exported into *./src/argaze/utils/demo_environment/* folder. + This demonstration assumes that camera calibration step is done and a **optic_parameters.json** has been exported into *./src/argaze/utils/demo_environment/* folder. !!! note Use **A3_demo.pdf** file located in *./src/argaze/utils/demo_environment/* folder ready to be printed on A3 paper sheet. diff --git a/docs/user_guide/utils/ready-made_scripts.md b/docs/user_guide/utils/ready-made_scripts.md index 82039c5..4ab8b52 100644 --- a/docs/user_guide/utils/ready-made_scripts.md +++ b/docs/user_guide/utils/ready-made_scripts.md @@ -25,7 +25,7 @@ python ./src/argaze/utils/aruco_calibration_board_export.py 7 5 5 3 DICT_APRILTA ## Camera calibration -Calibrate a camera device (-d DEVICE) using a 7 columns and 5 rows calibration board made of 5cm squares with 3cm markers from *DICT_APRILTAG_16h5* dictionary. Then, export its optical parameters into an *calibration.json* file into *./src/argaze/utils/demo_environment/* folder: +Calibrate a camera device (-d DEVICE) using a 7 columns and 5 rows calibration board made of 5cm squares with 3cm markers from *DICT_APRILTAG_16h5* dictionary. Then, export its optical parameters into an *optic_parameters.json* file into *./src/argaze/utils/demo_environment/* folder: ```shell python ./src/argaze/utils/camera_calibrate.py 7 5 5 3 DICT_APRILTAG_16h5 -d DEVICE -o ./src/argaze/utils/demo_environment diff --git a/src/argaze.test/ArFeatures.py b/src/argaze.test/ArFeatures.py index 3f7972d..5da63d1 100644 --- a/src/argaze.test/ArFeatures.py +++ b/src/argaze.test/ArFeatures.py @@ -37,11 +37,11 @@ class TestArEnvironmentClass(unittest.TestCase): self.assertEqual(ar_environment.aruco_detector.parameters.aprilTagQuadSigma, 2) self.assertEqual(ar_environment.aruco_detector.parameters.aprilTagDeglitch, 1) - # Check ArUco detector camera - self.assertEqual(ar_environment.aruco_detector.camera.rms, 1.0) - self.assertIsNone(numpy.testing.assert_array_equal(ar_environment.aruco_detector.camera.dimensions, [1920, 1080])) - self.assertIsNone(numpy.testing.assert_array_equal(ar_environment.aruco_detector.camera.K, [[1.0, 0.0, 1.0], [0.0, 1.0, 1.0], [0.0, 0.0, 1.0]])) - self.assertIsNone(numpy.testing.assert_array_equal(ar_environment.aruco_detector.camera.D, [-1.0, -0.5, 0.0, 0.5, 1.0])) + # Check ArUco detector optic parameters + self.assertEqual(ar_environment.aruco_detector.optic_parameters.rms, 1.0) + self.assertIsNone(numpy.testing.assert_array_equal(ar_environment.aruco_detector.optic_parameters.dimensions, [1920, 1080])) + self.assertIsNone(numpy.testing.assert_array_equal(ar_environment.aruco_detector.optic_parameters.K, [[1.0, 0.0, 1.0], [0.0, 1.0, 1.0], [0.0, 0.0, 1.0]])) + self.assertIsNone(numpy.testing.assert_array_equal(ar_environment.aruco_detector.optic_parameters.D, [-1.0, -0.5, 0.0, 0.5, 1.0])) # Check environment scenes self.assertEqual(len(ar_environment.scenes), 2) diff --git a/src/argaze.test/ArUcoMarkers/ArUcoCamera.py b/src/argaze.test/ArUcoMarkers/ArUcoCamera.py deleted file mode 100644 index 7a53070..0000000 --- a/src/argaze.test/ArUcoMarkers/ArUcoCamera.py +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env python - -""" """ - -__author__ = "Théo de la Hogue" -__credits__ = [] -__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)" -__license__ = "BSD" - -import unittest -import os - -from argaze.ArUcoMarkers import ArUcoCamera - -import numpy - -class TestArUcoCameraClass(unittest.TestCase): - """Test ArUcoCamera class.""" - - def test_new(self): - """Test ArUcoCamera creation.""" - - # Check defaut camera creation - aruco_camera = ArUcoCamera.ArUcoCamera() - - # Check ArUco camera - self.assertEqual(aruco_camera.rms, 0.0) - - #self.assertEqual(type(aruco_camera.K), numpy.array) - - self.assertIsNone(numpy.testing.assert_array_equal(aruco_camera.dimensions, [0, 0])) - self.assertIsNone(numpy.testing.assert_array_equal(aruco_camera.K, ArUcoCamera.K0)) - self.assertIsNone(numpy.testing.assert_array_equal(aruco_camera.D, ArUcoCamera.D0)) - - def test_from_json(self): - - # Edit camera file path - current_directory = os.path.dirname(os.path.abspath(__file__)) - json_filepath = os.path.join(current_directory, 'utils/camera.json') - - # Load camera calibration - aruco_camera = ArUcoCamera.ArUcoCamera.from_json(json_filepath) - - # Check ArUco camera - self.assertEqual(aruco_camera.rms, 1.0) - self.assertIsNone(numpy.testing.assert_array_equal(aruco_camera.dimensions, [1920, 1080])) - self.assertIsNone(numpy.testing.assert_array_equal(aruco_camera.K, [[1.0, 0.0, 1.0], [0.0, 1.0, 1.0], [0.0, 0.0, 1.0]])) - self.assertIsNone(numpy.testing.assert_array_equal(aruco_camera.D, [-1.0, -0.5, 0.0, 0.5, 1.0])) - -if __name__ == '__main__': - - unittest.main()
\ No newline at end of file diff --git a/src/argaze.test/ArUcoMarkers/ArUcoDetector.py b/src/argaze.test/ArUcoMarkers/ArUcoDetector.py index cd7c90c..750aaa5 100644 --- a/src/argaze.test/ArUcoMarkers/ArUcoDetector.py +++ b/src/argaze.test/ArUcoMarkers/ArUcoDetector.py @@ -11,7 +11,7 @@ import unittest import os import math -from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoCamera, ArUcoDetector, ArUcoBoard +from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoOpticCalibrator, ArUcoDetector, ArUcoBoard import cv2 as cv import numpy @@ -50,7 +50,7 @@ class TestArUcoDetectorClass(unittest.TestCase): # Check ArUcoDetector creation self.assertEqual(aruco_detector.dictionary.name, 'DICT_ARUCO_ORIGINAL') self.assertEqual(aruco_detector.marker_size, 3) - self.assertIsNone(numpy.testing.assert_array_equal(aruco_detector.camera.dimensions, [0, 0])) + self.assertIsNone(numpy.testing.assert_array_equal(aruco_detector.optic_parameters.dimensions, [0, 0])) self.assertEqual(aruco_detector.detected_markers_number, 0) self.assertEqual(aruco_detector.detected_markers, {}) @@ -60,7 +60,7 @@ class TestArUcoDetectorClass(unittest.TestCase): # Check ArUcoDetector creation self.assertEqual(aruco_detector.dictionary.name, 'DICT_APRILTAG_16h5') self.assertEqual(aruco_detector.marker_size, 5.2) - self.assertIsNone(numpy.testing.assert_array_equal(aruco_detector.camera.dimensions, [0, 0])) + self.assertIsNone(numpy.testing.assert_array_equal(aruco_detector.optic_parameters.dimensions, [0, 0])) self.assertEqual(aruco_detector.detected_markers_number, 0) self.assertEqual(aruco_detector.detected_markers, {}) @@ -77,7 +77,7 @@ class TestArUcoDetectorClass(unittest.TestCase): # Check ArUcoDetector creation self.assertEqual(aruco_detector.dictionary.name, 'DICT_ARUCO_ORIGINAL') self.assertEqual(aruco_detector.marker_size, 3) - self.assertIsNone(numpy.testing.assert_array_equal(aruco_detector.camera.dimensions, [1920, 1080])) + self.assertIsNone(numpy.testing.assert_array_equal(aruco_detector.optic_parameters.dimensions, [1920, 1080])) self.assertEqual(aruco_detector.parameters.cornerRefinementMethod, 3) self.assertEqual(aruco_detector.parameters.aprilTagQuadSigma, 2) self.assertEqual(aruco_detector.parameters.aprilTagDeglitch, 1) diff --git a/src/argaze.test/ArUcoMarkers/ArUcoOpticCalibrator.py b/src/argaze.test/ArUcoMarkers/ArUcoOpticCalibrator.py new file mode 100644 index 0000000..45b7669 --- /dev/null +++ b/src/argaze.test/ArUcoMarkers/ArUcoOpticCalibrator.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +""" """ + +__author__ = "Théo de la Hogue" +__credits__ = [] +__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)" +__license__ = "BSD" + +import unittest +import os + +from argaze.ArUcoMarkers import ArUcoOpticCalibrator + +import numpy + +class TestOpticParametersClass(unittest.TestCase): + """Test OpticParameters class.""" + + def test_new(self): + """Test OpticParameters creation.""" + + # Check defaut optic parameters creation + optic_parameters = ArUcoOpticCalibrator.OpticParameters() + + # Check ArUco optic parameters + self.assertEqual(optic_parameters.rms, 0.0) + + #self.assertEqual(type(optic_parameters.K), numpy.array) + + self.assertIsNone(numpy.testing.assert_array_equal(optic_parameters.dimensions, [0, 0])) + self.assertIsNone(numpy.testing.assert_array_equal(optic_parameters.K, ArUcoOpticCalibrator.K0)) + self.assertIsNone(numpy.testing.assert_array_equal(optic_parameters.D, ArUcoOpticCalibrator.D0)) + + def test_from_json(self): + + # Edit optic parameters file path + current_directory = os.path.dirname(os.path.abspath(__file__)) + json_filepath = os.path.join(current_directory, 'utils/optic_parameters.json') + + # Load optic parameters + optic_parameters = ArUcoOpticCalibrator.OpticParameters.from_json(json_filepath) + + # Check ArUco camera + self.assertEqual(optic_parameters.rms, 1.0) + self.assertIsNone(numpy.testing.assert_array_equal(optic_parameters.dimensions, [1920, 1080])) + self.assertIsNone(numpy.testing.assert_array_equal(optic_parameters.K, [[1.0, 0.0, 1.0], [0.0, 1.0, 1.0], [0.0, 0.0, 1.0]])) + self.assertIsNone(numpy.testing.assert_array_equal(optic_parameters.D, [-1.0, -0.5, 0.0, 0.5, 1.0])) + +if __name__ == '__main__': + + unittest.main()
\ No newline at end of file diff --git a/src/argaze.test/ArUcoMarkers/utils/detector.json b/src/argaze.test/ArUcoMarkers/utils/detector.json index a239bf8..8aada6d 100644 --- a/src/argaze.test/ArUcoMarkers/utils/detector.json +++ b/src/argaze.test/ArUcoMarkers/utils/detector.json @@ -3,7 +3,7 @@ "name": "DICT_ARUCO_ORIGINAL" }, "marker_size": 3.0, - "camera": { + "optic_parameters": { "rms": 1.0, "dimensions": [ 1920, diff --git a/src/argaze.test/ArUcoMarkers/utils/camera.json b/src/argaze.test/ArUcoMarkers/utils/optic_parameters.json index 988731c..988731c 100644 --- a/src/argaze.test/ArUcoMarkers/utils/camera.json +++ b/src/argaze.test/ArUcoMarkers/utils/optic_parameters.json diff --git a/src/argaze.test/utils/environment.json b/src/argaze.test/utils/environment.json index 57d04cf..df1c771 100644 --- a/src/argaze.test/utils/environment.json +++ b/src/argaze.test/utils/environment.json @@ -5,7 +5,7 @@ "name": "DICT_ARUCO_ORIGINAL" }, "marker_size": 3.0, - "camera": { + "optic_parameters": { "rms": 1.0, "dimensions": [ 1920, diff --git a/src/argaze/ArFeatures.py b/src/argaze/ArFeatures.py index 5899545..03f57f6 100644 --- a/src/argaze/ArFeatures.py +++ b/src/argaze/ArFeatures.py @@ -65,22 +65,22 @@ class ArEnvironment(): new_aruco_dictionary = ArUcoMarkersDictionary.ArUcoMarkersDictionary(**new_detector_data.pop('dictionary')) new_marker_size = new_detector_data.pop('marker_size') - # Check aruco_camera value type - aruco_camera_value = new_detector_data.pop('camera') + # Check optic_parameters value type + optic_parameters_value = new_detector_data.pop('optic_parameters') # str: relative path to .json file - if type(aruco_camera_value) == str: + if type(optic_parameters_value) == str: - aruco_camera_value = os.path.join(working_directory, aruco_camera_value) - new_aruco_camera = ArUcoCamera.ArUcoCamera.from_json(aruco_camera_value) + optic_parameters_value = os.path.join(working_directory, optic_parameters_value) + new_optic_parameters = ArUcoOpticCalibrator.OpticParameters.from_json(optic_parameters_value) # dict: else: - new_aruco_camera = ArUcoCamera.ArUcoCamera(**aruco_camera_value) + new_optic_parameters = ArUcoOpticCalibrator.OpticParameters(**optic_parameters_value) new_aruco_detecor_parameters = ArUcoDetector.DetectorParameters(**new_detector_data.pop('parameters')) - new_aruco_detector = ArUcoDetector.ArUcoDetector(new_aruco_dictionary, new_marker_size, new_aruco_camera, new_aruco_detecor_parameters) + new_aruco_detector = ArUcoDetector.ArUcoDetector(new_aruco_dictionary, new_marker_size, new_optic_parameters, new_aruco_detecor_parameters) new_scenes = {} for scene_name, scene_data in data.pop('scenes').items(): @@ -223,7 +223,7 @@ class ArScene(): tvec = self.aoi_scene.center*[-1, 1, 0] + [0, 0, scene_size[1]] rvec = numpy.array([[-numpy.pi, 0.0, 0.0]]) - # Edit intrinsic camera parameter to capture whole scene + # Edit optic intrinsic parameter to capture whole scene K = numpy.array([[scene_size[1]/scene_size[0], 0.0, 0.5], [0.0, 1., 0.5], [0.0, 0.0, 1.0]]) return self.aoi_scene.project(tvec, rvec, K) @@ -319,7 +319,7 @@ class ArScene(): aoi_scene_copy = self.aoi_scene.copy() - aoi_scene_projection = aoi_scene_copy.project(tvec, rvec, self._environment.aruco_detector.camera.K) + aoi_scene_projection = aoi_scene_copy.project(tvec, rvec, self._environment.aruco_detector.optic_parameters.K) # Warn user when the projected scene is empty if len(aoi_scene_projection) == 0: @@ -379,7 +379,7 @@ class ArScene(): frame: where to draw """ - self.aruco_scene.draw_axis(frame, self._environment.aruco_detector.camera.K, self._environment.aruco_detector.camera.D) + self.aruco_scene.draw_axis(frame, self._environment.aruco_detector.optic_parameters.K, self._environment.aruco_detector.optic_parameters.D) def draw_places(self, frame: numpy.array): """ @@ -389,6 +389,6 @@ class ArScene(): frame: where to draw """ - self.aruco_scene.draw_places(frame, self._environment.aruco_detector.camera.K, self._environment.aruco_detector.camera.D) + self.aruco_scene.draw_places(frame, self._environment.aruco_detector.optic_parameters.K, self._environment.aruco_detector.optic_parameters.D) diff --git a/src/argaze/ArUcoMarkers/ArUcoDetector.py b/src/argaze/ArUcoMarkers/ArUcoDetector.py index 4392e7a..5891f16 100644 --- a/src/argaze/ArUcoMarkers/ArUcoDetector.py +++ b/src/argaze/ArUcoMarkers/ArUcoDetector.py @@ -12,7 +12,7 @@ from dataclasses import dataclass, field import json from collections import Counter -from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoMarker, ArUcoCamera +from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoMarker, ArUcoOpticCalibrator import numpy import cv2 as cv @@ -24,7 +24,7 @@ ArUcoMarkerDictionaryType = TypeVar('ArUcoMarkerDictionary', bound="ArUcoMarkerD ArUcoMarkerType = TypeVar('ArUcoMarker', bound="ArUcoMarker") # Type definition for type annotation convenience -ArUcoCameraType = TypeVar('ArUcoCamera', bound="ArUcoCamera") +OpticParametersType = TypeVar('OpticParameters', bound="OpticParameters") # Type definition for type annotation convenience DetectorParametersType = TypeVar('DetectorParameters', bound="DetectorParameters") @@ -127,8 +127,8 @@ class ArUcoDetector(): marker_size: float = field(default=0.) """Size of ArUco markers to detect in centimeter.""" - camera: ArUcoCamera.ArUcoCamera = field(default_factory=ArUcoCamera.ArUcoCamera) - """ArUco camera to use to setup optical parameters.""" + optic_parameters: ArUcoOpticCalibrator.OpticParameters = field(default_factory=ArUcoOpticCalibrator.OpticParameters) + """Optic parameters to use for ArUco detection into frame.""" parameters: DetectorParameters = field(default_factory=DetectorParameters) """ArUco detector parameters.""" @@ -160,18 +160,18 @@ class ArUcoDetector(): new_dictionary = ArUcoMarkersDictionary.ArUcoMarkersDictionary(**data.pop('dictionary')) new_marker_size = data.pop('marker_size') - new_camera = ArUcoCamera.ArUcoCamera(**data.pop('camera')) + new_optic_parameters = ArUcoOpticCalibrator.OpticParameters(**data.pop('optic_parameters')) new_parameters = DetectorParameters(**data.pop('parameters')) - return ArUcoDetector(new_dictionary, new_marker_size, new_camera, new_parameters) + return ArUcoDetector(new_dictionary, new_marker_size, new_optic_parameters, new_parameters) def __str__(self) -> str: """String display""" output = f'\n\tDictionary: {self.dictionary}\n' output += f'\tMarker size: {self.marker_size} cm\n\n' - output += f'\tCamera:\n{self.camera}\n' - output += f'\tParameters:\n{self.parameters}' + output += f'\tOptic parameters:\n{self.optic_parameters}\n' + output += f'\tDetection Parameters:\n{self.parameters}' return output @@ -240,7 +240,7 @@ class ArUcoDetector(): # Estimate pose of selected markers if len(selected_markers_corners) > 0: - markers_rvecs, markers_tvecs, markers_points = aruco.estimatePoseSingleMarkers(selected_markers_corners, self.marker_size, numpy.array(self.camera.K), numpy.array(self.camera.D)) + markers_rvecs, markers_tvecs, markers_points = aruco.estimatePoseSingleMarkers(selected_markers_corners, self.marker_size, numpy.array(self.optic_parameters.K), numpy.array(self.optic_parameters.D)) for i, marker_id in enumerate(selected_markers_ids): @@ -267,7 +267,7 @@ class ArUcoDetector(): for marker_id, marker in self.__detected_markers.items(): - marker.draw(frame, self.camera.K, self.camera.D) + marker.draw(frame, self.optic_parameters.K, self.optic_parameters.D) def detect_board(self, frame: numpy.array, board, expected_markers_number): """Detect ArUco markers board in frame setting up the number of detected markers needed to agree detection. diff --git a/src/argaze/ArUcoMarkers/ArUcoCamera.py b/src/argaze/ArUcoMarkers/ArUcoOpticCalibrator.py index 205c591..ed33c95 100644 --- a/src/argaze/ArUcoMarkers/ArUcoCamera.py +++ b/src/argaze/ArUcoMarkers/ArUcoOpticCalibrator.py @@ -17,14 +17,14 @@ import cv2 import cv2.aruco as aruco K0 = numpy.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 0.]]) -"""Define default camera intrinsic parameters matrix.""" +"""Define default optic intrinsic parameters matrix.""" D0 = numpy.array([0.0, 0.0, 0.0, 0.0, 0.0]) -"""Define default camera distorsion coefficients vector.""" +"""Define default optic distorsion coefficients vector.""" @dataclass -class CalibrationData(): - """Define optical camera calibration data.""" +class OpticParameters(): + """Define optic parameters outputed by optic calibrator.""" rms: float = field(default=0) """Root Mean Square error of calibration.""" @@ -44,7 +44,7 @@ class CalibrationData(): with open(json_filepath) as calibration_file: - return CalibrationData(**json.load(calibration_file)) + return OpticParameters(**json.load(calibration_file)) def to_json(self, json_filepath): """Save optical parameters into .json file.""" @@ -84,24 +84,31 @@ class CalibrationData(): except: pass -class ArUcoCamera(CalibrationData): - """Handle camera calibration process.""" +class ArUcoOpticCalibrator(): + """Handle optic calibration process.""" - def __init__(self, **kwargs): - - super().__init__(**kwargs) + def __init__(self,): # Calibration data self.__corners_set_number = 0 self.__corners_set = [] self.__corners_set_ids = [] - def calibrate(self, board): - """Retrieve camera K and D from stored calibration data.""" + def calibrate(self, board, dimensions:tuple = (0, 0)) -> OpticParameters: + """Retrieve K and D parameters from stored calibration data. + + Parameters: + dimensions: camera frame dimensions + + Returns: + Optic parameters + """ if self.__corners_set_number > 0: - self.rms, self.K, self.D, r, t = aruco.calibrateCameraCharuco(self.__corners_set, self.__corners_set_ids, board.model, self.dimensions, None, None) + rms, K, D, r, t = aruco.calibrateCameraCharuco(self.__corners_set, self.__corners_set_ids, board.model, dimensions, None, None) + + return OpticParameters(rms, dimensions, K, D) def reset_calibration_data(self): """Clear all calibration data.""" diff --git a/src/argaze/ArUcoMarkers/ArUcoScene.py b/src/argaze/ArUcoMarkers/ArUcoScene.py index 672e31c..85c3fbf 100644 --- a/src/argaze/ArUcoMarkers/ArUcoScene.py +++ b/src/argaze/ArUcoMarkers/ArUcoScene.py @@ -14,7 +14,7 @@ import math import itertools import re -from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoMarker, ArUcoCamera +from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoMarker, ArUcoOpticCalibrator import numpy import cv2 as cv diff --git a/src/argaze/ArUcoMarkers/__init__.py b/src/argaze/ArUcoMarkers/__init__.py index ac1bee4..350c69e 100644 --- a/src/argaze/ArUcoMarkers/__init__.py +++ b/src/argaze/ArUcoMarkers/__init__.py @@ -1,4 +1,4 @@ """ Handle [OpenCV ArUco markers](https://docs.opencv.org/4.x/d5/dae/tutorial_aruco_detection.html): generate and detect markers, calibrate camera, describe scene, ... """ -__all__ = ['ArUcoMarkersDictionary', 'ArUcoMarker', 'ArUcoBoard', 'ArUcoCamera', 'ArUcoDetector', 'ArUcoScene', 'utils']
\ No newline at end of file +__all__ = ['ArUcoMarkersDictionary', 'ArUcoMarker', 'ArUcoBoard', 'ArUcoOpticCalibrator', 'ArUcoDetector', 'ArUcoScene', 'utils']
\ No newline at end of file diff --git a/src/argaze/AreaOfInterest/AOI3DScene.py b/src/argaze/AreaOfInterest/AOI3DScene.py index dbb0424..2272206 100644 --- a/src/argaze/AreaOfInterest/AOI3DScene.py +++ b/src/argaze/AreaOfInterest/AOI3DScene.py @@ -24,10 +24,10 @@ R0 = numpy.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]) """Define no rotation matrix.""" K0 = numpy.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 0.]]) -"""Define default camera intrinsic parameters matrix.""" +"""Define default optic intrinsic parameters matrix.""" D0 = numpy.array([0.0, 0.0, 0.0, 0.0, 0.0]) -"""Define default camera distorsion coefficients vector.""" +"""Define default optic distorsion coefficients vector.""" AOI3DSceneType = TypeVar('AOI3DScene', bound="AOI3DScene") # Type definition for type annotation convenience diff --git a/src/argaze/utils/camera_calibrate.py b/src/argaze/utils/camera_calibrate.py index b590767..c42b721 100644 --- a/src/argaze/utils/camera_calibrate.py +++ b/src/argaze/utils/camera_calibrate.py @@ -11,23 +11,23 @@ import argparse import os import time -from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoBoard, ArUcoDetector, ArUcoCamera +from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoBoard, ArUcoDetector, ArUcoOpticCalibrator import cv2 def main(): """ - Captures board pictures and finally outputs camera calibration data into a calibration.json file. + Captures board pictures and finally outputs optic parameter into a calibration.json file. - Export and print a calibration board using aruco_calibration_board_export.py script. - Place the calibration board face to the camera. - Move the calibration board in a manner to view it entirely on screen in various 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. + - Once enough pictures have been captured (~20), press Esc key then, wait for optic parameters 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/) + - [Optic calibration using ArUco marker tutorial](https://automaticaddison.com/how-to-perform-camera-calibration-using-opencv/) """ # Manage arguments @@ -48,8 +48,8 @@ def main(): frame_width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) - # Create aruco camera - aruco_camera = ArUcoCamera.ArUcoCamera(dimensions=(frame_width, frame_height)) + # Create aruco optic calibrator + aruco_optic_calibrator = ArUcoOpticCalibrator.ArUcoOpticCalibrator() # Create aruco board aruco_board = ArUcoBoard.ArUcoBoard(args.columns, args.rows, args.square_size, args.marker_size, args.dictionary) @@ -57,7 +57,7 @@ def main(): # Create aruco detector aruco_detector = ArUcoDetector.ArUcoDetector(dictionary=args.dictionary, marker_size=args.marker_size) - print(f'{aruco_camera.dimensions[0]}x{aruco_camera.dimensions[1]} pixels camera calibration starts') + print(f'{frame_width}x{frame_height} pixels camera calibration starts') print("Waiting for calibration board...") expected_markers_number = aruco_board.markers_number @@ -80,8 +80,8 @@ def main(): aruco_detector.draw_detected_markers(video_frame) # Draw current calibration data count - cv2.putText(video_frame, f'Capture: {aruco_camera.calibration_data_count}', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA) - cv2.imshow('Camera Calibration', video_frame) + cv2.putText(video_frame, f'Capture: {aruco_optic_calibrator.calibration_data_count}', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA) + cv2.imshow('Optic Calibration', video_frame) # If all board corners are detected if aruco_detector.board_corners_number == expected_corners_number: @@ -90,9 +90,9 @@ def main(): aruco_detector.draw_board(video_frame) # Append calibration data - aruco_camera.store_calibration_data(aruco_detector.board_corners, aruco_detector.board_corners_identifier) + aruco_optic_calibrator.store_calibration_data(aruco_detector.board_corners, aruco_detector.board_corners_identifier) - cv2.imshow('Camera Calibration', video_frame) + cv2.imshow('Optic Calibration', video_frame) # Stop calibration by pressing 'Esc' key if cv2.waitKey(1) == 27: @@ -106,17 +106,24 @@ def main(): cv2.destroyAllWindows() print('\nCalibrating camera...') - aruco_camera.calibrate(aruco_board) + optic_parameters = aruco_optic_calibrator.calibrate(aruco_board, dimensions=(frame_width, frame_height)) - print('\nCalibration succeeded!') - print(f'\nRMS:\n{aruco_camera.rms}') - print(f'\nDimensions:\n{frame_width}x{frame_height}') - print(f'\nCamera matrix:\n{aruco_camera.K}') - print(f'\nDistortion coefficients:\n{aruco_camera.D}') + if optic_parameters: - aruco_camera.to_json(f'{args.output}/calibration.json') + print('\nCalibration succeeded!') - print(f'\ncalibration.json file exported into {args.output} folder') + print(f'\nRMS:\n{optic_parameters.rms}') + print(f'\nDimensions:\n{optic_parameters.dimensions[0]}x{optic_parameters.dimensions[1]}') + print(f'\nCamera matrix:\n{optic_parameters.K}') + print(f'\nDistortion coefficients:\n{optic_parameters.D}') + + optic_parameters.to_json(f'{args.output}/optic_parameters.json') + + print(f'\ncalibration.json file exported into {args.output} folder') + + else: + + print('\nCalibration error.') if __name__ == '__main__': diff --git a/src/argaze/utils/demo_environment/setup.json b/src/argaze/utils/demo_environment/setup.json index d67b53f..58c7c0d 100644 --- a/src/argaze/utils/demo_environment/setup.json +++ b/src/argaze/utils/demo_environment/setup.json @@ -5,7 +5,7 @@ "name": "DICT_APRILTAG_16h5" }, "marker_size": 5, - "camera": "calibration.json", + "optic_parameters": "optic_parameters.json", "parameters": { "cornerRefinementMethod": 1, "aprilTagQuadSigma": 2, diff --git a/src/argaze/utils/demo_heatmap_run.py b/src/argaze/utils/demo_heatmap_run.py index 3e6bb63..df98d33 100644 --- a/src/argaze/utils/demo_heatmap_run.py +++ b/src/argaze/utils/demo_heatmap_run.py @@ -31,7 +31,7 @@ def main(): cv2.imshow(window_name, aoi_frame.heatmap) - # Stop calibration by pressing 'Esc' key + # Stop and save picture by pressing 'Esc' key if cv2.waitKey(10) == 27: current_directory = os.path.dirname(os.path.abspath(__file__)) diff --git a/src/argaze/utils/environment_edit.py b/src/argaze/utils/environment_edit.py index 4fab715..7b6c2fd 100644 --- a/src/argaze/utils/environment_edit.py +++ b/src/argaze/utils/environment_edit.py @@ -164,7 +164,7 @@ def main(): if draw_grid: cv2.putText(video_frame, f'Grid at {z_grid} cm', (500, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA) - ar_environment.aruco_detector.camera.draw(video_frame, frame_width/10, frame_height/10, z_grid, color=(127, 127, 127)) + ar_environment.aruco_detector.optic_parameters.draw(video_frame, frame_width/10, frame_height/10, z_grid, color=(127, 127, 127)) # Write timing cv2.putText(video_frame, f'Time: {int(current_frame_time)} ms', (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA) |