Skip to content

Commit 59162a8

Browse files
authored
Fix negative coordinate handling in FeedForwardFrameCropper. (#54)
1 parent 83bc840 commit 59162a8

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

detection/component_util/mpf_component_util/frame_transformers/frame_cropper.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,61 +25,69 @@
2525
#############################################################################
2626

2727
import abc
28+
from typing import List, Mapping
2829

29-
from . frame_transformer import BaseDecoratedFrameTransformer
30+
import numpy as np
31+
32+
import mpf_component_api as mpf
33+
from . frame_transformer import BaseDecoratedFrameTransformer, IFrameTransformer
3034
from .. import utils
3135

3236

3337
class _BaseFrameCropper(BaseDecoratedFrameTransformer, abc.ABC):
3438

35-
def get_frame_size(self, frame_index):
39+
def get_frame_size(self, frame_index: int) -> utils.Size:
3640
return self._get_region_of_interest(frame_index).size
3741

3842

39-
def _do_frame_transform(self, frame, frame_index):
43+
def _do_frame_transform(self, frame: np.ndarray, frame_index: int) -> np.ndarray:
4044
region = self._get_region_of_interest(frame_index)
4145
return frame[region.y:region.br.y, region.x:region.br.x]
4246

4347

44-
def _do_reverse_transform(self, image_location, frame_index):
48+
def _do_reverse_transform(self, image_location: mpf.ImageLocation, frame_index: int) -> None:
4549
region = self._get_region_of_interest(frame_index)
4650
image_location.x_left_upper += region.x
4751
image_location.y_left_upper += region.y
4852

4953

54+
def _get_intersecting_region(
55+
self, region_of_interest: utils.Rect, frame_index: int) -> utils.Rect:
56+
frame_rect = utils.Rect.from_corner_and_size(
57+
utils.Point(0, 0), self._get_inner_frame_size(frame_index))
58+
return region_of_interest.intersection(frame_rect)
59+
60+
5061
@abc.abstractmethod
51-
def _get_region_of_interest(self, frame_index):
62+
def _get_region_of_interest(self, frame_index: int) -> utils.Rect:
5263
raise NotImplementedError()
5364

5465

5566

5667
class SearchRegionFrameCropper(_BaseFrameCropper):
57-
def __init__(self, search_region, inner_transform):
68+
def __init__(self, search_region: utils.Rect, inner_transform: IFrameTransformer):
5869
super().__init__(inner_transform)
59-
self.__search_region = self.__get_intersecting_region(search_region, 0)
70+
self.__search_region: utils.Rect = self._get_intersecting_region(search_region, 0)
6071

6172

62-
def _get_region_of_interest(self, frame_index):
73+
def _get_region_of_interest(self, frame_index: int) -> utils.Rect:
6374
return self.__search_region
6475

65-
def __get_intersecting_region(self, search_region, frame_index):
66-
frame_rect = utils.Rect.from_corner_and_size(utils.Point(0, 0), self._get_inner_frame_size(frame_index))
67-
return search_region.intersection(frame_rect)
68-
69-
7076

7177
class FeedForwardFrameCropper(_BaseFrameCropper):
72-
def __init__(self, frame_location_map, inner_transform):
78+
def __init__(self, frame_location_map: Mapping[int, mpf.ImageLocation],
79+
inner_transform: IFrameTransformer):
7380
super().__init__(inner_transform)
74-
self.__fed_forward_detections = [utils.Rect.from_image_location(loc)
75-
for loc in utils.dict_values_ordered_by_key(frame_location_map)]
81+
self.__fed_forward_detections: List[utils.Rect] = \
82+
[self._get_intersecting_region(utils.Rect.from_image_location(loc), idx)
83+
for idx, loc in utils.dict_items_ordered_by_key(frame_location_map)]
7684

7785

78-
def _get_region_of_interest(self, frame_index):
86+
def _get_region_of_interest(self, frame_index: int) -> utils.Rect:
7987
try:
8088
return self.__fed_forward_detections[frame_index]
8189
except IndexError:
8290
raise IndexError(
83-
'Attempted to get feed forward region of interest for frame: %s, '
84-
'but there are only %s entries in the feed forward track.'
85-
% (frame_index, len(self.__fed_forward_detections)))
91+
f'Attempted to get feed forward region of interest for frame: {frame_index}, '
92+
f'but there are only {len(self.__fed_forward_detections)} entries in the '
93+
f'feed forward track.')

detection/component_util/tests/test_affine_transformer.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,16 @@ def test_rotation_threshold_with_feed_forward(self):
471471
self.assertFalse(np.array_equal(original_img, img))
472472

473473

474+
def test_rotation_threshold_with_feed_forward_and_negative_coordinate(self):
475+
ff_loc = mpf.ImageLocation(-10, 20, 50, 30, 1, dict(ROTATION='0.5'))
476+
job = mpf.ImageJob(
477+
'Test job', test_util.get_data_file_path('test_img.png'),
478+
dict(FEED_FORWARD_TYPE='REGION', ROTATION_THRESHOLD='1'),
479+
dict(), ff_loc)
480+
img = mpf_util.ImageReader(job).get_image()
481+
self.assertEqual(img.shape, (30, 40, 3))
482+
483+
474484
def test_rotation_fill_color(self):
475485
test_img_path = test_util.get_data_file_path('rotation/hello-world.png')
476486

0 commit comments

Comments
 (0)