def test_copy_creates_copy(self): roi = ROI.set_from_abs_points((45, 48), (173, 12), self.tarray().shape) roi2 = roi.copy() self.assertEqual(roi.x_rel, roi2.x_rel) self.assertEqual(roi.y_rel, roi2.y_rel) self.assertEqual(roi.w_rel, roi2.w_rel) self.assertEqual(roi.h_rel, roi2.h_rel)
def test_get_left_of_center_returns_portion_of_frame_in_roi_left_of_the_center_line(self): expected = np.ones((25, 65, 3), dtype='uint8') frame = np.ones((100, 150, 3), dtype='uint8') roi = ROI.set_from_abs_points((10, 10), (80, 35), frame.shape) clipped_frame = roi.get_left_of_center(frame) self.assertEquals(expected.shape, clipped_frame.shape) self.assertTrue((expected == clipped_frame).all())
def test_from_points_gets_roi_from_two_cordanates(self): test_array = self.tarray() p1 = (10, 20) p2 = (200, 10) roi = ROI.set_from_abs_points(p1, p2, test_array.shape) self.assertEquals(10, roi.get(test_array).shape[0]) self.assertEquals(190, roi.get(test_array).shape[1]) self.assertTrue((roi.get(test_array)[0][0] == [10, 10, 0]).all())
def test_set_region_of_interest_from_abs_points_should_create_expected_roi(self, mock_camera): cam = mock_camera.return_value cam.shape = [300, 100] api = ScannerAPI() expected_roi = ROI.set_from_abs_points((0, 0), (151, 90), [100, 300, 3]) api.set_region_of_interest_from_abs_points((0, 0), (151, 90), [300, 100]) self.assertROIEquals(expected_roi, api.roi)
def test_set_region_of_interest_rel_points_should_create_expected_roi( self, mock_camera): cam = mock_camera.return_value cam.shape = [300, 100] api = ScannerAPI() expected_roi = ROI(0.0, 0.0, 0.5, 0.9) api.set_region_of_interest_from_rel_points(0, 0, 0.5, 0.9) self.assertROIEquals(expected_roi, api.roi)
def __init__(self): self.camera = Camera() self.camera.start() self._default_roi = ROI(0.0, 0.0, 1.0, 1.0) self._default_encoder = Encoder((0.2, 0.2), 382, 100, 20, 200) self._default_laser_detector = LaserDetector2(225, 'red') self.encoder = self._default_encoder self.roi = self._default_roi self.laser_detector = self._default_laser_detector self.video_processor = VideoProcessor(self.camera, self.encoder, self.roi, self.laser_detector)
def test_set_region_of_interest_from_abs_points_should_create_expected_roi( self, mock_camera): cam = mock_camera.return_value cam.shape = [300, 100] api = ScannerAPI() expected_roi = ROI.set_from_abs_points((0, 0), (151, 90), [100, 300, 3]) api.set_region_of_interest_from_abs_points((0, 0), (151, 90), [300, 100]) self.assertROIEquals(expected_roi, api.roi)
def create_video_processor(self, roi=None): self.camera = FakeCamera() self.encoder = Mock() self.encoder.position = 0 self.encoder.sections = 200 self.encoder.should_capture_frame_for_section.return_value = (True, 0) self.mock_laser_detector = Mock() self.detected_image = np.ones((self.camera.image.shape[0], self.camera.image.shape[1]), dtype='uint8') * 255 self.mock_laser_detector.detect.return_value = self.detected_image x_center = self.camera.image.shape[1] // 2 if roi: self.roi = roi else: self.roi = ROI.set_from_abs_points((10, 50), (x_center + 1, 70), self.camera.image.shape) return VideoProcessor(self.camera, self.encoder, self.roi, self.mock_laser_detector)
def create_video_processor(self, roi=None): self.camera = FakeCamera() self.encoder = Mock() self.encoder.position = 0 self.encoder.sections = 200 self.encoder.should_capture_frame_for_section.return_value = (True, 0) self.mock_laser_detector = Mock() self.detected_image = np.ones( (self.camera.image.shape[0], self.camera.image.shape[1]), dtype='uint8') * 255 self.mock_laser_detector.detect.return_value = self.detected_image x_center = self.camera.image.shape[1] // 2 if roi: self.roi = roi else: self.roi = ROI.set_from_abs_points((10, 50), (x_center + 1, 70), self.camera.image.shape) return VideoProcessor(self.camera, self.encoder, self.roi, self.mock_laser_detector)
def setUp(self): self.img2point = Mock() self.img2point.get_points.return_value = np.array([1.0, 1.0, 1.0]) self.roi = ROI(0, 0, 1, 1) self.laser_theta = .77
def test_should_raise_exception_if_center_left_of_roi(self): frame = np.ones((100, 100, 3), dtype='uint8') with self.assertRaises(Exception): ROI.set_from_abs_points((70, 10), (80, 20), frame.shape)
def test_replace_returns_only_new_part_if_no_data(self): test_array = self.tarray() newpart = np.ones(test_array.shape, dtype='uint8') roi = ROI.set_from_abs_points((0, 0), (256, 256), self.tarray().shape) replaced = roi.replace(test_array, newpart) self.assertTrue((replaced == newpart).all())
def set_region_of_interest_from_abs_points(self, point1, point2, frame_shape_xy): self.roi = ROI.set_from_abs_points(point1, point2, [frame_shape_xy[1], frame_shape_xy[0], 3]) self.video_processor.roi = self.roi
def set_region_of_interest_from_abs_points(self, point1, point2, frame_shape_xy): self.roi = ROI.set_from_abs_points( point1, point2, [frame_shape_xy[1], frame_shape_xy[0], 3]) self.video_processor.roi = self.roi
def test_get_returns_roi_of_expected_details(self): test_array = self.tarray() roi = ROI.set_from_abs_points((127, 0), (129, 2), self.tarray().shape) result = roi.get(test_array) self.assertTrue((result[0][0] == [0, 127, 0]).all()) self.assertTrue((result[1][1] == [1, 128, 0]).all())
def test_get_returns_roi_of_expected_size(self): test_array = self.tarray() roi = ROI(0, 0, 1, 1) self.assertTrue((roi.get(test_array) == test_array).all())
def test_get_points_should_return_a_list_of_points(self): roi = ROI(0, 0, 1, 1) self.assertEquals([0, 0, 1, 1], roi.get_points())
def set_region_of_interest_from_rel_points(self, x_rel, y_rel, w_rel, h_rel): self.roi = ROI(x_rel, y_rel, w_rel, h_rel) self.video_processor.roi = self.roi
def test_overlay_dims_surrounding_area(self): test_array = self.tarray() roi = ROI.set_from_abs_points((0, 0), (128, 128), self.tarray().shape) overlay = roi.overlay(test_array) self.assertTrue((overlay[254][254] == [127, 127, 0]).all(), "{}".format(overlay[255][255]))