mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-11-17 02:53:01 +03:00
Fix camera name prefix matching (#24992)
* only remove the deleted camera's media files
Camera deletion globbed snapshot, clean copy, and review thumbnail files by "{camera}-*", and camera names can contain "-", so deleting "front" also removed every one of those files for "front-door". Files are now matched on the full name, the camera followed by an event or review id.
* only use the camera's own preview frames for review descriptions
get_cache_frames matched cached preview frames by "preview_{camera}-", so when "front" had no frame after the review ended, the first "front-door" frame became the trailing frame and went into front's GenAI prompt. It now uses is_camera_preview_frame like the preview recorder and exports.
* keep cleaning up when a media directory can't be listed
This commit is contained in:
@@ -35,6 +35,7 @@ from frigate.data_processing.types import PostProcessDataEnum
|
||||
from frigate.genai import GenAIClient
|
||||
from frigate.genai.manager import GenAIClientManager
|
||||
from frigate.models import Event, Recordings, ReviewSegment
|
||||
from frigate.output.preview import is_camera_preview_frame
|
||||
from frigate.util.builtin import EventsPerSecond, InferenceSpeed
|
||||
from frigate.util.image import get_image_from_recording
|
||||
from frigate.util.recording_coverage import recording_overlap
|
||||
@@ -528,7 +529,7 @@ class ReviewDescriptionProcessor(PostProcessorApi):
|
||||
camera_files = [
|
||||
entry.name
|
||||
for entry in os.scandir(preview_dir)
|
||||
if entry.name.startswith(file_start)
|
||||
if is_camera_preview_frame(entry.name, camera)
|
||||
]
|
||||
camera_files.sort()
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
"""Regression tests for runtime camera add and delete handling."""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import tempfile
|
||||
import threading
|
||||
import unittest
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
# LicensePlatePostProcessor is imported via the maintainer rather than from
|
||||
# data_processing.post.license_plate, which circularly imports back through
|
||||
@@ -16,6 +18,7 @@ from frigate.embeddings.maintainer import (
|
||||
from frigate.ptz.autotrack import PtzAutoTracker
|
||||
from frigate.review.maintainer import ReviewSegmentMaintainer
|
||||
from frigate.track.object_processing import TrackedObjectProcessor
|
||||
from frigate.util import camera_cleanup
|
||||
|
||||
|
||||
def _make_processor() -> TrackedObjectProcessor:
|
||||
@@ -231,3 +234,33 @@ class TestCameraStateAccessors(unittest.TestCase):
|
||||
processor = _make_processor()
|
||||
|
||||
self.assertEqual(processor.get_current_frame_time("deleted_cam"), 0.0)
|
||||
|
||||
|
||||
class TestCameraFileCleanup(unittest.TestCase):
|
||||
def test_delete_keeps_media_of_camera_sharing_name_prefix(self):
|
||||
own = [
|
||||
"front-1700000000.123456-abc123.jpg",
|
||||
"front-1700000000.123456-abc123-clean.webp",
|
||||
"review/thumb-front-1700000000.5-abc123.webp",
|
||||
]
|
||||
other = [
|
||||
"front-door-1700000000.123456-def456.jpg",
|
||||
"front-door-1700000000.123456-def456-clean.webp",
|
||||
"review/thumb-front-door-1700000000.5-def456.webp",
|
||||
]
|
||||
|
||||
with tempfile.TemporaryDirectory() as clips:
|
||||
os.makedirs(os.path.join(clips, "review"))
|
||||
for name in own + other:
|
||||
open(os.path.join(clips, name), "w").close()
|
||||
|
||||
with patch.multiple(
|
||||
camera_cleanup, CLIPS_DIR=clips, RECORD_DIR=clips, THUMB_DIR=clips
|
||||
):
|
||||
camera_cleanup.cleanup_camera_files("front")
|
||||
|
||||
remaining = [
|
||||
n for n in own + other if os.path.exists(os.path.join(clips, n))
|
||||
]
|
||||
|
||||
self.assertEqual(remaining, other)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
"""Utilities for cleaning up camera data from database and filesystem."""
|
||||
|
||||
import glob
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
|
||||
from frigate.const import CLIPS_DIR, RECORD_DIR, THUMB_DIR
|
||||
@@ -126,33 +126,39 @@ def cleanup_camera_files(
|
||||
except Exception as e:
|
||||
logger.error("Failed to remove %s: %s", dir_path, e)
|
||||
|
||||
# Remove event snapshot files
|
||||
for snapshot in glob.glob(os.path.join(CLIPS_DIR, f"{camera_name}-*.jpg")):
|
||||
try:
|
||||
os.remove(snapshot)
|
||||
except Exception as e:
|
||||
logger.error("Failed to remove snapshot %s: %s", snapshot, e)
|
||||
# Match the whole file name so deleting "front" keeps the files of "front-door"
|
||||
media_id = r"[\d.]+-[a-z0-9]{6}"
|
||||
camera = re.escape(camera_name)
|
||||
media_patterns = [
|
||||
(
|
||||
CLIPS_DIR,
|
||||
re.compile(rf"{camera}-{media_id}(\.jpg|-clean\.webp|-clean\.png)"),
|
||||
),
|
||||
(
|
||||
os.path.join(CLIPS_DIR, "review"),
|
||||
re.compile(rf"thumb-{camera}-{media_id}\.webp"),
|
||||
),
|
||||
]
|
||||
|
||||
for snapshot in glob.glob(os.path.join(CLIPS_DIR, f"{camera_name}-*-clean.webp")):
|
||||
try:
|
||||
os.remove(snapshot)
|
||||
except Exception as e:
|
||||
logger.error("Failed to remove snapshot %s: %s", snapshot, e)
|
||||
for directory, pattern in media_patterns:
|
||||
if not os.path.isdir(directory):
|
||||
continue
|
||||
|
||||
for snapshot in glob.glob(os.path.join(CLIPS_DIR, f"{camera_name}-*-clean.png")):
|
||||
try:
|
||||
os.remove(snapshot)
|
||||
except Exception as e:
|
||||
logger.error("Failed to remove snapshot %s: %s", snapshot, e)
|
||||
files = os.listdir(directory)
|
||||
except OSError as e:
|
||||
logger.error("Failed to list %s: %s", directory, e)
|
||||
continue
|
||||
|
||||
# Remove review thumbnail files
|
||||
for thumb in glob.glob(
|
||||
os.path.join(CLIPS_DIR, "review", f"thumb-{camera_name}-*.webp")
|
||||
):
|
||||
try:
|
||||
os.remove(thumb)
|
||||
except Exception as e:
|
||||
logger.error("Failed to remove review thumbnail %s: %s", thumb, e)
|
||||
for file in files:
|
||||
if not pattern.fullmatch(file):
|
||||
continue
|
||||
|
||||
path = os.path.join(directory, file)
|
||||
try:
|
||||
os.remove(path)
|
||||
except Exception as e:
|
||||
logger.error("Failed to remove media file %s: %s", path, e)
|
||||
|
||||
# Remove export files if requested
|
||||
if export_paths:
|
||||
|
||||
Reference in New Issue
Block a user