2023-06-11 14:23:18 +02:00
|
|
|
import datetime
|
2020-11-04 13:28:07 +01:00
|
|
|
import logging
|
|
|
|
import multiprocessing as mp
|
|
|
|
import os
|
2023-01-31 00:42:23 +01:00
|
|
|
import shutil
|
2021-06-14 14:31:13 +02:00
|
|
|
import signal
|
|
|
|
import sys
|
2023-05-29 12:31:17 +02:00
|
|
|
import traceback
|
|
|
|
from multiprocessing.synchronize import Event as MpEvent
|
2022-04-16 17:40:04 +02:00
|
|
|
from types import FrameType
|
2023-05-29 12:31:17 +02:00
|
|
|
from typing import Optional
|
2020-11-04 13:28:07 +01:00
|
|
|
|
2023-05-29 12:31:17 +02:00
|
|
|
import psutil
|
2023-06-28 12:39:39 +02:00
|
|
|
from faster_fifo import Queue
|
2020-12-24 14:47:27 +01:00
|
|
|
from peewee_migrate import Router
|
2020-11-04 13:28:07 +01:00
|
|
|
from playhouse.sqlite_ext import SqliteExtDatabase
|
2021-01-24 13:53:01 +01:00
|
|
|
from playhouse.sqliteq import SqliteQueueDatabase
|
2020-11-04 13:28:07 +01:00
|
|
|
|
2022-11-24 03:03:20 +01:00
|
|
|
from frigate.comms.dispatcher import Communicator, Dispatcher
|
|
|
|
from frigate.comms.mqtt import MqttClient
|
|
|
|
from frigate.comms.ws import WebSocketClient
|
|
|
|
from frigate.config import FrigateConfig
|
2023-04-30 20:32:36 +02:00
|
|
|
from frigate.const import (
|
|
|
|
CACHE_DIR,
|
|
|
|
CLIPS_DIR,
|
|
|
|
CONFIG_DIR,
|
|
|
|
DEFAULT_DB_PATH,
|
2023-06-08 13:32:35 +02:00
|
|
|
EXPORT_DIR,
|
2023-04-30 20:32:36 +02:00
|
|
|
MODEL_CACHE_DIR,
|
|
|
|
RECORD_DIR,
|
|
|
|
)
|
2023-07-01 15:18:33 +02:00
|
|
|
from frigate.events.audio import listen_to_audio
|
2023-05-19 12:16:11 +02:00
|
|
|
from frigate.events.cleanup import EventCleanup
|
|
|
|
from frigate.events.external import ExternalEventProcessor
|
|
|
|
from frigate.events.maintainer import EventProcessor
|
2020-11-04 13:28:07 +01:00
|
|
|
from frigate.http import create_app
|
|
|
|
from frigate.log import log_process, root_configurer
|
2023-04-23 17:45:19 +02:00
|
|
|
from frigate.models import Event, Recordings, Timeline
|
2023-05-29 12:31:17 +02:00
|
|
|
from frigate.object_detection import ObjectDetectProcess
|
2020-11-04 13:28:07 +01:00
|
|
|
from frigate.object_processing import TrackedObjectProcessor
|
2021-05-29 20:27:00 +02:00
|
|
|
from frigate.output import output_frames
|
2022-04-03 22:00:11 +02:00
|
|
|
from frigate.plus import PlusApi
|
2023-04-26 13:08:53 +02:00
|
|
|
from frigate.ptz import OnvifController
|
2023-04-26 15:25:26 +02:00
|
|
|
from frigate.record.record import manage_recordings
|
2021-01-04 00:35:58 +01:00
|
|
|
from frigate.stats import StatsEmitter, stats_init
|
2022-10-09 13:28:26 +02:00
|
|
|
from frigate.storage import StorageMaintainer
|
2023-04-23 17:45:19 +02:00
|
|
|
from frigate.timeline import TimelineProcessor
|
2023-07-01 15:18:33 +02:00
|
|
|
from frigate.types import CameraMetricsTypes, FeatureMetricsTypes
|
2021-09-14 05:02:23 +02:00
|
|
|
from frigate.version import VERSION
|
2020-11-04 13:28:07 +01:00
|
|
|
from frigate.video import capture_camera, track_camera
|
|
|
|
from frigate.watchdog import FrigateWatchdog
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
|
|
|
class FrigateApp:
|
2022-04-16 17:40:04 +02:00
|
|
|
def __init__(self) -> None:
|
2022-11-19 14:16:33 +01:00
|
|
|
self.stop_event: MpEvent = mp.Event()
|
2022-04-16 17:40:04 +02:00
|
|
|
self.detection_queue: Queue = mp.Queue()
|
2022-11-04 03:23:09 +01:00
|
|
|
self.detectors: dict[str, ObjectDetectProcess] = {}
|
2022-11-19 14:16:33 +01:00
|
|
|
self.detection_out_events: dict[str, MpEvent] = {}
|
2022-04-16 17:40:04 +02:00
|
|
|
self.detection_shms: list[mp.shared_memory.SharedMemory] = []
|
|
|
|
self.log_queue: Queue = mp.Queue()
|
|
|
|
self.plus_api = PlusApi()
|
|
|
|
self.camera_metrics: dict[str, CameraMetricsTypes] = {}
|
2023-07-01 15:18:33 +02:00
|
|
|
self.feature_metrics: dict[str, FeatureMetricsTypes] = {}
|
2023-05-05 00:58:59 +02:00
|
|
|
self.processes: dict[str, int] = {}
|
2022-04-16 17:40:04 +02:00
|
|
|
|
|
|
|
def set_environment_vars(self) -> None:
|
2021-01-16 04:33:53 +01:00
|
|
|
for key, value in self.config.environment_vars.items():
|
|
|
|
os.environ[key] = value
|
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def ensure_dirs(self) -> None:
|
2023-06-08 13:32:35 +02:00
|
|
|
for d in [
|
|
|
|
CONFIG_DIR,
|
|
|
|
RECORD_DIR,
|
|
|
|
CLIPS_DIR,
|
|
|
|
CACHE_DIR,
|
|
|
|
MODEL_CACHE_DIR,
|
|
|
|
EXPORT_DIR,
|
|
|
|
]:
|
2020-12-01 14:22:23 +01:00
|
|
|
if not os.path.exists(d) and not os.path.islink(d):
|
2020-12-03 15:01:22 +01:00
|
|
|
logger.info(f"Creating directory: {d}")
|
2020-12-01 14:22:23 +01:00
|
|
|
os.makedirs(d)
|
2020-12-03 15:01:22 +01:00
|
|
|
else:
|
|
|
|
logger.debug(f"Skipping directory: {d}")
|
2020-12-21 14:37:42 +01:00
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def init_logger(self) -> None:
|
2021-02-17 14:23:32 +01:00
|
|
|
self.log_process = mp.Process(
|
|
|
|
target=log_process, args=(self.log_queue,), name="log_process"
|
|
|
|
)
|
2020-12-05 18:14:18 +01:00
|
|
|
self.log_process.daemon = True
|
2020-11-04 13:28:07 +01:00
|
|
|
self.log_process.start()
|
2023-05-05 00:58:59 +02:00
|
|
|
self.processes["logger"] = self.log_process.pid or 0
|
2020-11-04 13:28:07 +01:00
|
|
|
root_configurer(self.log_queue)
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def init_config(self) -> None:
|
2021-02-17 14:23:32 +01:00
|
|
|
config_file = os.environ.get("CONFIG_FILE", "/config/config.yml")
|
2021-12-12 16:27:05 +01:00
|
|
|
|
|
|
|
# Check if we can use .yaml instead of .yml
|
|
|
|
config_file_yaml = config_file.replace(".yml", ".yaml")
|
|
|
|
if os.path.isfile(config_file_yaml):
|
|
|
|
config_file = config_file_yaml
|
|
|
|
|
2021-06-24 07:45:27 +02:00
|
|
|
user_config = FrigateConfig.parse_file(config_file)
|
2023-04-30 20:32:36 +02:00
|
|
|
self.config = user_config.runtime_config(self.plus_api)
|
2020-11-04 13:28:07 +01:00
|
|
|
|
2023-07-01 15:18:33 +02:00
|
|
|
for camera_name, camera_config in self.config.cameras.items():
|
2020-11-04 13:28:07 +01:00
|
|
|
# create camera_metrics
|
|
|
|
self.camera_metrics[camera_name] = {
|
2023-07-01 14:47:16 +02:00
|
|
|
"camera_fps": mp.Value("d", 0.0), # type: ignore[typeddict-item]
|
|
|
|
# issue https://github.com/python/typeshed/issues/8799
|
|
|
|
# from mypy 0.981 onwards
|
|
|
|
"skipped_fps": mp.Value("d", 0.0), # type: ignore[typeddict-item]
|
|
|
|
# issue https://github.com/python/typeshed/issues/8799
|
|
|
|
# from mypy 0.981 onwards
|
|
|
|
"process_fps": mp.Value("d", 0.0), # type: ignore[typeddict-item]
|
|
|
|
# issue https://github.com/python/typeshed/issues/8799
|
|
|
|
# from mypy 0.981 onwards
|
|
|
|
"detection_enabled": mp.Value( # type: ignore[typeddict-item]
|
|
|
|
# issue https://github.com/python/typeshed/issues/8799
|
|
|
|
# from mypy 0.981 onwards
|
|
|
|
"i",
|
|
|
|
self.config.cameras[camera_name].detect.enabled,
|
2021-02-17 14:23:32 +01:00
|
|
|
),
|
2023-07-01 14:47:16 +02:00
|
|
|
"motion_enabled": mp.Value("i", True), # type: ignore[typeddict-item]
|
|
|
|
# issue https://github.com/python/typeshed/issues/8799
|
|
|
|
# from mypy 0.981 onwards
|
|
|
|
"improve_contrast_enabled": mp.Value( # type: ignore[typeddict-item]
|
|
|
|
# issue https://github.com/python/typeshed/issues/8799
|
|
|
|
# from mypy 0.981 onwards
|
|
|
|
"i",
|
|
|
|
self.config.cameras[camera_name].motion.improve_contrast,
|
2022-04-16 15:42:44 +02:00
|
|
|
),
|
2023-07-01 14:47:16 +02:00
|
|
|
"motion_threshold": mp.Value( # type: ignore[typeddict-item]
|
|
|
|
# issue https://github.com/python/typeshed/issues/8799
|
|
|
|
# from mypy 0.981 onwards
|
|
|
|
"i",
|
|
|
|
self.config.cameras[camera_name].motion.threshold,
|
2022-04-27 16:52:45 +02:00
|
|
|
),
|
2023-07-01 14:47:16 +02:00
|
|
|
"motion_contour_area": mp.Value( # type: ignore[typeddict-item]
|
|
|
|
# issue https://github.com/python/typeshed/issues/8799
|
|
|
|
# from mypy 0.981 onwards
|
|
|
|
"i",
|
|
|
|
self.config.cameras[camera_name].motion.contour_area,
|
2022-04-27 16:52:45 +02:00
|
|
|
),
|
2023-07-01 14:47:16 +02:00
|
|
|
"detection_fps": mp.Value("d", 0.0), # type: ignore[typeddict-item]
|
|
|
|
# issue https://github.com/python/typeshed/issues/8799
|
|
|
|
# from mypy 0.981 onwards
|
|
|
|
"detection_frame": mp.Value("d", 0.0), # type: ignore[typeddict-item]
|
|
|
|
# issue https://github.com/python/typeshed/issues/8799
|
|
|
|
# from mypy 0.981 onwards
|
|
|
|
"read_start": mp.Value("d", 0.0), # type: ignore[typeddict-item]
|
|
|
|
# issue https://github.com/python/typeshed/issues/8799
|
|
|
|
# from mypy 0.981 onwards
|
|
|
|
"ffmpeg_pid": mp.Value("i", 0), # type: ignore[typeddict-item]
|
|
|
|
# issue https://github.com/python/typeshed/issues/8799
|
|
|
|
# from mypy 0.981 onwards
|
2021-02-17 14:23:32 +01:00
|
|
|
"frame_queue": mp.Queue(maxsize=2),
|
2022-04-16 17:40:04 +02:00
|
|
|
"capture_process": None,
|
|
|
|
"process": None,
|
2020-11-04 13:28:07 +01:00
|
|
|
}
|
2023-07-01 15:18:33 +02:00
|
|
|
self.feature_metrics[camera_name] = {
|
|
|
|
"audio_enabled": mp.Value( # type: ignore[typeddict-item]
|
|
|
|
# issue https://github.com/python/typeshed/issues/8799
|
|
|
|
# from mypy 0.981 onwards
|
|
|
|
"i",
|
|
|
|
self.config.cameras[camera_name].audio.enabled,
|
|
|
|
),
|
2023-07-01 14:47:16 +02:00
|
|
|
"record_enabled": mp.Value( # type: ignore[typeddict-item]
|
|
|
|
# issue https://github.com/python/typeshed/issues/8799
|
|
|
|
# from mypy 0.981 onwards
|
|
|
|
"i",
|
|
|
|
self.config.cameras[camera_name].record.enabled,
|
2023-07-01 15:18:33 +02:00
|
|
|
),
|
2023-04-26 15:25:26 +02:00
|
|
|
}
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def set_log_levels(self) -> None:
|
2021-06-24 07:45:27 +02:00
|
|
|
logging.getLogger().setLevel(self.config.logger.default.value.upper())
|
2020-12-04 13:59:03 +01:00
|
|
|
for log, level in self.config.logger.logs.items():
|
2021-06-24 07:45:27 +02:00
|
|
|
logging.getLogger(log).setLevel(level.value.upper())
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2023-05-29 12:31:17 +02:00
|
|
|
if "werkzeug" not in self.config.logger.logs:
|
2021-06-14 14:31:13 +02:00
|
|
|
logging.getLogger("werkzeug").setLevel("ERROR")
|
2020-11-04 13:28:07 +01:00
|
|
|
|
2023-05-29 12:31:17 +02:00
|
|
|
if "ws4py" not in self.config.logger.logs:
|
2023-01-31 00:42:53 +01:00
|
|
|
logging.getLogger("ws4py").setLevel("ERROR")
|
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def init_queues(self) -> None:
|
2020-11-25 17:37:41 +01:00
|
|
|
# Queues for clip processing
|
2022-04-16 17:40:04 +02:00
|
|
|
self.event_queue: Queue = mp.Queue()
|
|
|
|
self.event_processed_queue: Queue = mp.Queue()
|
|
|
|
self.video_output_queue: Queue = mp.Queue(
|
|
|
|
maxsize=len(self.config.cameras.keys()) * 2
|
|
|
|
)
|
2020-11-04 13:28:07 +01:00
|
|
|
|
|
|
|
# Queue for cameras to push tracked objects to
|
2022-04-16 17:40:04 +02:00
|
|
|
self.detected_frames_queue: Queue = mp.Queue(
|
2021-02-17 14:23:32 +01:00
|
|
|
maxsize=len(self.config.cameras.keys()) * 2
|
|
|
|
)
|
2020-11-04 13:28:07 +01:00
|
|
|
|
2021-12-11 05:56:29 +01:00
|
|
|
# Queue for recordings info
|
2022-04-16 17:40:04 +02:00
|
|
|
self.recordings_info_queue: Queue = mp.Queue()
|
2021-12-11 05:56:29 +01:00
|
|
|
|
2023-04-23 17:45:19 +02:00
|
|
|
# Queue for timeline events
|
|
|
|
self.timeline_queue: Queue = mp.Queue()
|
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def init_database(self) -> None:
|
2023-06-11 14:23:18 +02:00
|
|
|
def vacuum_db(db: SqliteExtDatabase) -> None:
|
|
|
|
db.execute_sql("VACUUM;")
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(f"{CONFIG_DIR}/.vacuum", "w") as f:
|
|
|
|
f.write(str(datetime.datetime.now().timestamp()))
|
|
|
|
except PermissionError:
|
|
|
|
logger.error("Unable to write to /config to save DB state")
|
|
|
|
|
2021-06-07 03:24:36 +02:00
|
|
|
# Migrate DB location
|
2023-04-23 18:35:40 +02:00
|
|
|
old_db_path = DEFAULT_DB_PATH
|
2021-06-07 03:24:36 +02:00
|
|
|
if not os.path.isfile(self.config.database.path) and os.path.isfile(
|
|
|
|
old_db_path
|
|
|
|
):
|
|
|
|
os.rename(old_db_path, self.config.database.path)
|
|
|
|
|
|
|
|
# Migrate DB schema
|
2021-01-24 13:53:01 +01:00
|
|
|
migrate_db = SqliteExtDatabase(self.config.database.path)
|
2020-12-24 14:47:27 +01:00
|
|
|
|
|
|
|
# Run migrations
|
2021-02-17 14:23:32 +01:00
|
|
|
del logging.getLogger("peewee_migrate").handlers[:]
|
2021-01-24 13:53:01 +01:00
|
|
|
router = Router(migrate_db)
|
2020-12-24 14:47:27 +01:00
|
|
|
router.run()
|
|
|
|
|
2023-06-11 14:23:18 +02:00
|
|
|
# check if vacuum needs to be run
|
|
|
|
if os.path.exists(f"{CONFIG_DIR}/.vacuum"):
|
|
|
|
with open(f"{CONFIG_DIR}/.vacuum") as f:
|
|
|
|
try:
|
2023-06-15 00:19:26 +02:00
|
|
|
timestamp = round(float(f.readline()))
|
2023-06-11 14:23:18 +02:00
|
|
|
except Exception:
|
|
|
|
timestamp = 0
|
|
|
|
|
|
|
|
if (
|
|
|
|
timestamp
|
|
|
|
< (
|
|
|
|
datetime.datetime.now() - datetime.timedelta(weeks=2)
|
|
|
|
).timestamp()
|
|
|
|
):
|
|
|
|
vacuum_db(migrate_db)
|
|
|
|
else:
|
|
|
|
vacuum_db(migrate_db)
|
|
|
|
|
2021-01-24 13:53:01 +01:00
|
|
|
migrate_db.close()
|
|
|
|
|
2023-05-05 00:58:59 +02:00
|
|
|
def init_go2rtc(self) -> None:
|
|
|
|
for proc in psutil.process_iter(["pid", "name"]):
|
|
|
|
if proc.info["name"] == "go2rtc":
|
|
|
|
logger.info(f"go2rtc process pid: {proc.info['pid']}")
|
|
|
|
self.processes["go2rtc"] = proc.info["pid"]
|
|
|
|
|
2023-04-26 15:25:26 +02:00
|
|
|
def init_recording_manager(self) -> None:
|
|
|
|
recording_process = mp.Process(
|
|
|
|
target=manage_recordings,
|
|
|
|
name="recording_manager",
|
2023-07-01 15:18:33 +02:00
|
|
|
args=(self.config, self.recordings_info_queue, self.feature_metrics),
|
2023-04-26 15:25:26 +02:00
|
|
|
)
|
|
|
|
recording_process.daemon = True
|
|
|
|
self.recording_process = recording_process
|
|
|
|
recording_process.start()
|
2023-05-05 00:58:59 +02:00
|
|
|
self.processes["recording"] = recording_process.pid or 0
|
2023-04-26 15:25:26 +02:00
|
|
|
logger.info(f"Recording process started: {recording_process.pid}")
|
|
|
|
|
|
|
|
def bind_database(self) -> None:
|
|
|
|
"""Bind db to the main process."""
|
|
|
|
# NOTE: all db accessing processes need to be created before the db can be bound to the main process
|
2023-06-11 14:23:18 +02:00
|
|
|
self.db = SqliteQueueDatabase(
|
|
|
|
self.config.database.path,
|
|
|
|
pragmas={
|
|
|
|
"auto_vacuum": "FULL", # Does not defragment database
|
|
|
|
"cache_size": -512 * 1000, # 512MB of cache,
|
|
|
|
"synchronous": "NORMAL", # Safe when using WAL https://www.sqlite.org/pragma.html#pragma_synchronous
|
|
|
|
},
|
|
|
|
timeout=60,
|
|
|
|
)
|
2023-04-23 17:45:19 +02:00
|
|
|
models = [Event, Recordings, Timeline]
|
2020-11-04 13:28:07 +01:00
|
|
|
self.db.bind(models)
|
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def init_stats(self) -> None:
|
2023-01-26 01:36:26 +01:00
|
|
|
self.stats_tracking = stats_init(
|
2023-05-05 00:58:59 +02:00
|
|
|
self.config, self.camera_metrics, self.detectors, self.processes
|
2023-01-26 01:36:26 +01:00
|
|
|
)
|
2021-01-04 00:35:58 +01:00
|
|
|
|
2023-05-19 12:16:11 +02:00
|
|
|
def init_external_event_processor(self) -> None:
|
|
|
|
self.external_event_processor = ExternalEventProcessor(
|
|
|
|
self.config, self.event_queue
|
|
|
|
)
|
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def init_web_server(self) -> None:
|
2021-02-17 14:23:32 +01:00
|
|
|
self.flask_app = create_app(
|
|
|
|
self.config,
|
|
|
|
self.db,
|
|
|
|
self.stats_tracking,
|
|
|
|
self.detected_frames_processor,
|
2022-11-30 02:59:56 +01:00
|
|
|
self.storage_maintainer,
|
2023-04-26 13:08:53 +02:00
|
|
|
self.onvif_controller,
|
2023-05-19 12:16:11 +02:00
|
|
|
self.external_event_processor,
|
2022-04-03 22:00:11 +02:00
|
|
|
self.plus_api,
|
2021-02-17 14:23:32 +01:00
|
|
|
)
|
2020-11-04 13:28:07 +01:00
|
|
|
|
2023-04-26 13:08:53 +02:00
|
|
|
def init_onvif(self) -> None:
|
|
|
|
self.onvif_controller = OnvifController(self.config)
|
|
|
|
|
2022-11-24 03:03:20 +01:00
|
|
|
def init_dispatcher(self) -> None:
|
|
|
|
comms: list[Communicator] = []
|
2020-11-04 13:28:07 +01:00
|
|
|
|
2022-11-24 03:03:20 +01:00
|
|
|
if self.config.mqtt.enabled:
|
|
|
|
comms.append(MqttClient(self.config))
|
|
|
|
|
2023-02-04 03:15:47 +01:00
|
|
|
comms.append(WebSocketClient(self.config))
|
2023-04-26 13:08:53 +02:00
|
|
|
self.dispatcher = Dispatcher(
|
2023-04-26 15:25:26 +02:00
|
|
|
self.config,
|
|
|
|
self.onvif_controller,
|
|
|
|
self.camera_metrics,
|
2023-07-01 15:18:33 +02:00
|
|
|
self.feature_metrics,
|
2023-04-26 15:25:26 +02:00
|
|
|
comms,
|
2023-04-26 13:08:53 +02:00
|
|
|
)
|
2021-06-14 14:31:13 +02:00
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def start_detectors(self) -> None:
|
2020-11-04 13:28:07 +01:00
|
|
|
for name in self.config.cameras.keys():
|
|
|
|
self.detection_out_events[name] = mp.Event()
|
2021-06-19 14:15:02 +02:00
|
|
|
|
|
|
|
try:
|
2022-12-15 14:12:52 +01:00
|
|
|
largest_frame = max(
|
|
|
|
[
|
|
|
|
det.model.height * det.model.width * 3
|
|
|
|
for (name, det) in self.config.detectors.items()
|
|
|
|
]
|
|
|
|
)
|
2021-06-19 14:15:02 +02:00
|
|
|
shm_in = mp.shared_memory.SharedMemory(
|
2021-06-24 07:45:27 +02:00
|
|
|
name=name,
|
|
|
|
create=True,
|
2022-12-15 14:12:52 +01:00
|
|
|
size=largest_frame,
|
2021-06-19 14:15:02 +02:00
|
|
|
)
|
|
|
|
except FileExistsError:
|
|
|
|
shm_in = mp.shared_memory.SharedMemory(name=name)
|
|
|
|
|
|
|
|
try:
|
|
|
|
shm_out = mp.shared_memory.SharedMemory(
|
|
|
|
name=f"out-{name}", create=True, size=20 * 6 * 4
|
|
|
|
)
|
|
|
|
except FileExistsError:
|
|
|
|
shm_out = mp.shared_memory.SharedMemory(name=f"out-{name}")
|
|
|
|
|
2020-11-04 13:28:07 +01:00
|
|
|
self.detection_shms.append(shm_in)
|
|
|
|
self.detection_shms.append(shm_out)
|
|
|
|
|
2022-12-15 14:12:52 +01:00
|
|
|
for name, detector_config in self.config.detectors.items():
|
2022-11-04 03:23:09 +01:00
|
|
|
self.detectors[name] = ObjectDetectProcess(
|
|
|
|
name,
|
|
|
|
self.detection_queue,
|
|
|
|
self.detection_out_events,
|
2022-12-15 14:12:52 +01:00
|
|
|
detector_config,
|
2022-11-04 03:23:09 +01:00
|
|
|
)
|
2020-11-04 13:28:07 +01:00
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def start_detected_frames_processor(self) -> None:
|
2021-02-17 14:23:32 +01:00
|
|
|
self.detected_frames_processor = TrackedObjectProcessor(
|
|
|
|
self.config,
|
2022-11-24 03:03:20 +01:00
|
|
|
self.dispatcher,
|
2021-02-17 14:23:32 +01:00
|
|
|
self.detected_frames_queue,
|
|
|
|
self.event_queue,
|
|
|
|
self.event_processed_queue,
|
2021-05-29 20:27:00 +02:00
|
|
|
self.video_output_queue,
|
2021-12-11 05:56:29 +01:00
|
|
|
self.recordings_info_queue,
|
2021-02-17 14:23:32 +01:00
|
|
|
self.stop_event,
|
|
|
|
)
|
2020-11-04 13:28:07 +01:00
|
|
|
self.detected_frames_processor.start()
|
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def start_video_output_processor(self) -> None:
|
2021-05-29 20:27:00 +02:00
|
|
|
output_processor = mp.Process(
|
|
|
|
target=output_frames,
|
2023-05-29 12:31:17 +02:00
|
|
|
name="output_processor",
|
2021-05-29 20:27:00 +02:00
|
|
|
args=(
|
|
|
|
self.config,
|
|
|
|
self.video_output_queue,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
output_processor.daemon = True
|
|
|
|
self.output_processor = output_processor
|
|
|
|
output_processor.start()
|
2021-05-30 13:45:37 +02:00
|
|
|
logger.info(f"Output process started: {output_processor.pid}")
|
2021-05-29 20:27:00 +02:00
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def start_camera_processors(self) -> None:
|
2020-11-04 13:28:07 +01:00
|
|
|
for name, config in self.config.cameras.items():
|
2022-11-02 12:41:44 +01:00
|
|
|
if not self.config.cameras[name].enabled:
|
|
|
|
logger.info(f"Camera processor not started for disabled camera {name}")
|
|
|
|
continue
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
camera_process = mp.Process(
|
|
|
|
target=track_camera,
|
|
|
|
name=f"camera_processor:{name}",
|
|
|
|
args=(
|
|
|
|
name,
|
|
|
|
config,
|
2022-11-04 03:23:09 +01:00
|
|
|
self.config.model,
|
2021-07-08 05:57:19 +02:00
|
|
|
self.config.model.merged_labelmap,
|
2021-02-17 14:23:32 +01:00
|
|
|
self.detection_queue,
|
|
|
|
self.detection_out_events[name],
|
|
|
|
self.detected_frames_queue,
|
|
|
|
self.camera_metrics[name],
|
|
|
|
),
|
|
|
|
)
|
2020-11-04 13:28:07 +01:00
|
|
|
camera_process.daemon = True
|
2021-02-17 14:23:32 +01:00
|
|
|
self.camera_metrics[name]["process"] = camera_process
|
2020-11-04 13:28:07 +01:00
|
|
|
camera_process.start()
|
|
|
|
logger.info(f"Camera processor started for {name}: {camera_process.pid}")
|
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def start_camera_capture_processes(self) -> None:
|
2020-11-04 13:28:07 +01:00
|
|
|
for name, config in self.config.cameras.items():
|
2022-11-02 12:41:44 +01:00
|
|
|
if not self.config.cameras[name].enabled:
|
|
|
|
logger.info(f"Capture process not started for disabled camera {name}")
|
|
|
|
continue
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
capture_process = mp.Process(
|
|
|
|
target=capture_camera,
|
|
|
|
name=f"camera_capture:{name}",
|
|
|
|
args=(name, config, self.camera_metrics[name]),
|
|
|
|
)
|
2020-11-04 13:28:07 +01:00
|
|
|
capture_process.daemon = True
|
2021-02-17 14:23:32 +01:00
|
|
|
self.camera_metrics[name]["capture_process"] = capture_process
|
2020-11-04 13:28:07 +01:00
|
|
|
capture_process.start()
|
|
|
|
logger.info(f"Capture process started for {name}: {capture_process.pid}")
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2023-07-01 15:18:33 +02:00
|
|
|
def start_audio_processors(self) -> None:
|
|
|
|
if len([c for c in self.config.cameras.values() if c.audio.enabled]) > 0:
|
|
|
|
audio_process = mp.Process(
|
|
|
|
target=listen_to_audio,
|
|
|
|
name="audio_capture",
|
|
|
|
args=(self.config, self.feature_metrics),
|
|
|
|
)
|
|
|
|
audio_process.daemon = True
|
|
|
|
audio_process.start()
|
2023-07-03 16:48:29 +02:00
|
|
|
self.processes["audioDetector"] = audio_process.pid or 0
|
2023-07-01 15:18:33 +02:00
|
|
|
logger.info(f"Audio process started: {audio_process.pid}")
|
|
|
|
|
2023-04-23 17:45:19 +02:00
|
|
|
def start_timeline_processor(self) -> None:
|
|
|
|
self.timeline_processor = TimelineProcessor(
|
|
|
|
self.config, self.timeline_queue, self.stop_event
|
|
|
|
)
|
|
|
|
self.timeline_processor.start()
|
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def start_event_processor(self) -> None:
|
2021-02-17 14:23:32 +01:00
|
|
|
self.event_processor = EventProcessor(
|
|
|
|
self.config,
|
|
|
|
self.camera_metrics,
|
|
|
|
self.event_queue,
|
|
|
|
self.event_processed_queue,
|
2023-04-23 17:45:19 +02:00
|
|
|
self.timeline_queue,
|
2021-02-17 14:23:32 +01:00
|
|
|
self.stop_event,
|
|
|
|
)
|
2020-11-04 13:28:07 +01:00
|
|
|
self.event_processor.start()
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def start_event_cleanup(self) -> None:
|
2020-11-24 14:27:51 +01:00
|
|
|
self.event_cleanup = EventCleanup(self.config, self.stop_event)
|
|
|
|
self.event_cleanup.start()
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2022-10-09 13:28:26 +02:00
|
|
|
def start_storage_maintainer(self) -> None:
|
|
|
|
self.storage_maintainer = StorageMaintainer(self.config, self.stop_event)
|
|
|
|
self.storage_maintainer.start()
|
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def start_stats_emitter(self) -> None:
|
2021-02-17 14:23:32 +01:00
|
|
|
self.stats_emitter = StatsEmitter(
|
|
|
|
self.config,
|
|
|
|
self.stats_tracking,
|
2022-11-24 03:03:20 +01:00
|
|
|
self.dispatcher,
|
2021-02-17 14:23:32 +01:00
|
|
|
self.stop_event,
|
|
|
|
)
|
2021-01-04 00:35:58 +01:00
|
|
|
self.stats_emitter.start()
|
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def start_watchdog(self) -> None:
|
2020-11-04 13:28:07 +01:00
|
|
|
self.frigate_watchdog = FrigateWatchdog(self.detectors, self.stop_event)
|
|
|
|
self.frigate_watchdog.start()
|
|
|
|
|
2023-01-31 00:42:23 +01:00
|
|
|
def check_shm(self) -> None:
|
2023-06-11 21:49:13 +02:00
|
|
|
available_shm = round(shutil.disk_usage("/dev/shm").total / pow(2, 20), 1)
|
2023-01-31 00:42:23 +01:00
|
|
|
min_req_shm = 30
|
|
|
|
|
|
|
|
for _, camera in self.config.cameras.items():
|
|
|
|
min_req_shm += round(
|
|
|
|
(camera.detect.width * camera.detect.height * 1.5 * 9 + 270480)
|
|
|
|
/ 1048576,
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
|
|
|
|
if available_shm < min_req_shm:
|
|
|
|
logger.warning(
|
|
|
|
f"The current SHM size of {available_shm}MB is too small, recommend increasing it to at least {min_req_shm}MB."
|
|
|
|
)
|
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def start(self) -> None:
|
2020-11-04 13:28:07 +01:00
|
|
|
self.init_logger()
|
2021-09-14 05:02:23 +02:00
|
|
|
logger.info(f"Starting Frigate ({VERSION})")
|
2020-12-03 15:01:03 +01:00
|
|
|
try:
|
2023-04-30 20:32:36 +02:00
|
|
|
self.ensure_dirs()
|
2020-12-05 16:47:43 +01:00
|
|
|
try:
|
|
|
|
self.init_config()
|
|
|
|
except Exception as e:
|
2021-10-16 14:17:36 +02:00
|
|
|
print("*************************************************************")
|
|
|
|
print("*************************************************************")
|
|
|
|
print("*** Your config file is not valid! ***")
|
|
|
|
print("*** Please check the docs at ***")
|
|
|
|
print("*** https://docs.frigate.video/configuration/index ***")
|
|
|
|
print("*************************************************************")
|
|
|
|
print("*************************************************************")
|
|
|
|
print("*** Config Validation Errors ***")
|
|
|
|
print("*************************************************************")
|
|
|
|
print(e)
|
2022-02-06 15:40:24 +01:00
|
|
|
print(traceback.format_exc())
|
2021-10-16 14:17:36 +02:00
|
|
|
print("*************************************************************")
|
|
|
|
print("*** End Config Validation Errors ***")
|
|
|
|
print("*************************************************************")
|
2020-12-05 16:47:43 +01:00
|
|
|
self.log_process.terminate()
|
|
|
|
sys.exit(1)
|
2021-01-16 04:33:53 +01:00
|
|
|
self.set_environment_vars()
|
2020-12-05 16:47:43 +01:00
|
|
|
self.set_log_levels()
|
|
|
|
self.init_queues()
|
|
|
|
self.init_database()
|
2023-04-26 13:08:53 +02:00
|
|
|
self.init_onvif()
|
2023-04-26 15:25:26 +02:00
|
|
|
self.init_recording_manager()
|
2023-05-05 00:58:59 +02:00
|
|
|
self.init_go2rtc()
|
2023-04-26 15:25:26 +02:00
|
|
|
self.bind_database()
|
2022-11-24 03:03:20 +01:00
|
|
|
self.init_dispatcher()
|
2020-12-03 15:01:03 +01:00
|
|
|
except Exception as e:
|
2020-12-20 15:00:07 +01:00
|
|
|
print(e)
|
2020-12-03 15:01:03 +01:00
|
|
|
self.log_process.terminate()
|
|
|
|
sys.exit(1)
|
2020-11-04 13:28:07 +01:00
|
|
|
self.start_detectors()
|
2021-05-29 20:27:00 +02:00
|
|
|
self.start_video_output_processor()
|
2020-11-04 13:28:07 +01:00
|
|
|
self.start_detected_frames_processor()
|
|
|
|
self.start_camera_processors()
|
|
|
|
self.start_camera_capture_processes()
|
2023-07-01 15:18:33 +02:00
|
|
|
self.start_audio_processors()
|
2022-11-30 02:59:56 +01:00
|
|
|
self.start_storage_maintainer()
|
2021-01-04 00:35:58 +01:00
|
|
|
self.init_stats()
|
2023-05-19 12:16:11 +02:00
|
|
|
self.init_external_event_processor()
|
2020-11-04 13:28:07 +01:00
|
|
|
self.init_web_server()
|
2023-04-23 17:45:19 +02:00
|
|
|
self.start_timeline_processor()
|
2020-11-04 13:28:07 +01:00
|
|
|
self.start_event_processor()
|
2020-11-24 14:27:51 +01:00
|
|
|
self.start_event_cleanup()
|
2021-01-04 00:35:58 +01:00
|
|
|
self.start_stats_emitter()
|
2020-11-04 13:28:07 +01:00
|
|
|
self.start_watchdog()
|
2023-01-31 00:42:23 +01:00
|
|
|
self.check_shm()
|
2020-12-05 18:14:18 +01:00
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def receiveSignal(signalNumber: int, frame: Optional[FrameType]) -> None:
|
2020-12-05 18:14:18 +01:00
|
|
|
self.stop()
|
|
|
|
sys.exit()
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2020-12-05 18:14:18 +01:00
|
|
|
signal.signal(signal.SIGTERM, receiveSignal)
|
|
|
|
|
2021-05-18 07:52:08 +02:00
|
|
|
try:
|
2021-06-14 14:31:13 +02:00
|
|
|
self.flask_app.run(host="127.0.0.1", port=5001, debug=False)
|
2021-05-18 07:52:08 +02:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
2021-02-13 15:09:44 +01:00
|
|
|
|
2020-11-04 13:28:07 +01:00
|
|
|
self.stop()
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def stop(self) -> None:
|
2023-05-29 12:31:17 +02:00
|
|
|
logger.info("Stopping...")
|
2020-11-04 13:28:07 +01:00
|
|
|
self.stop_event.set()
|
|
|
|
|
2023-02-04 15:58:45 +01:00
|
|
|
for detector in self.detectors.values():
|
|
|
|
detector.stop()
|
|
|
|
|
|
|
|
# Empty the detection queue and set the events for all requests
|
|
|
|
while not self.detection_queue.empty():
|
|
|
|
connection_id = self.detection_queue.get(timeout=1)
|
|
|
|
self.detection_out_events[connection_id].set()
|
|
|
|
self.detection_queue.close()
|
|
|
|
self.detection_queue.join_thread()
|
2023-02-04 03:15:47 +01:00
|
|
|
|
|
|
|
self.dispatcher.stop()
|
2020-11-04 13:28:07 +01:00
|
|
|
self.detected_frames_processor.join()
|
|
|
|
self.event_processor.join()
|
2020-11-24 14:27:51 +01:00
|
|
|
self.event_cleanup.join()
|
2021-01-04 00:35:58 +01:00
|
|
|
self.stats_emitter.join()
|
2020-11-04 13:28:07 +01:00
|
|
|
self.frigate_watchdog.join()
|
2021-02-07 15:38:35 +01:00
|
|
|
self.db.stop()
|
2020-11-04 13:28:07 +01:00
|
|
|
|
|
|
|
while len(self.detection_shms) > 0:
|
|
|
|
shm = self.detection_shms.pop()
|
|
|
|
shm.close()
|
|
|
|
shm.unlink()
|
2023-02-04 03:15:47 +01:00
|
|
|
|
|
|
|
for queue in [
|
|
|
|
self.event_queue,
|
|
|
|
self.event_processed_queue,
|
|
|
|
self.video_output_queue,
|
|
|
|
self.detected_frames_queue,
|
|
|
|
self.recordings_info_queue,
|
2023-02-04 15:58:45 +01:00
|
|
|
self.log_queue,
|
2023-02-04 03:15:47 +01:00
|
|
|
]:
|
|
|
|
while not queue.empty():
|
|
|
|
queue.get_nowait()
|
|
|
|
queue.close()
|
|
|
|
queue.join_thread()
|