Skip to content

Commit

Permalink
Don't zero out motion during calibration (blakeblackshear#8163)
Browse files Browse the repository at this point in the history
* don't zero out motion boxes

* define detect resolution to speed up tests
  • Loading branch information
blakeblackshear committed Oct 14, 2023
1 parent fa6c6c5 commit 9ea10f8
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 23 deletions.
4 changes: 4 additions & 0 deletions frigate/motion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ def __init__(
@abstractmethod
def detect(self, frame):
pass

@abstractmethod
def is_calibrating(self):
pass
3 changes: 3 additions & 0 deletions frigate/motion/frigate_motion.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def __init__(
self.threshold = threshold
self.contour_area = contour_area

def is_calibrating(self):
return False

def detect(self, frame):
motion_boxes = []

Expand Down
4 changes: 3 additions & 1 deletion frigate/motion/improved_motion.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def __init__(
self.contrast_values[:, 1:2] = 255
self.contrast_values_index = 0

def is_calibrating(self):
return self.calibrating

def detect(self, frame):
motion_boxes = []

Expand Down Expand Up @@ -141,7 +144,6 @@ def detect(self, frame):

# if calibrating or the motion contours are > 80% of the image area (lightning, ir, ptz) recalibrate
if self.calibrating or pct_motion > self.config.lightning_threshold:
motion_boxes = []
self.calibrating = True

if self.save_images:
Expand Down
110 changes: 105 additions & 5 deletions frigate/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,12 @@ def test_default_detect(self):
"roles": ["detect"],
},
]
}
},
"detect": {
"height": 720,
"width": 1280,
"fps": 5,
},
}
},
}
Expand Down Expand Up @@ -1082,6 +1087,11 @@ def test_global_snapshots(self):
},
]
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
"snapshots": {
"height": 100,
},
Expand All @@ -1107,7 +1117,12 @@ def test_default_snapshots(self):
"roles": ["detect"],
},
]
}
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
}
},
}
Expand All @@ -1132,6 +1147,11 @@ def test_global_snapshots_merge(self):
},
]
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
"snapshots": {
"height": 150,
"enabled": True,
Expand Down Expand Up @@ -1160,6 +1180,11 @@ def test_global_rtmp_disabled(self):
},
]
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
}
},
}
Expand All @@ -1181,7 +1206,12 @@ def test_default_not_rtmp(self):
"roles": ["detect"],
},
]
}
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
}
},
}
Expand All @@ -1205,6 +1235,11 @@ def test_global_rtmp_merge(self):
},
]
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
"rtmp": {
"enabled": True,
},
Expand Down Expand Up @@ -1234,6 +1269,11 @@ def test_global_rtmp_default(self):
},
]
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
}
},
}
Expand All @@ -1257,6 +1297,11 @@ def test_global_jsmpeg(self):
},
]
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
}
},
}
Expand All @@ -1278,7 +1323,12 @@ def test_default_live(self):
"roles": ["detect"],
},
]
}
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
}
},
}
Expand All @@ -1302,6 +1352,11 @@ def test_global_live_merge(self):
},
]
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
"live": {
"quality": 7,
},
Expand Down Expand Up @@ -1329,6 +1384,11 @@ def test_global_timestamp_style(self):
},
]
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
}
},
}
Expand All @@ -1350,7 +1410,12 @@ def test_default_timestamp_style(self):
"roles": ["detect"],
},
]
}
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
}
},
}
Expand All @@ -1375,6 +1440,11 @@ def test_global_timestamp_style_merge(self):
},
]
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
"timestamp_style": {"position": "bl", "thickness": 4},
}
},
Expand All @@ -1400,6 +1470,11 @@ def test_allow_retain_to_be_a_decimal(self):
},
]
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
}
},
}
Expand All @@ -1423,6 +1498,11 @@ def test_fails_on_bad_camera_name(self):
},
]
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
}
},
}
Expand Down Expand Up @@ -1450,6 +1530,11 @@ def test_fails_on_bad_segment_time(self):
},
],
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
}
},
}
Expand All @@ -1475,6 +1560,11 @@ def test_fails_zone_defines_untracked_object(self):
},
]
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
"zones": {
"steps": {
"coordinates": "0,0,0,0",
Expand Down Expand Up @@ -1546,6 +1636,11 @@ def test_valid_movement_weights(self):
{"path": "rtsp:https://10.0.0.1:554/video", "roles": ["detect"]}
]
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
"onvif": {"autotracking": {"movement_weights": "1.23, 2.34, 0.50"}},
}
},
Expand All @@ -1569,6 +1664,11 @@ def test_fails_invalid_movement_weights(self):
{"path": "rtsp:https://10.0.0.1:554/video", "roles": ["detect"]}
]
},
"detect": {
"height": 1080,
"width": 1920,
"fps": 5,
},
"onvif": {"autotracking": {"movement_weights": "1.234, 2.345a"}},
}
},
Expand Down
27 changes: 10 additions & 17 deletions frigate/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from frigate.motion import MotionDetector
from frigate.motion.improved_motion import ImprovedMotionDetector
from frigate.object_detection import RemoteObjectDetector
from frigate.ptz.autotrack import ptz_moving_at_frame_time
from frigate.track import ObjectTracker
from frigate.track.norfair_tracker import NorfairTracker
from frigate.types import PTZMetricsTypes
Expand Down Expand Up @@ -777,19 +776,8 @@ def process_frames(
logger.info(f"{camera_name}: frame {frame_time} is not in memory store.")
continue

# look for motion if enabled and ptz is not moving
# ptz_moving_at_frame_time() always returns False for
# non ptz/autotracking cameras
motion_boxes = (
motion_detector.detect(frame)
if motion_enabled.value
and not ptz_moving_at_frame_time(
frame_time,
ptz_metrics["ptz_start_time"].value,
ptz_metrics["ptz_stop_time"].value,
)
else []
)
# look for motion if enabled
motion_boxes = motion_detector.detect(frame) if motion_enabled.value else []

regions = []
consolidated_detections = []
Expand All @@ -814,8 +802,10 @@ def process_frames(
)
# and it hasn't disappeared
and object_tracker.disappeared[obj["id"]] == 0
# and it doesn't overlap with any current motion boxes
and not intersects_any(obj["box"], motion_boxes)
# and it doesn't overlap with any current motion boxes when not calibrating
and not intersects_any(
obj["box"], [] if motion_detector.is_calibrating() else motion_boxes
)
]

# get tracked object boxes that aren't stationary
Expand All @@ -825,7 +815,10 @@ def process_frames(
if obj["id"] not in stationary_object_ids
]

combined_boxes = motion_boxes + tracked_object_boxes
combined_boxes = tracked_object_boxes
# only add in the motion boxes when not calibrating
if not motion_detector.is_calibrating():
combined_boxes += motion_boxes

cluster_candidates = get_cluster_candidates(
frame_shape, region_min_size, combined_boxes
Expand Down

0 comments on commit 9ea10f8

Please sign in to comment.