NVR with realtime local object detection for IP cameras
TypeScript aicameragoogle-coralhome-assistanthome-automationhomeautomationmqttnvrobject-detectionrealtimertsptensorflow
Require setting process priority for FrigateProcess (#19207)
Nicolas Mowen20104761e801
diff --git a/frigate/const.py b/frigate/const.pyindex 5a5ee3f244..f02dab1dc2 100644--- a/frigate/const.py+++ b/frigate/const.py@@ -116,6 +116,12 @@ UPDATE_EMBEDDINGS_REINDEX_PROGRESS = "handle_embeddings_reindex_progress"UPDATE_BIRDSEYE_LAYOUT = "update_birdseye_layout"NOTIFICATION_TEST = "notification_test"+# IO Nice Values++PROCESS_PRIORITY_HIGH = 0+PROCESS_PRIORITY_MED = 10+PROCESS_PRIORITY_LOW = 19+# Stats ValuesFREQUENCY_STATS_POINTS = 15diff --git a/frigate/embeddings/__init__.py b/frigate/embeddings/__init__.pyindex ab69adb681..03316ca58f 100644--- a/frigate/embeddings/__init__.py+++ b/frigate/embeddings/__init__.py@@ -14,7 +14,7 @@ from pathvalidate import ValidationError, sanitize_filenamefrom frigate.comms.embeddings_updater import EmbeddingsRequestEnum, EmbeddingsRequestorfrom frigate.config import FrigateConfig-from frigate.const import CONFIG_DIR, FACE_DIR+from frigate.const import CONFIG_DIR, FACE_DIR, PROCESS_PRIORITY_HIGHfrom frigate.data_processing.types import DataProcessorMetricsfrom frigate.db.sqlitevecq import SqliteVecQueueDatabasefrom frigate.models import Event@@ -35,7 +35,12 @@ class EmbeddingProcess(FrigateProcess):metrics: DataProcessorMetrics | None,stop_event: MpEvent,) -> None:- super().__init__(stop_event, name="frigate.embeddings_manager", daemon=True)+ super().__init__(+ stop_event,+ PROCESS_PRIORITY_HIGH,+ name="frigate.embeddings_manager",+ daemon=True,+ )self.config = configself.metrics = metricsdiff --git a/frigate/events/audio.py b/frigate/events/audio.pyindex f99e6fe411..8446ca48df 100644--- a/frigate/events/audio.py+++ b/frigate/events/audio.py@@ -29,6 +29,7 @@ from frigate.const import (AUDIO_MAX_BIT_RANGE,AUDIO_MIN_CONFIDENCE,AUDIO_SAMPLE_RATE,+ PROCESS_PRIORITY_HIGH,)from frigate.data_processing.common.audio_transcription.model import (AudioTranscriptionModelRunner,@@ -90,7 +91,9 @@ class AudioProcessor(FrigateProcess):camera_metrics: DictProxy,stop_event: MpEvent,):- super().__init__(stop_event, name="frigate.audio_manager", daemon=True)+ super().__init__(+ stop_event, PROCESS_PRIORITY_HIGH, name="frigate.audio_manager", daemon=True+ )self.camera_metrics = camera_metricsself.cameras = camerasdiff --git a/frigate/object_detection/base.py b/frigate/object_detection/base.pyindex 32f33ffa5b..921f88b465 100644--- a/frigate/object_detection/base.py+++ b/frigate/object_detection/base.py@@ -12,6 +12,7 @@ from frigate.comms.object_detector_signaler import (ObjectDetectorSubscriber,)from frigate.config import FrigateConfig+from frigate.const import PROCESS_PRIORITY_HIGHfrom frigate.detectors import create_detectorfrom frigate.detectors.detector_config import (BaseDetectorConfig,@@ -97,7 +98,7 @@ class DetectorRunner(FrigateProcess):detector_config: BaseDetectorConfig,stop_event: MpEvent,) -> None:- super().__init__(stop_event, name=name, daemon=True)+ super().__init__(stop_event, PROCESS_PRIORITY_HIGH, name=name, daemon=True)self.detection_queue = detection_queueself.cameras = camerasself.avg_speed = avg_speeddiff --git a/frigate/output/output.py b/frigate/output/output.pyindex 34c9e33c01..4d7ff24667 100644--- a/frigate/output/output.py+++ b/frigate/output/output.py@@ -22,7 +22,7 @@ from frigate.config.camera.updater import (CameraConfigUpdateEnum,CameraConfigUpdateSubscriber,)-from frigate.const import CACHE_DIR, CLIPS_DIR+from frigate.const import CACHE_DIR, CLIPS_DIR, PROCESS_PRIORITY_MEDfrom frigate.output.birdseye import Birdseyefrom frigate.output.camera import JsmpegCamerafrom frigate.output.preview import PreviewRecorder@@ -74,7 +74,9 @@ def check_disabled_camera_update(class OutputProcess(FrigateProcess):def __init__(self, config: FrigateConfig, stop_event: MpEvent) -> None:- super().__init__(stop_event, name="frigate.output", daemon=True)+ super().__init__(+ stop_event, PROCESS_PRIORITY_MED, name="frigate.output", daemon=True+ )self.config = configdef run(self) -> None:diff --git a/frigate/record/export.py b/frigate/record/export.pyindex 0d3f96da09..1d56baf157 100644--- a/frigate/record/export.py+++ b/frigate/record/export.py@@ -21,6 +21,7 @@ from frigate.const import (EXPORT_DIR,MAX_PLAYLIST_SECONDS,PREVIEW_FRAME_TYPE,+ PROCESS_PRIORITY_LOW,)from frigate.ffmpeg_presets import (EncodeTypeEnum,@@ -36,7 +37,7 @@ TIMELAPSE_DATA_INPUT_ARGS = "-an -skip_frame nokey"def lower_priority():- os.nice(10)+ os.nice(PROCESS_PRIORITY_LOW)class PlaybackFactorEnum(str, Enum):diff --git a/frigate/record/record.py b/frigate/record/record.pyindex b04a68e8c4..624ed6e9a2 100644--- a/frigate/record/record.py+++ b/frigate/record/record.py@@ -6,6 +6,7 @@ from multiprocessing.synchronize import Event as MpEventfrom playhouse.sqliteq import SqliteQueueDatabasefrom frigate.config import FrigateConfig+from frigate.const import PROCESS_PRIORITY_HIGHfrom frigate.models import Recordings, ReviewSegmentfrom frigate.record.maintainer import RecordingMaintainerfrom frigate.util.process import FrigateProcess@@ -15,7 +16,12 @@ logger = logging.getLogger(__name__)class RecordProcess(FrigateProcess):def __init__(self, config: FrigateConfig, stop_event: MpEvent) -> None:- super().__init__(stop_event, name="frigate.recording_manager", daemon=True)+ super().__init__(+ stop_event,+ PROCESS_PRIORITY_HIGH,+ name="frigate.recording_manager",+ daemon=True,+ )self.config = configdef run(self) -> None:diff --git a/frigate/review/review.py b/frigate/review/review.pyindex 917a53a4b0..c00c302a20 100644--- a/frigate/review/review.py+++ b/frigate/review/review.py@@ -4,6 +4,7 @@ import loggingfrom multiprocessing.synchronize import Event as MpEventfrom frigate.config import FrigateConfig+from frigate.const import PROCESS_PRIORITY_MEDfrom frigate.review.maintainer import ReviewSegmentMaintainerfrom frigate.util.process import FrigateProcess@@ -12,7 +13,12 @@ logger = logging.getLogger(__name__)class ReviewProcess(FrigateProcess):def __init__(self, config: FrigateConfig, stop_event: MpEvent) -> None:- super().__init__(stop_event, name="frigate.review_segment_manager", daemon=True)+ super().__init__(+ stop_event,+ PROCESS_PRIORITY_MED,+ name="frigate.review_segment_manager",+ daemon=True,+ )self.config = configdef run(self) -> None:diff --git a/frigate/util/classification.py b/frigate/util/classification.pyindex 533c1345a1..87942a9166 100644--- a/frigate/util/classification.py+++ b/frigate/util/classification.py@@ -8,7 +8,12 @@ import numpy as npfrom frigate.comms.embeddings_updater import EmbeddingsRequestEnum, EmbeddingsRequestorfrom frigate.comms.inter_process import InterProcessRequestor-from frigate.const import CLIPS_DIR, MODEL_CACHE_DIR, UPDATE_MODEL_STATE+from frigate.const import (+ CLIPS_DIR,+ MODEL_CACHE_DIR,+ UPDATE_MODEL_STATE,+ PROCESS_PRIORITY_LOW,+)from frigate.log import redirect_output_to_loggerfrom frigate.types import ModelStatusTypesEnumfrom frigate.util.process import FrigateProcess@@ -24,6 +29,7 @@ class ClassificationTrainingProcess(FrigateProcess):def __init__(self, model_name: str) -> None:super().__init__(stop_event=None,+ priority=PROCESS_PRIORITY_LOW,name=f"model_training:{model_name}",)self.model_name = model_namediff --git a/frigate/util/process.py b/frigate/util/process.pyindex 830818d4d0..b9fede44ef 100644--- a/frigate/util/process.py+++ b/frigate/util/process.py@@ -1,6 +1,7 @@import faulthandlerimport loggingimport multiprocessing as mp+import osimport threadingfrom logging.handlers import QueueHandlerfrom multiprocessing.synchronize import Event as MpEvent@@ -16,6 +17,7 @@ class BaseProcess(mp.Process):def __init__(self,stop_event: MpEvent,+ priority: int,*,name: Optional[str] = None,target: Optional[Callable] = None,@@ -23,6 +25,7 @@ class BaseProcess(mp.Process):kwargs: dict = {},daemon: Optional[bool] = None,):+ self.priority = priorityself.stop_event = stop_eventsuper().__init__(name=name, target=target, args=args, kwargs=kwargs, daemon=daemon@@ -47,6 +50,7 @@ class FrigateProcess(BaseProcess):self.__log_queue = frigate.log.log_listener.queuedef pre_run_setup(self, logConfig: LoggerConfig | None = None) -> None:+ os.nice(self.priority)setproctitle(self.name)threading.current_thread().name = f"process:{self.name}"faulthandler.enable()diff --git a/frigate/video.py b/frigate/video.pyindex 3bc2702a5a..a6381fbe34 100755--- a/frigate/video.py+++ b/frigate/video.py@@ -23,6 +23,7 @@ from frigate.const import (CACHE_DIR,CACHE_SEGMENT_FORMAT,REQUEST_REGION_GRID,+ PROCESS_PRIORITY_HIGH,)from frigate.log import LogPipefrom frigate.motion import MotionDetector@@ -455,7 +456,12 @@ class CameraCapture(FrigateProcess):camera_metrics: CameraMetrics,stop_event: MpEvent,) -> None:- super().__init__(stop_event, name=f"frigate.capture:{config.name}", daemon=True)+ super().__init__(+ stop_event,+ PROCESS_PRIORITY_HIGH,+ name=f"frigate.capture:{config.name}",+ daemon=True,+ )self.config = configself.shm_frame_count = shm_frame_countself.camera_metrics = camera_metrics@@ -488,7 +494,12 @@ class CameraTracker(FrigateProcess):region_grid: list[list[dict[str, Any]]],stop_event: MpEvent,) -> None:- super().__init__(stop_event, name=f"frigate.process:{config.name}", daemon=True)+ super().__init__(+ stop_event,+ PROCESS_PRIORITY_HIGH,+ name=f"frigate.process:{config.name}",+ daemon=True,+ )self.config = configself.model_config = model_configself.labelmap = labelmap