Skip to content

Commit

Permalink
[overlays] Fix incorrect bounding box overlay on masked region
Browse files Browse the repository at this point in the history
  • Loading branch information
Breakthrough committed Oct 16, 2023
1 parent 9aadd69 commit e1ecc32
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions dvr_scan/overlays.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def update(self, motion_mask: numpy.ndarray):
self._smoothing_window = self._smoothing_window[-smoothing_amount:]
return self._get_smoothed_window()

def draw(self, frame: numpy.ndarray, bounding_box: Tuple[int, int, int, int]):
def draw(self, frame: numpy.ndarray, bounding_box: Tuple[int, int, int, int], use_shift: bool):
"""Draw a bounding box onto a target frame using the provided ROI and downscale factor."""
# Correct for downscale factor
bounding_box = [side_len * self._downscale_factor for side_len in bounding_box]
Expand All @@ -210,7 +210,7 @@ def draw(self, frame: numpy.ndarray, bounding_box: Tuple[int, int, int, int]):
top_left = (top_left[0] - correction_x // 2, top_left[1] - correction_y // 2)
bottom_right = (bottom_right[0] + correction_x // 2, bottom_right[1] + correction_y // 2)
# Shift bounding box if ROI was set
if self._shift:
if self._shift and use_shift:
top_left = (top_left[0] + self._shift[0], top_left[1] + self._shift[1])
bottom_right = (bottom_right[0] + self._shift[0], bottom_right[1] + self._shift[1])
# Ensure coordinates are positive. Values greater than frame size are okay, and should be
Expand Down
8 changes: 5 additions & 3 deletions dvr_scan/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def scan(self) -> Optional[DetectionResult]:

# Length of buffer we require in memory to keep track of all frames required for -l and -tb.
buff_len = pre_event_len + min_event_len
event_end = FrameTimecode(timecode=0, fps=self._input.framerate)
event_end = self._input.position
last_frame_above_threshold = 0

if self._bounding_box:
Expand Down Expand Up @@ -847,14 +847,15 @@ def _draw_overlays(
timecode: FrameTimecode,
frame_score: float,
bounding_box: Optional[Tuple[int, int, int, int]],
use_shift=True,
):
if not self._timecode_overlay is None:
self._timecode_overlay.draw(frame, text=timecode.get_timecode())
if not self._metrics_overlay is None:
to_display = "Frame: %04d\nScore: %3.2f" % (timecode.get_frames(), frame_score)
self._metrics_overlay.draw(frame, text=to_display)
if not self._bounding_box is None and not bounding_box is None:
self._bounding_box.draw(frame, bounding_box)
self._bounding_box.draw(frame, bounding_box, use_shift)

def _on_mask_event(self, event: MotionMaskEvent):
# Initialize the VideoWriter used for mask output.
Expand All @@ -863,7 +864,8 @@ def _on_mask_event(self, event: MotionMaskEvent):
self._mask_writer = self._init_video_writer(self._mask_file, resolution)
# Write the motion mask to the output file.
out_frame = cv2.cvtColor(event.motion_mask, cv2.COLOR_GRAY2BGR)
self._draw_overlays(out_frame, event.timecode, event.score, event.bounding_box)
self._draw_overlays(
out_frame, event.timecode, event.score, event.bounding_box, use_shift=False)
self._mask_writer.write(out_frame)

def _on_motion_event(self, event: MotionEvent):
Expand Down

0 comments on commit e1ecc32

Please sign in to comment.