Skip to content

Commit

Permalink
More consistent naming: always coord_transormations
Browse files Browse the repository at this point in the history
  • Loading branch information
Agustín Castro committed Feb 26, 2024
1 parent d8a9993 commit e25c7c3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
2 changes: 1 addition & 1 deletion demos/camera_motion/src/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def run():

if args.draw_paths:
frame = path_drawer.draw(
frame, tracked_objects, coord_transform=coord_transformations
frame, tracked_objects, coord_transformations=coord_transformations
)

if use_fixed_camera:
Expand Down
42 changes: 25 additions & 17 deletions norfair/drawing/fixed_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ class FixedCamera:
>>> video.write(bigger_frame)
"""

def __init__(self, scale: float = 2, attenuation: float = 0.05):
def __init__(self, scale: float = None, attenuation: float = 0.05):
self.scale = scale
self._background = None
self._attenuation_factor = 1 - attenuation

def adjust_frame(
self,
frame: np.ndarray,
coord_transformation: Union[
coord_transformations: Union[
HomographyTransformation, TranslationTransformation
],
) -> np.ndarray:
Expand All @@ -77,7 +77,7 @@ def adjust_frame(
----------
frame : np.ndarray
The OpenCV frame.
coord_transformation : TranslationTransformation
coord_transformations : Union[TranslationTransformation, HomographyTransformation]
The coordinate transformation as returned by the [`MotionEstimator`][norfair.camera_motion.MotionEstimator]
Returns
Expand All @@ -88,6 +88,12 @@ def adjust_frame(

# initialize background if necessary
if self._background is None:
if self.scale is None:
if coord_transformations is None:
self.scale = 1
else:
self.scale = 3

original_size = (
frame.shape[1],
frame.shape[0],
Expand All @@ -113,12 +119,12 @@ def adjust_frame(

# warp the frame with the following composition:
# top_left_translation o rel_to_abs
if isinstance(coord_transformation, HomographyTransformation):
if isinstance(coord_transformations, HomographyTransformation):
top_left_translation = np.array(
[[1, 0, self.top_left[0]], [0, 1, self.top_left[1]], [0, 0, 1]]
)
full_transformation = (
top_left_translation @ coord_transformation.inverse_homography_matrix
top_left_translation @ coord_transformations.inverse_homography_matrix
)
background_with_current_frame = cv2.warpPerspective(
frame,
Expand All @@ -128,12 +134,12 @@ def adjust_frame(
borderMode=cv2.BORDER_CONSTANT,
borderValue=(0, 0, 0),
)
elif isinstance(coord_transformation, TranslationTransformation):
elif isinstance(coord_transformations, TranslationTransformation):

full_transformation = np.array(
[
[1, 0, self.top_left[0] - coord_transformation.movement_vector[0]],
[0, 1, self.top_left[1] - coord_transformation.movement_vector[1]],
[1, 0, self.top_left[0] - coord_transformations.movement_vector[0]],
[0, 1, self.top_left[1] - coord_transformations.movement_vector[1]],
]
)
background_with_current_frame = cv2.warpAffine(
Expand All @@ -144,12 +150,14 @@ def adjust_frame(
borderMode=cv2.BORDER_CONSTANT,
borderValue=(0, 0, 0),
)

self._background = cv2.addWeighted(
self._background,
0.5,
background_with_current_frame,
0.5,
0.0,
)
return self._background
try:
self._background = cv2.addWeighted(
self._background,
0.5,
background_with_current_frame,
0.5,
0.0,
)
return self._background
except UnboundLocalError:
return frame
20 changes: 10 additions & 10 deletions norfair/drawing/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def get_points_to_draw(obj):
self.path_blend_factor = path_blend_factor
self.frame_blend_factor = frame_blend_factor

def draw(self, frame, tracked_objects, coord_transform=None):
def draw(self, frame, tracked_objects, coord_transformations=None):
"""
the objects have a relative frame: frame_det
the objects have an absolute frame: frame_one
Expand All @@ -226,19 +226,19 @@ def draw(self, frame, tracked_objects, coord_transform=None):
1. top_left is an arbitrary coordinate of some pixel inside background
logic:
1. draw track.get_estimate(absolute=True) + top_left, in background
2. transform background with the composition (coord_transform.abs_to_rel o minus_top_left_translation). If coord_transform is None, only use minus_top_left_translation.
2. transform background with the composition (coord_transformations.abs_to_rel o minus_top_left_translation). If coord_transformations is None, only use minus_top_left_translation.
3. crop [:frame.width, :frame.height] from the result
4. overlay that over frame
Remark:
In any case, coord_transform should be the coordinate transformation between the tracker absolute coords (as abs) and frame coords (as rel)
In any case, coord_transformations should be the coordinate transformation between the tracker absolute coords (as abs) and frame coords (as rel)
"""

# initialize background if necessary
if self._background is None:
if self.scale is None:
# set the default scale, depending if coord_transform is provided or not
if coord_transform is None:
# set the default scale, depending if coord_transformations is provided or not
if coord_transformations is None:
self.scale = 1
else:
self.scale = 3
Expand Down Expand Up @@ -293,12 +293,12 @@ def draw(self, frame, tracked_objects, coord_transform=None):
)

# apply warp to self._background with composition abs_to_rel o -top_left_translation to background, and crop [:width, :height] to get frame overdrawn
if isinstance(coord_transform, HomographyTransformation):
if isinstance(coord_transformations, HomographyTransformation):
minus_top_left_translation = np.array(
[[1, 0, -self.top_left[0]], [0, 1, -self.top_left[1]], [0, 0, 1]]
)
full_transformation = (
coord_transform.homography_matrix @ minus_top_left_translation
coord_transformations.homography_matrix @ minus_top_left_translation
)
background_size_frame = cv2.warpPerspective(
self._background,
Expand All @@ -308,11 +308,11 @@ def draw(self, frame, tracked_objects, coord_transform=None):
borderMode=cv2.BORDER_CONSTANT,
borderValue=(0, 0, 0),
)
elif isinstance(coord_transform, TranslationTransformation):
elif isinstance(coord_transformations, TranslationTransformation):
full_transformation = np.array(
[
[1, 0, coord_transform.movement_vector[0] - self.top_left[0]],
[0, 1, coord_transform.movement_vector[1] - self.top_left[1]],
[1, 0, coord_transformations.movement_vector[0] - self.top_left[0]],
[0, 1, coord_transformations.movement_vector[1] - self.top_left[1]],
]
)
background_size_frame = cv2.warpAffine(
Expand Down

0 comments on commit e25c7c3

Please sign in to comment.