def test_simplify_fiducial_arrays(self): # No markers corners, ids = self.TEST_NO_MARKER.corners, self.TEST_NO_MARKER.IDs actual_corners, actual_ids = ImageProcessor._simplify_fiducial_arrays( corners, ids) exp_corners, exp_ids = [], [] np.testing.assert_allclose(actual_corners, exp_corners) np.testing.assert_array_equal(actual_ids, exp_ids) self.assertIsInstance(actual_corners, np.ndarray) self.assertIsInstance(actual_ids, np.ndarray) # One marker corners, ids = self.TEST_SINGLE_MARKER.corners, self.TEST_SINGLE_MARKER.IDs actual_corners, actual_ids = ImageProcessor._simplify_fiducial_arrays( corners, ids) exp_corners, exp_ids = np.array(corners).squeeze( axis=1), np.array(ids).squeeze() np.testing.assert_allclose(actual_corners, exp_corners) np.testing.assert_array_equal(actual_ids, exp_ids) self.assertEqual(np.shape(actual_corners), (1, 4, 2)) self.assertEqual(np.shape(actual_ids), (1, )) self.assertIsInstance(actual_corners, np.ndarray) self.assertIsInstance(actual_ids, np.ndarray) # Multiple markers corners, ids = self.TEST_MULT_AEROCUBES.corners, self.TEST_MULT_AEROCUBES.IDs actual_corners, actual_ids = ImageProcessor._simplify_fiducial_arrays( corners, ids) exp_corners, exp_ids = np.array(corners).squeeze( axis=1), np.array(ids).squeeze() np.testing.assert_allclose(actual_corners, exp_corners) np.testing.assert_array_equal(actual_ids, exp_ids) self.assertEqual(np.shape(actual_corners), (2, 4, 2)) self.assertEqual(np.shape(actual_ids), (2, )) self.assertIsInstance(actual_corners, np.ndarray) self.assertIsInstance(actual_ids, np.ndarray)
def test_find_aerocube_markers_none(self): # get hard-coded results aerocube_markers = [] # get ImP results imp = ImageProcessor(self.TEST_NO_MARKER.img_path) test_markers = imp._find_aerocube_markers() self.assertTrue(np.array_equal(aerocube_markers, test_markers))
def test_find_fiducial_marker_none(self): imp = ImageProcessor(self.TEST_NO_MARKER.img_path) corners, ids = imp._simplify_fiducial_arrays( self.TEST_NO_MARKER.corners, self.TEST_NO_MARKER.IDs) test_corners, test_ids = imp._find_fiducial_markers() np.testing.assert_allclose(corners, test_corners) np.testing.assert_array_equal(ids, test_ids)
def scan_image(self, img_event): """ Handler for ImageEventSignal.IDENTIFY_AEROCUBES. :param img_event: :return: """ # Initialize variables file_path = img_event.payload.strings(ImageEvent.FILE_PATH) try: logger.debug( self.__class__.__name__, func_name='scan_image', msg='Instantiating ImP at {}'.format(file_path), id=img_event.payload.strings(job_id_bundle_key)) imp = ImageProcessor(file_path) logger.debug( self.__class__.__name__, func_name='scan_image', msg='Finding fiducial markers', id=img_event.payload.strings(job_id_bundle_key)) aerocubes_as_json, markers_as_json = imp.identify_markers_for_storage() logger.success( self.__class__.__name__, func_name='scan_image', msg='Done with Scan!', id=img_event.payload.strings(job_id_bundle_key)) # Set result signal to OK result_signal = ResultEventSignal.OK # Prepare bundle from original result_bundle = img_event.payload result_bundle.insert_string(ImageEvent.SCAN_ID, str(img_event.created_at).split('.')[0]) result_bundle.insert_raw(AeroCube.STR_KEY_CUBE_IDS, aerocubes_as_json[AeroCube.STR_KEY_CUBE_IDS]) result_bundle.insert_raw(AeroCube.STR_KEY_QUATERNIONS, aerocubes_as_json[AeroCube.STR_KEY_QUATERNIONS]) result_bundle.insert_raw(AeroCube.STR_KEY_TVECS, aerocubes_as_json[AeroCube.STR_KEY_TVECS]) result_bundle.insert_raw(AeroCube.STR_KEY_DISTANCES, aerocubes_as_json[AeroCube.STR_KEY_DISTANCES]) result_bundle.insert_raw(AeroCube.STR_KEY_MARKERS_DETECTED, aerocubes_as_json[AeroCube.STR_KEY_MARKERS_DETECTED]) result_bundle.insert_raw(ImageEvent.SCAN_MARKERS, markers_as_json) store_image("ImP/output_files/" + os.path.split(file_path)[-1], imp.draw_aerocubes()) logger.success( self.__class__.__name__, func_name='scan_image', msg='Controller.scan_image: Done with setting bundle : {}'.format(str(result_bundle)), id=img_event.payload.strings(job_id_bundle_key)) except Exception as ex: logger.err( self.__class__.__name__, func_name='scan_image', msg='ImP Failed', id=img_event.payload.strings(job_id_bundle_key)) template = "An exception of type {0} occured. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) logger.err( self.__class__.__name__, func_name='scan_image', msg=message, id=img_event.payload.strings(job_id_bundle_key)) result_signal = ResultEventSignal.ERROR result_bundle = img_event.payload return result_signal, result_bundle
def test_find_distance(self): imp = ImageProcessor(self.TEST_JETSON_SINGLE_MARKER.img_path) corners, ids = imp._find_fiducial_markers() dist = imp._find_distance(corners) print(dist) self.assertGreater(dist[0], 0.8) self.assertLess(dist[0], 1.0)
def test_identify_aerocubes(self): aerocube_ID = 0 aerocube_face = AeroCubeFace.ZENITH corners, _ = ImageProcessor._simplify_fiducial_arrays( self.TEST_SINGLE_MARKER.corners, [[aerocube_ID]]) marker_list = [AeroCubeMarker(aerocube_ID, aerocube_face, corners[0])] aerocube_list = [AeroCube(marker_list)] imp = ImageProcessor(self.TEST_SINGLE_MARKER.img_path) self.assertEqual(imp._identify_aerocubes(), aerocube_list)
def test_find_fiducial_marker_multiple(self): # get results from ImP imp = ImageProcessor(self.TEST_MULT_AEROCUBES.img_path) corners, ids = imp._simplify_fiducial_arrays( self.TEST_MULT_AEROCUBES.corners, self.TEST_MULT_AEROCUBES.IDs) test_corners, test_ids = imp._find_fiducial_markers() # assert hard-coded results equal ImP results np.testing.assert_allclose(corners, test_corners) np.testing.assert_array_equal(ids, test_ids)
def test_rodrigues_to_quaternion(self): imp = ImageProcessor(self.TEST_JETSON_SINGLE_MARKER.img_path) rvecs, _ = imp._find_pose(imp._find_fiducial_markers()[0]) # test value test_quat = imp.rodrigues_to_quaternion(rvecs[0]) # true value true_quat = pyquaternion.Quaternion(w=-0.060, x=0.746, y=-0.648, z=-0.140) # assert that the str representations are equal (components identical up to 3 decimal places) self.assertEqual(str(test_quat), str(true_quat))
def test_identify_markers_for_storage(self): # TODO: needs to be rewritten for new method self.fail() # get hard-coded results corners, _ = ImageProcessor._simplify_fiducial_arrays( self.TEST_SINGLE_MARKER.corners, [[0]]) marker_list = [AeroCubeMarker(0, AeroCubeFace.ZENITH, corners[0])] aerocube_list = [AeroCube(marker_list)] # get ImP results imp = ImageProcessor(self.TEST_SINGLE_MARKER.img_path) scan_results = imp.identify_markers_for_storage() # assert equality np.testing.assert_array_equal(aerocube_list, scan_results)
def test_find_fiducial_marker(self): # hard code results of operation corners, ids = ImageProcessor._simplify_fiducial_arrays( self.TEST_SINGLE_MARKER.corners, self.TEST_SINGLE_MARKER.IDs) # get results of function imp = ImageProcessor(self.TEST_SINGLE_MARKER.img_path) test_corners, test_ids = imp._find_fiducial_markers() # assert hard-coded results equal results of function self.assertTrue(np.array_equal(corners, test_corners)) self.assertTrue(np.array_equal(ids, test_ids)) self.assertEqual(type(test_ids).__module__, np.__name__) # save output image for visual confirmation output_img = imp.draw_fiducial_markers(test_corners, test_ids) cv2.imwrite(self.test_output_path, output_img)
def test_detect_markers_parallel_on_capstone_photos(self): img_paths = [ os.path.join(self._CAPSTONE_PHOTO_DIR, f) for f in os.listdir(self._CAPSTONE_PHOTO_DIR) if os.path.isfile(os.path.join(self._CAPSTONE_PHOTO_DIR, f)) ] for img_path in img_paths: imp = ImageProcessor(img_path) actual_corners, actual_ids = MarkerDetectPar.detect_markers_parallel( imp._img_mat) expected_corners, expected_ids = imp._find_fiducial_markers( parallel=False) np.testing.assert_allclose(actual_corners, expected_corners) np.testing.assert_array_equal(actual_ids, expected_ids) print("PASSED: {}".format(img_path))
def test_simplify_fiducial_arrays_raises_properly(self): markers = self.TEST_MULT_AEROCUBES corners, ids = ImageProcessor._simplify_fiducial_arrays( markers.corners, markers.IDs) self.assertRaises(AssertionError, ImageProcessor._simplify_fiducial_arrays, corners, ids)
def test_rodrigues_to_quaternion_has_identical_rotation(self): """ Confirms the following case: http://stackoverflow.com/questions/12933284/rodrigues-into-eulerangles-and-vice-versa """ imp = ImageProcessor(self.TEST_JETSON_SINGLE_MARKER.img_path) rvecs, _ = imp._find_pose(imp._find_fiducial_markers()[0]) test_quat = imp.rodrigues_to_quaternion(rvecs[0]) # get rotation matrices rot_mat_orig = cv2.Rodrigues(rvecs[0])[0] rot_mat_quat = test_quat.rotation_matrix rot_mat = cv2.multiply(rot_mat_orig, cv2.transpose(rot_mat_quat)) # test not working for some reason; just test equality of rotation matrices # self.assertTrue(np.allclose(rot_mat, # np.identity(3), # rtol=1e-02)) self.assertTrue(np.allclose(rot_mat_orig, rot_mat_quat, rtol=1e-02))
def test_identify_aerocubes_multiple(self): # get hard-coded results corners, _ = ImageProcessor._simplify_fiducial_arrays( self.TEST_MULT_AEROCUBES.corners, [[2], [0]]) aerocube_2_markers = [ AeroCubeMarker(2, AeroCubeFace.ZENITH, corners[0]) ] aerocube_0_markers = [AeroCubeMarker(0, AeroCubeFace.BACK, corners[1])] aerocubes = [ AeroCube(aerocube_2_markers), AeroCube(aerocube_0_markers) ] # get ImP results imp = ImageProcessor(self.TEST_MULT_AEROCUBES.img_path) test_aerocubes = imp._identify_aerocubes() # assert equality self.assertTrue(np.array_equal(aerocubes, test_aerocubes))
def test_quaternion_to_rodrigues(self): rodr = np.array([[2.43782719, -2.1174341, -0.45808756]]) quat = pyquaternion.Quaternion(w=-0.060, x=0.746, y=-0.648, z=-0.140) # assert that rotation matrices of Rodrigues representation and Quaternion translated into Rodrigues # is close enough (1e-02) self.assertTrue( np.allclose(cv2.Rodrigues(rodr)[0], cv2.Rodrigues( ImageProcessor.quaternion_to_rodrigues(quat))[0], rtol=1e-02))
def test_prepare_fiducial_arrays_for_aruco(self): # No markers markers = self.TEST_NO_MARKER corners, ids = ImageProcessor._simplify_fiducial_arrays( markers.corners, markers.IDs) actual_corners, actual_ids = ImageProcessor._prepare_fiducial_arrays_for_aruco( corners, ids) exp_corners, exp_ids = [], None np.testing.assert_allclose(actual_corners, exp_corners) np.testing.assert_array_equal(actual_ids, exp_ids) self.assertNotIsInstance(actual_ids, np.ndarray) # One marker markers = self.TEST_SINGLE_MARKER corners, ids = ImageProcessor._simplify_fiducial_arrays( markers.corners, markers.IDs) actual_corners, actual_ids = ImageProcessor._prepare_fiducial_arrays_for_aruco( corners, ids) exp_corners, exp_ids = [corners], [ids] np.testing.assert_allclose(actual_corners, exp_corners) np.testing.assert_array_equal(actual_ids, exp_ids) self.assertEqual(np.shape(actual_corners), (1, 1, 4, 2)) self.assertEqual(np.shape(actual_ids), (1, 1)) # Multiple markers markers = self.TEST_MULT_AEROCUBES corners, ids = ImageProcessor._simplify_fiducial_arrays( markers.corners, markers.IDs) actual_corners, actual_ids = ImageProcessor._prepare_fiducial_arrays_for_aruco( corners, ids) exp_corners, exp_ids = [[corners[0]], [corners[1]]], [[ids[0]], [ids[1]]] np.testing.assert_allclose(actual_corners, exp_corners) np.testing.assert_array_equal(actual_ids, exp_ids) self.assertEqual(np.shape(actual_corners), (2, 1, 4, 2)) self.assertEqual(np.shape(actual_ids), (2, 1))
def test_time_serial_vs_par(self): img_path = os.path.join(self._CAPSTONE_PHOTO_PATH, 'AC_0_1_FRONT_TOP.JPG') imp = ImageProcessor(img_path) pr = cProfile.Profile() pr.enable() actual_corners, actual_ids = imp._find_fiducial_markers() pr.disable() s = io.StringIO() ps = pstats.Stats(pr, stream=s).sort_stats('cumulative') ps.print_stats() print(s.getvalue()) pr = cProfile.Profile() pr.enable() test_corners, test_ids = imp._find_fiducial_markers(gpu=True) pr.disable() s = io.StringIO() ps = pstats.Stats(pr, stream=s).sort_stats('cumulative') ps.print_stats() print(s.getvalue()) np.testing.assert_allclose(actual_corners, test_corners) np.testing.assert_array_equal(actual_ids, test_ids)
Helper script for testing purposes. This file can be called from the python3 shell as such: exec(open("ImP/startImP.py").read()) """ from ImP.imageProcessing.imageProcessingInterface import ImageProcessor from ImP.imageProcessing.cameraCalibration import CameraCalibration from ImP.imageProcessing.settings import ImageProcessingSettings import cv2 from cv2 import aruco import numpy as np # img_path = "ImP/imageProcessing/test_files/jetson_test1.jpg" # img_path = "ImP/imageProcessing/test_files/capstone_class_photoshoot/AC_0_125_DOCT.JPG" img_path = "ImP/imageProcessing/test_files/capstone_class_photoshoot/SPACE_0.JPG" imp = ImageProcessor(img_path) corners, ids = imp._find_fiducial_markers() rvecs, tvecs = imp._find_pose(corners) quaternions = [imp.rodrigues_to_quaternion(r) for r in rvecs] [print(q == q.normalised) for q in quaternions] print(corners) print(quaternions) print(tvecs) img = aruco.drawAxis(np.copy(imp._img_mat), imp._cal.CAMERA_MATRIX, imp._cal.DIST_COEFFS, rvecs[-1], tvecs[-1], ImageProcessingSettings.get_marker_length()) cv2.imwrite("ImP/output_files/OUTPUT_1.jpg", img) img = imp.draw_aerocube_markers() cv2.imwrite("ImP/output_files/OUTPUT.jpg", img) img = imp.draw_aerocubes() cv2.imwrite("ImP/output_files/OUTPUT_CUBE.jpg", img)
from ImP.imageProcessing.imageProcessingInterface import ImageProcessor # this file can be called from the python3 shell as such: # exec(open("ImP/startImP.py").read()) img_path = "ImP/imageProcessing/test_files/2_ZENITH_0_BACK.jpg" imp = ImageProcessor(img_path) rvecs, tvecs = imp._find_pose()
def test_positive_load_image(self): imp = ImageProcessor(self.TEST_SINGLE_MARKER.img_path) self.assertIsNotNone( ImageProcessor._load_image(self.TEST_SINGLE_MARKER.img_path))
def test_init(self): imp = ImageProcessor(self.TEST_SINGLE_MARKER.img_path) self.assertIsNotNone(imp._img_mat)
def test_draw_axis(self): imp = ImageProcessor(self.TEST_JETSON_SINGLE_MARKER.img_path) rvecs, tvecs = imp._find_pose(imp._find_fiducial_markers()[0]) img = imp.draw_axis(imp.rodrigues_to_quaternion(rvecs[0]), tvecs[0]) self.assertIsNotNone(img)
def test_draw_fiducial_markers(self): imp = ImageProcessor(self.TEST_SINGLE_MARKER.img_path) corners, IDs = imp._find_fiducial_markers() img = imp.draw_fiducial_markers(corners, IDs) self.assertEqual(img.shape, imp._img_mat.shape)
def test_find_aerocube_marker(self): imp = ImageProcessor(self.AC_0_FACES_125_PATH) aerocube_markers = imp._find_aerocube_markers() [self.assertIsInstance(m, AeroCubeMarker) for m in aerocube_markers]
def test_identify_aerocubes_none(self): imp = ImageProcessor(self.TEST_NO_MARKER.img_path) self.assertEqual([], imp._identify_aerocubes())