def test_image_crop(self):
        job = mpf.ImageJob(
            'Test Job', test_util.get_data_file_path('test_img.png'), {
                'SEARCH_REGION_ENABLE_DETECTION': 'true',
                'SEARCH_REGION_TOP_LEFT_X_DETECTION': '80',
                'SEARCH_REGION_TOP_LEFT_Y_DETECTION': '50',
                'SEARCH_REGION_BOTTOM_RIGHT_X_DETECTION': '240',
                'SEARCH_REGION_BOTTOM_RIGHT_Y_DETECTION': '150'
            }, {}, None)

        image_reader = mpf_util.ImageReader(job)
        image = image_reader.get_image()
        image_size = mpf_util.Size.from_frame(image)
        self.assertEqual((160, 100), image_size)

        # Check if all black
        self.assertTrue(test_util.is_all_black(image))

        self._assert_reverse_transform(image_reader, (0, 0, 160, 100),
                                       (80, 50, 160, 100))

        job = mpf.ImageJob(
            'Test Job', test_util.get_data_file_path('test_img.png'), {
                'SEARCH_REGION_ENABLE_DETECTION': 'true',
                'SEARCH_REGION_BOTTOM_RIGHT_Y_DETECTION': '40'
            }, {}, None)

        image_reader = mpf_util.ImageReader(job)
        image = image_reader.get_image()
        image_size = mpf_util.Size.from_frame(image)
        self.assertEqual((320, 40), image_size)

        # Check if all white
        self.assertTrue((image == (255, 255, 255)).all())
        self.assertTrue(test_util.is_all_white(image))
Example #2
0
    def test_image(self):
        job = mpf.ImageJob('test', str(TEST_DATA / 'test.png'), {}, {})
        component = OcvComponent()
        results = list(component.get_detections_from_image(job))
        self.assertEqual(2, len(results))

        self.assertEqual(300, results[0].x_left_upper)
        self.assertEqual(0, results[0].y_left_upper)
 def test_image_load(self):
     job = mpf.ImageJob('Test Job',
                        test_util.get_data_file_path('test_img.png'), {},
                        {}, None)
     image_reader = mpf_util.ImageReader(job)
     image = image_reader.get_image()
     image_size = mpf_util.Size.from_frame(image)
     self.assertEqual((320, 200), image_size)
Example #4
0
 def test_rotation_threshold_with_feed_forward_and_negative_coordinate(
         self):
     ff_loc = mpf.ImageLocation(-10, 20, 50, 30, 1, dict(ROTATION='0.5'))
     job = mpf.ImageJob(
         'Test job', test_util.get_data_file_path('test_img.png'),
         dict(FEED_FORWARD_TYPE='REGION', ROTATION_THRESHOLD='1'), dict(),
         ff_loc)
     img = mpf_util.ImageReader(job).get_image()
     self.assertEqual(img.shape, (30, 40, 3))
Example #5
0
def run_component_test():
    tc = EXPORT_MPF_COMPONENT()
    job = mpf.ImageJob('Test Job', '/home/mpf/sample-data/dog.jpg', {'job_prop1': 'job_val1'},
                       {'media_prop1': 'media_val1'}, None)

    logger.info('About to call get_detections_from_image')
    results = list(tc.get_detections_from_image(job))
    logger.info('get_detections_from_image found: %s detections', len(results))
    logger.info('get_detections_from_image results: %s', results)
    def test_rotation(self):
        job = mpf.ImageJob('Test Job',
                           test_util.get_data_file_path('test_img.png'),
                           {'ROTATION': '90'}, {}, None)
        image_reader = mpf_util.ImageReader(job)
        image = image_reader.get_image()

        self.assertEqual((200, 320), mpf_util.Size.from_frame(image))
        self.assertTrue(test_util.is_all_black(image[300:, :30]))
        self._assert_reverse_transform(image_reader, (0, 300, 30, 20),
                                       (300, 199, 30, 20, 90))
Example #7
0
    def test_rotation_threshold(self):
        test_img_path = test_util.get_data_file_path(
            'rotation/hello-world.png')
        original_img = cv2.imread(test_img_path)

        job = mpf.ImageJob('test', test_img_path,
                           dict(ROTATION='10', ROTATION_THRESHOLD='10.001'),
                           dict())
        img = mpf_util.ImageReader(job).get_image()
        self.assertTrue(np.array_equal(original_img, img))

        job.job_properties['ROTATION_THRESHOLD'] = '9.99'
        img = mpf_util.ImageReader(job).get_image()
        self.assertFalse(np.array_equal(original_img, img))
Example #8
0
    def test_rotation_fill_color(self):
        test_img_path = test_util.get_data_file_path(
            'rotation/hello-world.png')

        job = mpf.ImageJob('test', test_img_path, dict(ROTATION='45'), dict())
        img = mpf_util.ImageReader(job).get_image()
        self.assertTrue(np.array_equal(img[0, 0], (0, 0, 0)))

        job.job_properties['ROTATION_FILL_COLOR'] = 'BLACK'
        img = mpf_util.ImageReader(job).get_image()
        self.assertTrue(np.array_equal(img[0, 0], (0, 0, 0)))

        job.job_properties['ROTATION_FILL_COLOR'] = 'WHITE'
        img = mpf_util.ImageReader(job).get_image()
        self.assertTrue(np.array_equal(img[0, 0], (255, 255, 255)))
    def test_frame_flip(self):
        job = mpf.ImageJob('Test Job',
                           test_util.get_data_file_path('test_img.png'), {},
                           {}, None)
        original_image_reader = mpf_util.ImageReader(job)
        original_image = original_image_reader.get_image()
        original_white_corner = original_image[170:, :20]
        original_black_corner = original_image[170:, 300:]

        self.assertEqual((320, 200), mpf_util.Size.from_frame(original_image))
        self.assertTrue(test_util.is_all_white(original_white_corner))
        self.assertTrue(test_util.is_all_black(original_black_corner))

        self._assert_reverse_transform(original_image_reader, (0, 170, 20, 30),
                                       (0, 170, 20, 30))

        job = mpf.ImageJob('Test Job',
                           test_util.get_data_file_path('test_img.png'),
                           {'HORIZONTAL_FLIP': 'True'}, {}, None)
        flipped_image_reader = mpf_util.ImageReader(job)
        flipped_image = flipped_image_reader.get_image()

        self.assertEqual(original_image.shape, flipped_image.shape)
        flipped_black_corner = flipped_image[170:, :20]
        flipped_white_corner = flipped_image[170:, 300:]

        self.assertTrue(test_util.is_all_black(flipped_black_corner))
        self.assertTrue(test_util.is_all_white(flipped_white_corner))
        self.assertTrue(
            images_equal(original_black_corner, flipped_black_corner))
        self.assertTrue(
            images_equal(original_white_corner, flipped_white_corner))

        self._assert_reverse_transform(
            flipped_image_reader, (0, 170, 20, 30),
            (flipped_image.shape[1] - 1, 170, 20, 30))
Example #10
0
    def verify_correctly_rotated(self, file_name, feed_forward_detection):
        full_path = test_util.get_data_file_path('rotation/' + file_name)
        job = mpf.ImageJob('Test', full_path, dict(FEED_FORWARD_TYPE='REGION'),
                           {}, feed_forward_detection)
        image_reader = mpf_util.ImageReader(job)
        img = image_reader.get_image()

        height, width = img.shape[:2]

        self.assertEqual(feed_forward_detection.width, width)
        self.assertEqual(feed_forward_detection.height, height)

        self.assert_image_color(img, (255, 0, 0))

        detection = mpf.ImageLocation(0, 0, width, height)
        image_reader.reverse_transform(detection)
        self.assert_detections_same_location(detection, feed_forward_detection)
Example #11
0
    def test_rotation_threshold_with_feed_forward(self):
        test_img_path = test_util.get_data_file_path(
            'rotation/hello-world.png')
        original_img = cv2.imread(test_img_path)
        ff_img_loc = mpf.ImageLocation(0, 0, original_img.shape[1],
                                       original_img.shape[0], -1,
                                       dict(ROTATION='354.9'))

        job = mpf.ImageJob(
            'test', test_img_path,
            dict(ROTATION_THRESHOLD='5.12', FEED_FORWARD_TYPE='REGION'),
            dict(), ff_img_loc)
        img = mpf_util.ImageReader(job).get_image()
        self.assertTrue(np.array_equal(original_img, img))

        job.job_properties['ROTATION_THRESHOLD'] = '5.00'
        img = mpf_util.ImageReader(job).get_image()
        self.assertFalse(np.array_equal(original_img, img))
Example #12
0
    def test_rotation_full_frame(self):
        frame_rotation = 15
        job = mpf.ImageJob(
            'test', test_util.get_data_file_path('rotation/hello-world.png'),
            dict(ROTATION=str(frame_rotation)), {})

        image_reader = mpf_util.ImageReader(job)
        image = image_reader.get_image()

        il = mpf.ImageLocation(0, 0, image.shape[1], image.shape[0])
        image_reader.reverse_transform(il)

        self.assertEqual(-141, il.x_left_upper)
        self.assertEqual(38, il.y_left_upper)
        self.assertEqual(image.shape[1], il.width)
        self.assertEqual(image.shape[0], il.height)
        self.assertNotIn('HORIZONTAL_FLIP', il.detection_properties)
        self.assertAlmostEqual(frame_rotation,
                               float(il.detection_properties['ROTATION']))
Example #13
0
    def test_search_region_with_non_orthogonal_rotation(self):
        job = mpf.ImageJob(
            'Test',
            test_util.get_data_file_path('rotation/20deg-bounding-box.png'),
            dict(ROTATION='20',
                 SEARCH_REGION_ENABLE_DETECTION='true',
                 SEARCH_REGION_TOP_LEFT_X_DETECTION='199',
                 SEARCH_REGION_TOP_LEFT_Y_DETECTION='245',
                 SEARCH_REGION_BOTTOM_RIGHT_X_DETECTION='299',
                 SEARCH_REGION_BOTTOM_RIGHT_Y_DETECTION='285'), {}, None)
        image_reader = mpf_util.ImageReader(job)
        img = image_reader.get_image()
        self.assert_image_color(img, (255, 0, 0))

        il = mpf.ImageLocation(0, 0, img.shape[1], img.shape[0])
        image_reader.reverse_transform(il)
        self.assertEqual(117, il.x_left_upper)
        self.assertEqual(218, il.y_left_upper)
        self.assertEqual(100, il.width)
        self.assertEqual(40, il.height)
        actual_rotation = mpf_util.get_property(il.detection_properties,
                                                'ROTATION', 0.0)
        self.assertTrue(mpf_util.rotation_angles_equal(20, actual_rotation))
    def test_combined(self):
        job_properties = {
            'ROTATION': '270',
            'HORIZONTAL_FLIP': 'True',
            'SEARCH_REGION_ENABLE_DETECTION': 'true',
            'SEARCH_REGION_BOTTOM_RIGHT_X_DETECTION': '80',
            'SEARCH_REGION_BOTTOM_RIGHT_Y_DETECTION': '100'
        }

        job = mpf.ImageJob('Test Job',
                           test_util.get_data_file_path('test_img.png'),
                           job_properties, {}, None)

        component = ImageReaderMixinComponent(self)
        results = list(component.get_detections_from_image(job))
        self.assertEqual(4, len(results))
        self.assertEqual((319, 199, 30, 20),
                         mpf_util.Rect.from_image_location(results[0]))
        self.assertEqual((239, 149, 30, 20),
                         mpf_util.Rect.from_image_location(results[1]))
        self.assertEqual((319, 149, 30, 20),
                         mpf_util.Rect.from_image_location(results[2]))
        self.assertEqual((239, 199, 30, 20),
                         mpf_util.Rect.from_image_location(results[3]))
Example #15
0
    def test_search_region_with_orthogonal_rotation(self):
        absolute_props = dict(SEARCH_REGION_ENABLE_DETECTION='true',
                              SEARCH_REGION_TOP_LEFT_X_DETECTION='100',
                              SEARCH_REGION_BOTTOM_RIGHT_Y_DETECTION='50')

        percentage_props = dict(SEARCH_REGION_ENABLE_DETECTION='true',
                                SEARCH_REGION_TOP_LEFT_X_DETECTION='50%',
                                SEARCH_REGION_BOTTOM_RIGHT_Y_DETECTION='50%')

        rotate_90_props = dict(ROTATION='90',
                               SEARCH_REGION_ENABLE_DETECTION='true',
                               SEARCH_REGION_TOP_LEFT_X_DETECTION='50',
                               SEARCH_REGION_TOP_LEFT_Y_DETECTION='50%')

        rotate_180_props = dict(ROTATION='180',
                                SEARCH_REGION_ENABLE_DETECTION='true',
                                SEARCH_REGION_BOTTOM_RIGHT_X_DETECTION='50%',
                                SEARCH_REGION_TOP_LEFT_Y_DETECTION='50%')

        rotate_270_props = dict(ROTATION='270',
                                SEARCH_REGION_ENABLE_DETECTION='true',
                                SEARCH_REGION_BOTTOM_RIGHT_X_DETECTION='50%',
                                SEARCH_REGION_BOTTOM_RIGHT_Y_DETECTION='50%')

        flip_props = dict(HORIZONTAL_FLIP='true',
                          SEARCH_REGION_ENABLE_DETECTION='true',
                          SEARCH_REGION_BOTTOM_RIGHT_X_DETECTION='50%',
                          SEARCH_REGION_BOTTOM_RIGHT_Y_DETECTION='50%')

        rotate_90_and_flip_props = dict(
            ROTATION='90',
            HORIZONTAL_FLIP='true',
            SEARCH_REGION_ENABLE_DETECTION='true',
            SEARCH_REGION_BOTTOM_RIGHT_X_DETECTION='50%',
            SEARCH_REGION_TOP_LEFT_Y_DETECTION='50%')

        rotate_180_and_flip_props = dict(
            ROTATION='180',
            HORIZONTAL_FLIP='true',
            SEARCH_REGION_ENABLE_DETECTION='true',
            SEARCH_REGION_TOP_LEFT_X_DETECTION='50%',
            SEARCH_REGION_TOP_LEFT_Y_DETECTION='50%')

        rotate_270_and_flip_props = dict(
            ROTATION='270',
            HORIZONTAL_FLIP='true',
            SEARCH_REGION_ENABLE_DETECTION='true',
            SEARCH_REGION_TOP_LEFT_X_DETECTION='50%',
            SEARCH_REGION_BOTTOM_RIGHT_Y_DETECTION='50%')

        prop_sets = (absolute_props, percentage_props, rotate_90_props,
                     rotate_180_props, rotate_270_props, flip_props,
                     rotate_90_and_flip_props, rotate_180_and_flip_props,
                     rotate_270_and_flip_props)

        for props in prop_sets:
            job = mpf.ImageJob(
                'Test',
                test_util.get_data_file_path(
                    'rotation/search-region-test.png'), props, {}, None)
            img = mpf_util.ImageReader(job).get_image()
            num_green = count_matching_pixels(img, (0, 255, 0))
            self.assertEqual(5000, img.shape[0] * img.shape[1])
            self.assertEqual(5000, num_green)