Skip to content

Commit e1ecc32

Browse files
committed
[overlays] Fix incorrect bounding box overlay on masked region
1 parent 9aadd69 commit e1ecc32

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

dvr_scan/overlays.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def update(self, motion_mask: numpy.ndarray):
195195
self._smoothing_window = self._smoothing_window[-smoothing_amount:]
196196
return self._get_smoothed_window()
197197

198-
def draw(self, frame: numpy.ndarray, bounding_box: Tuple[int, int, int, int]):
198+
def draw(self, frame: numpy.ndarray, bounding_box: Tuple[int, int, int, int], use_shift: bool):
199199
"""Draw a bounding box onto a target frame using the provided ROI and downscale factor."""
200200
# Correct for downscale factor
201201
bounding_box = [side_len * self._downscale_factor for side_len in bounding_box]
@@ -210,7 +210,7 @@ def draw(self, frame: numpy.ndarray, bounding_box: Tuple[int, int, int, int]):
210210
top_left = (top_left[0] - correction_x // 2, top_left[1] - correction_y // 2)
211211
bottom_right = (bottom_right[0] + correction_x // 2, bottom_right[1] + correction_y // 2)
212212
# Shift bounding box if ROI was set
213-
if self._shift:
213+
if self._shift and use_shift:
214214
top_left = (top_left[0] + self._shift[0], top_left[1] + self._shift[1])
215215
bottom_right = (bottom_right[0] + self._shift[0], bottom_right[1] + self._shift[1])
216216
# Ensure coordinates are positive. Values greater than frame size are okay, and should be

dvr_scan/scanner.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ def scan(self) -> Optional[DetectionResult]:
590590

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

596596
if self._bounding_box:
@@ -847,14 +847,15 @@ def _draw_overlays(
847847
timecode: FrameTimecode,
848848
frame_score: float,
849849
bounding_box: Optional[Tuple[int, int, int, int]],
850+
use_shift=True,
850851
):
851852
if not self._timecode_overlay is None:
852853
self._timecode_overlay.draw(frame, text=timecode.get_timecode())
853854
if not self._metrics_overlay is None:
854855
to_display = "Frame: %04d\nScore: %3.2f" % (timecode.get_frames(), frame_score)
855856
self._metrics_overlay.draw(frame, text=to_display)
856857
if not self._bounding_box is None and not bounding_box is None:
857-
self._bounding_box.draw(frame, bounding_box)
858+
self._bounding_box.draw(frame, bounding_box, use_shift)
858859

859860
def _on_mask_event(self, event: MotionMaskEvent):
860861
# Initialize the VideoWriter used for mask output.
@@ -863,7 +864,8 @@ def _on_mask_event(self, event: MotionMaskEvent):
863864
self._mask_writer = self._init_video_writer(self._mask_file, resolution)
864865
# Write the motion mask to the output file.
865866
out_frame = cv2.cvtColor(event.motion_mask, cv2.COLOR_GRAY2BGR)
866-
self._draw_overlays(out_frame, event.timecode, event.score, event.bounding_box)
867+
self._draw_overlays(
868+
out_frame, event.timecode, event.score, event.bounding_box, use_shift=False)
867869
self._mask_writer.write(out_frame)
868870

869871
def _on_motion_event(self, event: MotionEvent):

0 commit comments

Comments
 (0)