diff options
Diffstat (limited to 'src/argaze/utils/aruco_markers_group_export.py')
-rw-r--r-- | src/argaze/utils/aruco_markers_group_export.py | 283 |
1 files changed, 146 insertions, 137 deletions
diff --git a/src/argaze/utils/aruco_markers_group_export.py b/src/argaze/utils/aruco_markers_group_export.py index 46507b8..569ba6b 100644 --- a/src/argaze/utils/aruco_markers_group_export.py +++ b/src/argaze/utils/aruco_markers_group_export.py @@ -10,7 +10,7 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with -this program. If not, see <http://www.gnu.org/licenses/>. +this program. If not, see <https://www.gnu.org/licenses/>. """ __author__ = "Théo de la Hogue" @@ -27,199 +27,208 @@ from argaze import DataFeatures from argaze.ArUcoMarkers import ArUcoDetector, ArUcoOpticCalibrator, ArUcoMarkersGroup from argaze.utils import UtilsFeatures -def main(): - """ - Detect DICTIONARY and SIZE ArUco markers inside a MOVIE frame then, export detected ArUco markers group as .obj file into an OUTPUT folder. - """ - - # Manage arguments - parser = argparse.ArgumentParser(description=main.__doc__.split('-')[0]) - parser.add_argument('movie', metavar='MOVIE', type=str, default=None, help='movie path') - parser.add_argument('dictionary', metavar='DICTIONARY', type=str, default=None, help='expected ArUco markers dictionary') - parser.add_argument('size', metavar='SIZE', type=float, default=None, help='expected ArUco markers size (in cm)') - - parser.add_argument('-p', '--parameters', metavar='PARAMETERS', type=str, default=None, help='ArUco detector parameters file') - parser.add_argument('-op', '--optic_parameters', metavar='OPTIC_PARAMETERS', type=str, default=None, help='ArUco detector optic parameters file') - - parser.add_argument('-s', '--start', metavar='START', type=float, default=0., help='start time in second') - parser.add_argument('-o', '--output', metavar='OUTPUT', type=str, default='.', help='export folder path') - parser.add_argument('-v', '--verbose', action='store_true', default=False, help='enable verbose mode to print information in console') - args = parser.parse_args() - - # Load movie - video_capture = cv2.VideoCapture(args.movie) +def main(): + """ + Detect DICTIONARY and SIZE ArUco markers inside a MOVIE frame then, export detected ArUco markers group as .obj file into an OUTPUT folder. + """ - video_fps = video_capture.get(cv2.CAP_PROP_FPS) - image_width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)) - image_height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) + # Manage arguments + parser = argparse.ArgumentParser(description=main.__doc__.split('-')[0]) + parser.add_argument('movie', metavar='MOVIE', type=str, default=None, help='movie path') + parser.add_argument('dictionary', metavar='DICTIONARY', type=str, default=None, + help='expected ArUco markers dictionary') + parser.add_argument('size', metavar='SIZE', type=float, default=None, help='expected ArUco markers size (in cm)') - # Edit ArUco detector configuration - configuration = { - "dictionary": args.dictionary - } + parser.add_argument('-p', '--parameters', metavar='PARAMETERS', type=str, default=None, + help='ArUco detector parameters file') + parser.add_argument('-op', '--optic_parameters', metavar='OPTIC_PARAMETERS', type=str, default=None, + help='ArUco detector optic parameters file') - if args.parameters: + parser.add_argument('-s', '--start', metavar='START', type=float, default=0., help='start time in second') + parser.add_argument('-o', '--output', metavar='OUTPUT', type=str, default='.', help='export folder path') + parser.add_argument('-v', '--verbose', action='store_true', default=False, + help='enable verbose mode to print information in console') - configuration["parameters"] = args.parameters + args = parser.parse_args() - if args.optic_parameters: + # Load movie + video_capture = cv2.VideoCapture(args.movie) - configuration["optic_parameters"] = args.optic_parameters + video_fps = video_capture.get(cv2.CAP_PROP_FPS) + image_width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)) + image_height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) - # Load ArUco detector configuration - aruco_detector = DataFeatures.from_dict(ArUcoDetector.ArUcoDetector, configuration) + # Edit ArUco detector configuration + configuration = { + "dictionary": args.dictionary + } - if args.verbose: + if args.parameters: + configuration["parameters"] = args.parameters - print(aruco_detector) + if args.optic_parameters: + configuration["optic_parameters"] = args.optic_parameters - # Create empty ArUco scene - aruco_markers_group = None + # Load ArUco detector configuration + aruco_detector = DataFeatures.from_dict(ArUcoDetector.ArUcoDetector, configuration) - # Edit draw parameters - draw_parameters = { - "color": [255, 255, 255], - "draw_axes": { - "thickness": 4 - } - } + if args.verbose: + print(aruco_detector) - # Create a window - cv2.namedWindow("Export detected ArUco markers", cv2.WINDOW_AUTOSIZE) + # Create empty ArUco scene + aruco_markers_group = None - # Init image selection - current_image_index = -1 - _, current_image = video_capture.read() - next_image_index = int(args.start * video_fps) - refresh = False + # Edit draw parameters + draw_parameters = { + "color": [255, 255, 255], + "draw_axes": { + "thickness": 4 + } + } - # Waiting for 'ctrl+C' interruption - with contextlib.suppress(KeyboardInterrupt): + # Create a window + cv2.namedWindow("Export detected ArUco markers", cv2.WINDOW_AUTOSIZE) - while True: + # Init image selection + current_image_index = -1 + _, current_image = video_capture.read() + next_image_index = int(args.start * video_fps) + refresh = False - # Select a new image and detect markers once - if next_image_index != current_image_index or refresh: + # Waiting for 'ctrl+C' interruption + with contextlib.suppress(KeyboardInterrupt): - video_capture.set(cv2.CAP_PROP_POS_FRAMES, next_image_index) + while True: - success, video_image = video_capture.read() + # Select a new image and detect markers once + if next_image_index != current_image_index or refresh: - video_height, video_width, _ = video_image.shape + video_capture.set(cv2.CAP_PROP_POS_FRAMES, next_image_index) - # Create default optic parameters adapted to frame size - if aruco_detector.optic_parameters is None: + success, video_image = video_capture.read() - # Note: The choice of 1000 for default focal length should be discussed... - aruco_detector.optic_parameters = ArUcoOpticCalibrator.OpticParameters(rms=-1, dimensions=(video_width, video_height), K=ArUcoOpticCalibrator.K0(focal_length=(1000., 1000.), width=video_width, height=video_height)) + video_height, video_width, _ = video_image.shape - if success: + # Create default optic parameters adapted to frame size + if aruco_detector.optic_parameters is None: + # Note: The choice of 1000 for default focal length should be discussed... + aruco_detector.optic_parameters = ArUcoOpticCalibrator.OpticParameters(rms=-1, dimensions=( + video_width, video_height), K=ArUcoOpticCalibrator.K0(focal_length=(1000., 1000.), + width=video_width, height=video_height)) - # Refresh once - refresh = False + if success: - current_image_index = video_capture.get(cv2.CAP_PROP_POS_FRAMES) - 1 - current_image_time = video_capture.get(cv2.CAP_PROP_POS_MSEC) + # Refresh once + refresh = False - try: + current_image_index = video_capture.get(cv2.CAP_PROP_POS_FRAMES) - 1 + current_image_time = video_capture.get(cv2.CAP_PROP_POS_MSEC) - # Detect and project AR features - aruco_detector.detect_markers(video_image) + try: - # Estimate all detected markers pose - aruco_detector.estimate_markers_pose(args.size) + # Detect and project AR features + aruco_detector.detect_markers(video_image) - # Build aruco scene from detected markers - aruco_markers_group = ArUcoMarkersGroup.ArUcoMarkersGroup(aruco_detector.dictionary, aruco_detector.detected_markers()) + # Estimate all detected markers pose + aruco_detector.estimate_markers_pose(args.size) - # Detection succeeded - exception = None + # Build aruco scene from detected markers + aruco_markers_group = ArUcoMarkersGroup.ArUcoMarkersGroup(aruco_detector.dictionary, + aruco_detector.detected_markers()) - # Write errors - except Exception as e: + # Detection succeeded + exception = None - aruco_markers_group = None + # Write errors + except Exception as e: - exception = e + aruco_markers_group = None - # Draw detected markers - aruco_detector.draw_detected_markers(video_image, draw_parameters) + exception = e - # Write detected markers - cv2.putText(video_image, f'Detecting markers {list(aruco_detector.detected_markers().keys())}', (20, video_height-40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA) + # Draw detected markers + aruco_detector.draw_detected_markers(video_image, draw_parameters) - # Write timing - cv2.putText(video_image, f'Frame at {int(current_image_time)}ms', (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA) + # Write detected markers + cv2.putText(video_image, f'Detecting markers {list(aruco_detector.detected_markers().keys())}', + (20, video_height - 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA) - # Write exception - if exception is not None: + # Write timing + cv2.putText(video_image, f'Frame at {int(current_image_time)}ms', (20, 40), + cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA) - cv2.putText(video_image, f'error: {exception}', (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 1, cv2.LINE_AA) + # Write exception + if exception is not None: + cv2.putText(video_image, f'error: {exception}', (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, + (0, 255, 255), 1, cv2.LINE_AA) - # Write documentation - cv2.putText(video_image, f'<- previous image', (video_width-500, video_height-160), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 1, cv2.LINE_AA) - cv2.putText(video_image, f'-> next image', (video_width-500, video_height-120), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 1, cv2.LINE_AA) - cv2.putText(video_image, f'r: reload config', (video_width-500, video_height-80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 1, cv2.LINE_AA) - cv2.putText(video_image, f'Ctrl+s: export ArUco markers', (video_width-500, video_height-40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 1, cv2.LINE_AA) + # Write documentation + cv2.putText(video_image, f'<- previous image', (video_width - 500, video_height - 160), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 1, cv2.LINE_AA) + cv2.putText(video_image, f'-> next image', (video_width - 500, video_height - 120), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 1, cv2.LINE_AA) + cv2.putText(video_image, f'r: reload config', (video_width - 500, video_height - 80), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 1, cv2.LINE_AA) + cv2.putText(video_image, f'Ctrl+s: export ArUco markers', (video_width - 500, video_height - 40), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 1, cv2.LINE_AA) - # Copy image - current_image = video_image.copy() + # Copy image + current_image = video_image.copy() - # Keep last image - else: + # Keep last image + else: - video_image = current_image.copy() + video_image = current_image.copy() - key_pressed = cv2.waitKey(10) + key_pressed = cv2.waitKey(10) - #if key_pressed != -1: - # print(key_pressed) + #if key_pressed != -1: + # print(key_pressed) - # Select previous image with left arrow - if key_pressed == 2: - next_image_index -= 1 + # Select previous image with left arrow + if key_pressed == 2: + next_image_index -= 1 - # Select next image with right arrow - if key_pressed == 3: - next_image_index += 1 + # Select next image with right arrow + if key_pressed == 3: + next_image_index += 1 - # Clip image index - if next_image_index < 0: - next_image_index = 0 + # Clip image index + if next_image_index < 0: + next_image_index = 0 - # r: reload configuration - if key_pressed == 114: + # r: reload configuration + if key_pressed == 114: + aruco_detector = DataFeatures.from_dict(ArUcoDetector.ArUcoDetector, configuration) + refresh = True + print('Configuration reloaded') - aruco_detector = DataFeatures.from_dict(ArUcoDetector.ArUcoDetector, configuration) - refresh = True - print('Configuration reloaded') + # Save selected marker edition using 'Ctrl + s' + if key_pressed == 19: - # Save selected marker edition using 'Ctrl + s' - if key_pressed == 19: + if aruco_markers_group: - if aruco_markers_group: + aruco_markers_group.to_obj(f'{args.output}/{int(current_image_time)}-aruco_markers_group.obj') + print(f'ArUco markers saved into {args.output}') - aruco_markers_group.to_obj(f'{args.output}/{int(current_image_time)}-aruco_markers_group.obj') - print(f'ArUco markers saved into {args.output}') + else: - else: + print(f'No ArUco markers to export') - print(f'No ArUco markers to export') + # Close window using 'Esc' key + if key_pressed == 27: + break - # Close window using 'Esc' key - if key_pressed == 27: - break + # Display video + cv2.imshow(aruco_detector.name, video_image) - # Display video - cv2.imshow(aruco_detector.name, video_image) + # Close movie capture + video_capture.release() - # Close movie capture - video_capture.release() + # Stop image display + cv2.destroyAllWindows() - # Stop image display - cv2.destroyAllWindows() if __name__ == '__main__': - - main()
\ No newline at end of file + main() |