class TestImageGeneratorZeroAngle(unittest.TestCase):
    def setUp(self):
        self.num_timestamps = 2
        self.fov = math.pi / 2
        self.object_universe = ObjectUniverse(
            num_timestamps=self.num_timestamps)

        self.camera = Camera()

        self.camera.set_fov_rad(math.pi / 2).\
                    set_actual_position((100,400)).\
                    set_gps_position((100,410)).\
                    set_gps_max_error(12).\
                    set_num_timestamps(self.num_timestamps)

        for timestamp in range(self.num_timestamps):
            self.camera.set_state_of_pan_motor_angle_at_timestamp(0, timestamp)

        self.viewable_objects = [
            StationaryObject((300, 599)),
            StationaryObject((300, 201)),
        ]
        self.object_universe.add_viewable_objects(self.viewable_objects)
        self.object_universe.add_camera(self.camera)

    def test_fov(self):
        self.camera.image_generator.get_x_for_all_inview_objects_for_all_camera_time(
        )
        self.assertEqual(self.camera.get_image_generator().fov, math.pi / 2)

    def test_d(self):
        self.camera.image_generator.get_x_for_all_inview_objects_for_all_camera_time(
        )
        self.assertAlmostEqual(self.camera.get_image_generator().d, 320)

    def test_get_x_remain_in_image_width(self):
        r = self.camera.get_image_generator(
        ).get_x_for_all_inview_objects_for_all_camera_time()

        for obj in self.viewable_objects:
            x = r[0][obj]
            self.assertLess(
                abs(x),
                self.camera.get_image_generator().get_image_width() / 2)

    def test_image_width_default(self):
        self.assertEqual(self.camera.get_image_generator().get_image_width(),
                         640)
Beispiel #2
0
 def test_stationary_object_updates_its_num_timestamps_to_match_object_universe_it_gets_added_to(self):
     ou = ObjectUniverse(num_timestamps=200)
     ou.add_viewable_objects(self.so)
     self.assertEqual(self.so.get_position_history_len(), 200)
     for timestamp in range(0, 200):
         self.assertEqual(self.so.get_position_at_timestamp(timestamp), self.pos)
class TestImageGenerator(unittest.TestCase):
    def setUp(self):
        self.num_timestamps = 10
        self.fov = math.pi / 2
        self.image_width = 1000
        self.object_universe = ObjectUniverse(
            num_timestamps=self.num_timestamps)
        self.camera = Camera()

        self.camera.set_actual_position((1,1)).\
                    set_fov_rad(self.fov).\
                    set_num_timestamps(self.num_timestamps).\
                    set_range(50000)

        for timestamp in range(self.camera.get_num_timestamps()):
            self.camera.set_state_of_pan_motor_angle_at_timestamp_rad(
                math.pi / 4, timestamp)

        self.cv = ComputerVision()
        self.image_generator = ImageGenerator(image_width=self.image_width)
        self.camera.set_computer_vision(self.cv).\
                    set_image_generator(self.image_generator)

        self.viewable_objects = [
            StationaryObject((2, 1 + math.tan(math.pi / 8))),
            StationaryObject((1 + math.tan(math.pi / 8), 2)),
            StationaryObject((10000, 1.1)),
            StationaryObject((1.1, 10000)),
        ]
        self.object_universe.add_viewable_objects(self.viewable_objects)
        self.object_universe.add_camera(self.camera)

        self.d = self.image_width / (2 * math.tan(self.fov / 2))
        self.theta = math.pi / 8

    def test_d_calculation_for_image_generator(self):
        self.camera.get_image_generator(
        ).get_x_for_all_inview_objects_for_all_camera_time()
        self.assertEqual(self.d, self.image_generator.d)

    def test_theta_calculation_for_image_generator(self):
        self.camera.get_image_generator(
        ).get_x_for_all_inview_objects_for_all_camera_time()
        self.assertAlmostEqual(
            -self.theta,
            self.image_generator._get_theta_rad(self.viewable_objects[0], 0))
        self.assertAlmostEqual(
            self.theta,
            self.image_generator._get_theta_rad(self.viewable_objects[1], 0))

    def test_get_x_for_all_inview_objects_for_all_camera_time(self):

        r = self.image_generator.get_x_for_all_inview_objects_for_all_camera_time(
        )

        for timestamp in range(self.camera.get_num_timestamps()):
            self.assertAlmostEqual(r[timestamp][self.viewable_objects[0]],
                                   -self.d * math.tan(math.pi / 8))
            self.assertAlmostEqual(r[timestamp][self.viewable_objects[1]],
                                   self.d * math.tan(math.pi / 8))

    def test_get_x_for_distant_objects_at_edge_of_view_remain_in_image_width(
            self):

        r = self.image_generator.get_x_for_all_inview_objects_for_all_camera_time(
        )

        for obj in self.viewable_objects:
            x = r[0][obj]
            self.assertLess(abs(x), self.image_width / 2)
class TestComputerVision(unittest.TestCase):

    def setUp(self):
        self.num_timestamps = 6
        self.object_universe = ObjectUniverse(num_timestamps=self.num_timestamps)
        self.camera = Camera(fov_deg=90, actual_position=(0,0))
        self.cv = ComputerVision()
        self.camera.set_computer_vision(self.cv)
        self.object_universe.add_camera(self.camera)

        for timestamp, _ in enumerate(self.camera.get_state_history()):
            self.camera.set_pan_angle_deg(0, timestamp)

        self.out_pos = (1,2)
        self.in_pos = (1,0)

        self.out_obj = ViewableObject()
        self.in_obj = ViewableObject()
        self.in_out_in_obj = ViewableObject()
        self.out_in_out_obj = ViewableObject()

        self.viewable_objects = [
            self.out_obj,
            self.in_obj,
            self.in_out_in_obj,
            self.out_in_out_obj,
        ]

        self.object_universe.add_viewable_objects(self.viewable_objects)

        for timestamp, _ in enumerate(self.out_obj.position_history):
            self.in_obj.set_position_at_timestamp(self.in_pos, timestamp)
            self.out_obj.set_position_at_timestamp(self.out_pos, timestamp)

        for timestamp in [0,1]:
            self.in_out_in_obj.set_position_at_timestamp(self.in_pos, timestamp)
            self.out_in_out_obj.set_position_at_timestamp(self.out_pos, timestamp)

        for timestamp in [2,3]:
            self.in_out_in_obj.set_position_at_timestamp(self.out_pos, timestamp)
            self.out_in_out_obj.set_position_at_timestamp(self.in_pos, timestamp)

        for timestamp in [4,5]:
            self.in_out_in_obj.set_position_at_timestamp(self.in_pos, timestamp)
            self.out_in_out_obj.set_position_at_timestamp(self.out_pos, timestamp)

        self.cv.set_cv_ids_for_all_camera_time()

    def test_camera_has_correct_number_of_timestamps(self):
        self.assertEqual(self.camera.get_num_timestamps(), self.num_timestamps)
        self.assertEqual(self.camera.get_state_history_len(), self.num_timestamps)

    def test_viewable_objects_have_correct_number_of_timestamps(self):
        for vo in self.viewable_objects:
            self.assertEqual(vo.get_position_history_len(), self.num_timestamps)
            self.assertEqual(vo.get_num_timestamps(), self.num_timestamps)

    def test_objects_that_never_are_in_view_never_get_id(self):
        for timestamp, _ in enumerate(self.out_obj.get_position_history()):
            self.assertIsNone(self.cv.get_cv_id_for_obj_at_timestamp(self.out_obj, timestamp))

    def test_it_ids_objects_that_come_into_view(self):
        for timestamp in [0,1]:
            self.assertIsNone(self.cv.get_cv_id_for_obj_at_timestamp(self.out_in_out_obj, timestamp))
            self.assertIsNotNone(self.cv.get_cv_id_for_obj_at_timestamp(self.in_out_in_obj, timestamp))

        self.assertEqual(self.cv.get_cv_id_for_obj_at_timestamp(self.in_out_in_obj, 0),
                         self.cv.get_cv_id_for_obj_at_timestamp(self.in_out_in_obj, 1))

    def that_it_prefixes_the_ids_with_cv(self):
        self.assertEqual(self.cv.get_cv_id_for_obj_at_timestamp(self.in_obj,0)[0:2], 'cv')

    def test_it_retains_ids_of_objects_that_remain_in_view(self):
        self.assertEqual(self.cv.get_cv_id_for_obj_at_timestamp(self.out_in_out_obj, 2),
                         self.cv.get_cv_id_for_obj_at_timestamp(self.out_in_out_obj, 3))


    def test_it_removes_ids_of_objects_that_leave_view(self):
        self.assertIsNone(self.cv.get_cv_id_for_obj_at_timestamp(self.out_in_out_obj, 4))
        self.assertIsNone(self.cv.get_cv_id_for_obj_at_timestamp(self.out_in_out_obj, 5))

    def test_it_re_ids_objects_that_come_back_in_view(self):
        in_first = self.cv.get_cv_id_for_obj_at_timestamp(self.in_out_in_obj, 1)
        in_again = self.cv.get_cv_id_for_obj_at_timestamp(self.in_out_in_obj, 4)
        self.assertIsNotNone(in_first)
        self.assertIsNotNone(in_again)
        self.assertNotEqual(in_first, in_again)