aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThéo de la Hogue2022-05-10 18:03:38 +0200
committerThéo de la Hogue2022-05-10 18:03:38 +0200
commite716c1e60d175a8f40617247d437d47c9348ba42 (patch)
treeb4f9c3ec4f2e327d73061ad4310603ac2cbfbf9c
parentca624af9d6f590e64c70a60ce2fc65f3a755ff2d (diff)
downloadargaze-e716c1e60d175a8f40617247d437d47c9348ba42.zip
argaze-e716c1e60d175a8f40617247d437d47c9348ba42.tar.gz
argaze-e716c1e60d175a8f40617247d437d47c9348ba42.tar.bz2
argaze-e716c1e60d175a8f40617247d437d47c9348ba42.tar.xz
Allowing to load a configuration file to setup aruco marker detection parameters.
-rw-r--r--src/argaze/ArUcoMarkers/ArUcoTracker.py71
1 files changed, 64 insertions, 7 deletions
diff --git a/src/argaze/ArUcoMarkers/ArUcoTracker.py b/src/argaze/ArUcoMarkers/ArUcoTracker.py
index 1d67221..19f4643 100644
--- a/src/argaze/ArUcoMarkers/ArUcoTracker.py
+++ b/src/argaze/ArUcoMarkers/ArUcoTracker.py
@@ -1,11 +1,46 @@
#!/usr/bin/env python
+import json
+
from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoCamera
import numpy
import cv2 as cv
import cv2.aruco as aruco
+ArUcoTrackerParameters = [
+ 'adaptiveThreshConstant',
+ 'adaptiveThreshWinSizeMax',
+ 'adaptiveThreshWinSizeMin',
+ 'adaptiveThreshWinSizeStep',
+ 'aprilTagCriticalRad',
+ 'aprilTagDeglitch',
+ 'aprilTagMaxLineFitMse',
+ 'aprilTagMaxNmaxima',
+ 'aprilTagMinClusterPixels',
+ 'aprilTagMinWhiteBlackDiff',
+ 'aprilTagQuadDecimate',
+ 'aprilTagQuadSigma',
+ 'cornerRefinementMaxIterations',
+ 'cornerRefinementMethod',
+ 'cornerRefinementMinAccuracy',
+ 'cornerRefinementWinSize',
+ 'markerBorderBits',
+ 'minMarkerPerimeterRate',
+ 'maxMarkerPerimeterRate',
+ 'minMarkerDistanceRate',
+ 'detectInvertedMarker',
+ 'errorCorrectionRate',
+ 'maxErroneousBitsInBorderRate',
+ 'minCornerDistanceRate',
+ 'minDistanceToBorder',
+ 'minOtsuStdDev',
+ 'perspectiveRemoveIgnoredMarginPerCell',
+ 'perspectiveRemovePixelPerCell',
+ 'polygonalApproxAccuracyRate'
+]
+"""All parameters are detailled on [opencv page](https://docs.opencv.org/4.x/d1/dcd/structcv_1_1aruco_1_1DetectorParameters.html)"""
+
class ArUcoTracker():
"""Track ArUco markers into a frame."""
@@ -22,8 +57,8 @@ class ArUcoTracker():
self.__camera = camera
# setup ArUco detection parameters
- self.__aruco_param = aruco.DetectorParameters_create()
- self.__aruco_param.cornerRefinementMethod = aruco.CORNER_REFINE_CONTOUR
+ self.__detector_parameters = aruco.DetectorParameters_create()
+ self.__detector_parameters.cornerRefinementMethod = aruco.CORNER_REFINE_CONTOUR
# define tracked markers data
self.__markers_corners = []
@@ -38,8 +73,30 @@ class ArUcoTracker():
self.__board_corners = []
self.__board_corners_ids = []
- def __del__(self):
- pass
+ def load_configuration_file(self, configuration_filepath):
+ """Load aruco detection parameters from .json file.
+ """
+ print(f'ArUcoTracker configuration for {self.__aruco_dict.get_markers_format()} markers detection:')
+ with open(configuration_filepath) as configuration_file:
+
+ configuration = json.load(configuration_file)
+
+ for key, value in configuration.items():
+
+ try:
+ setattr(self.__detector_parameters, key, value)
+ print(f'\t{key}: {getattr(self.__detector_parameters, key)}')
+
+ except AttributeError as error:
+
+ print(error)
+
+ def print_configuration(self):
+ """Print aruco detection parameters."""
+
+ print(f'ArUcoTracker configuration for {self.__aruco_dict.get_markers_format()} markers detection:')
+ for parameters in ArUcoTrackerParameters:
+ print(f'\t{parameters}: {getattr(self.__detector_parameters, parameters)}')
def track(self, frame, estimate_pose = True):
"""Track ArUco markers in frame."""
@@ -48,7 +105,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.__aruco_param)
+ self.__markers_corners, self.__markers_ids, rejectedPoints = aruco.detectMarkers(gray, self.__aruco_dict.get_markers(), parameters = self.__detector_parameters)
if len(self.__markers_corners) > 0 and estimate_pose:
@@ -68,7 +125,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.__aruco_param)
+ self.__markers_corners, self.__markers_ids, rejectedPoints = aruco.detectMarkers(gray, self.__aruco_dict.get_markers(), parameters = self.__detector_parameters)
# if all board markers are detected
if self.get_markers_number() == expected_markers_number:
@@ -111,7 +168,7 @@ class ArUcoTracker():
def get_markers_ids(self):
"""Get tracked markers identifers."""
- return self.__markers_ids
+ return [i[0] for i in self.__markers_ids]
def get_marker_corners(self, i):
"""Get marker i corners."""