Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save motion in DB as motion box count #10484

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions frigate/api/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ def motion_activity():
data: list[Recordings] = (
Recordings.select(
Recordings.start_time,
Recordings.regions,
Recordings.motion,
)
.where(reduce(operator.and_, clauses))
.order_by(Recordings.start_time.asc())
Expand All @@ -378,8 +378,7 @@ def motion_activity():
scale = request.args.get("scale", type=int, default=30)

# resample data using pandas to get activity on scaled basis
df = pd.DataFrame(data, columns=["start_time", "regions"])
df = df.rename(columns={"regions": "motion"})
df = pd.DataFrame(data, columns=["start_time", "motion"])

# set date as datetime index
df["start_time"] = pd.to_datetime(df["start_time"], unit="s")
Expand Down
36 changes: 7 additions & 29 deletions frigate/record/maintainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
RECORD_DIR,
)
from frigate.models import Event, Recordings
from frigate.util.image import area
from frigate.util.services import get_video_properties

logger = logging.getLogger(__name__)
Expand All @@ -39,20 +38,20 @@
class SegmentInfo:
def __init__(
self,
motion_area: int,
motion_count: int,
active_object_count: int,
region_count: int,
average_dBFS: int,
) -> None:
self.motion_area = motion_area
self.motion_count = motion_count
self.active_object_count = active_object_count
self.region_count = region_count
self.average_dBFS = average_dBFS

def should_discard_segment(self, retain_mode: RetainModeEnum) -> bool:
return (
retain_mode == RetainModeEnum.motion
and self.motion_area == 0
and self.motion_count == 0
and self.average_dBFS == 0
) or (
retain_mode == RetainModeEnum.active_objects
Expand All @@ -76,13 +75,6 @@ def __init__(self, config: FrigateConfig, stop_event: MpEvent):
self.audio_recordings_info: dict[str, list] = defaultdict(list)
self.end_time_cache: dict[str, Tuple[datetime.datetime, float]] = {}

self.camera_frame_area: dict[str, int] = {}

for camera in self.config.cameras.values():
self.camera_frame_area[camera.name] = (
camera.detect.width * camera.detect.height * 0.1
)

async def move_files(self) -> None:
cache_files = [
d
Expand Down Expand Up @@ -304,7 +296,7 @@ def segment_stats(
video_frame_count = 0
active_count = 0
region_count = 0
total_motion_area = 0
motion_count = 0
for frame in self.object_recordings_info[camera]:
# frame is after end time of segment
if frame[0] > end_time.timestamp():
Expand All @@ -321,23 +313,9 @@ def segment_stats(
if not o["false_positive"] and o["motionless_count"] == 0
]
)
total_motion_area += sum([area(box) for box in frame[2]])
motion_count += len(frame[2])
region_count += len(frame[3])

if video_frame_count > 0:
normalized_motion_area = min(
int(
(
total_motion_area
/ (self.camera_frame_area[camera] * video_frame_count)
)
* 100
),
100,
)
else:
normalized_motion_area = 0

audio_values = []
for frame in self.audio_recordings_info[camera]:
# frame is after end time of segment
Expand All @@ -357,7 +335,7 @@ def segment_stats(
average_dBFS = 0 if not audio_values else np.average(audio_values)

return SegmentInfo(
normalized_motion_area, active_count, region_count, round(average_dBFS)
motion_count, active_count, region_count, round(average_dBFS)
)

async def move_segment(
Expand Down Expand Up @@ -443,7 +421,7 @@ async def move_segment(
Recordings.start_time: start_time.timestamp(),
Recordings.end_time: end_time.timestamp(),
Recordings.duration: duration,
Recordings.motion: segment_info.motion_area,
Recordings.motion: segment_info.motion_count,
# TODO: update this to store list of active objects at some point
Recordings.objects: segment_info.active_object_count,
Recordings.regions: segment_info.region_count,
Expand Down
8 changes: 4 additions & 4 deletions frigate/test/test_record_retention.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@
class TestRecordRetention(unittest.TestCase):
def test_motion_should_keep_motion_not_object(self):
segment_info = SegmentInfo(
motion_area=1, active_object_count=0, region_count=0, average_dBFS=0
motion_count=1, active_object_count=0, region_count=0, average_dBFS=0
)
assert not segment_info.should_discard_segment(RetainModeEnum.motion)
assert segment_info.should_discard_segment(RetainModeEnum.active_objects)

def test_object_should_keep_object_not_motion(self):
segment_info = SegmentInfo(
motion_area=0, active_object_count=1, region_count=0, average_dBFS=0
motion_count=0, active_object_count=1, region_count=0, average_dBFS=0
)
assert segment_info.should_discard_segment(RetainModeEnum.motion)
assert not segment_info.should_discard_segment(RetainModeEnum.active_objects)

def test_all_should_keep_all(self):
segment_info = SegmentInfo(
motion_area=0, active_object_count=0, region_count=0, average_dBFS=0
motion_count=0, active_object_count=0, region_count=0, average_dBFS=0
)
assert not segment_info.should_discard_segment(RetainModeEnum.all)

def test_should_keep_audio_in_motion_mode(self):
segment_info = SegmentInfo(
motion_area=0, active_object_count=0, region_count=0, average_dBFS=1
motion_count=0, active_object_count=0, region_count=0, average_dBFS=1
)
assert not segment_info.should_discard_segment(RetainModeEnum.motion)
assert segment_info.should_discard_segment(RetainModeEnum.active_objects)
Loading