Files
frigate/frigate/__main__.py
T

137 lines
4.9 KiB
Python
Raw Normal View History

2024-09-24 15:07:47 +03:00
import argparse
2021-02-17 07:23:32 -06:00
import faulthandler
2025-06-12 12:12:34 -06:00
import multiprocessing as mp
2024-09-24 15:07:47 +03:00
import signal
import sys
2020-11-04 06:28:07 -06:00
import threading
from typing import Union
2020-11-04 06:31:25 -06:00
import ruamel.yaml
2024-09-24 15:07:47 +03:00
from pydantic import ValidationError
2020-11-01 08:06:15 -06:00
2024-06-23 09:13:02 -04:00
from frigate.app import FrigateApp
2024-09-24 15:07:47 +03:00
from frigate.config import FrigateConfig
from frigate.log import setup_logging
from frigate.util.config import find_config_file
2020-11-03 21:26:39 -06:00
2023-05-29 12:31:17 +02:00
2024-09-17 16:26:25 +03:00
def main() -> None:
2025-06-12 12:12:34 -06:00
manager = mp.Manager()
2024-09-17 16:26:25 +03:00
faulthandler.enable()
# Setup the logging thread
2025-06-12 12:12:34 -06:00
setup_logging(manager)
2024-09-17 16:26:25 +03:00
threading.current_thread().name = "frigate"
2025-06-24 11:41:11 -06:00
stop_event = mp.Event()
# send stop event on SIGINT
signal.signal(signal.SIGINT, lambda sig, frame: stop_event.set())
2024-09-17 16:26:25 +03:00
2024-09-24 15:07:47 +03:00
# Make sure we exit cleanly on SIGTERM.
signal.signal(signal.SIGTERM, lambda sig, frame: sys.exit())
# Parse the cli arguments.
parser = argparse.ArgumentParser(
prog="Frigate",
description="An NVR with realtime local object detection for IP cameras.",
)
parser.add_argument("--validate-config", action="store_true")
args = parser.parse_args()
# Load the configuration.
try:
config = FrigateConfig.load(install=True)
2024-09-24 15:07:47 +03:00
except ValidationError as e:
print("*************************************************************")
print("*************************************************************")
print("*** Your config file is not valid! ***")
print("*** Please check the docs at ***")
print("*** https://docs.frigate.video/configuration/ ***")
print("*************************************************************")
print("*************************************************************")
print("*** Config Validation Errors ***")
print("*************************************************************\n")
# Attempt to get the original config file for line number tracking
config_path = find_config_file()
with open(config_path, "r") as f:
yaml_config = ruamel.yaml.YAML()
yaml_config.preserve_quotes = True
full_config = yaml_config.load(f)
2024-09-24 15:07:47 +03:00
for error in e.errors():
error_path = error["loc"]
current = full_config
line_number = "Unknown"
last_line_number = "Unknown"
try:
for i, part in enumerate(error_path):
key: Union[int, str] = (
int(part) if isinstance(part, str) and part.isdigit() else part
)
if isinstance(current, ruamel.yaml.comments.CommentedMap):
current = current[key]
elif isinstance(current, list):
if isinstance(key, int):
current = current[key]
if hasattr(current, "lc"):
last_line_number = current.lc.line
if i == len(error_path) - 1:
if hasattr(current, "lc"):
line_number = current.lc.line
else:
line_number = last_line_number
except Exception as traverse_error:
print(f"Could not determine exact line number: {traverse_error}")
if current != full_config:
print(f"Line # : {line_number}")
print(f"Key : {' -> '.join(map(str, error_path))}")
2025-01-10 08:39:24 -07:00
print(f"Value : {error.get('input', '-')}")
print(f"Message : {error.get('msg', error.get('type', 'Unknown'))}\n")
2024-09-24 15:07:47 +03:00
print("*************************************************************")
print("*** End Config Validation Errors ***")
print("*************************************************************")
# attempt to start Frigate in recovery mode
try:
config = FrigateConfig.load(install=True, safe_load=True)
print("Starting Frigate in safe mode.")
except ValidationError:
print("Unable to start Frigate in safe mode.")
sys.exit(1)
2024-09-24 15:07:47 +03:00
if args.validate_config:
print("*************************************************************")
print("*** Your config file is valid. ***")
print("*************************************************************")
sys.exit(0)
2024-09-17 16:26:25 +03:00
# Run the main application.
2025-06-24 11:41:11 -06:00
FrigateApp(config, manager, stop_event).start()
2023-05-29 12:31:17 +02:00
2020-11-03 21:26:39 -06:00
2021-02-17 07:23:32 -06:00
if __name__ == "__main__":
2025-06-24 11:41:11 -06:00
mp.set_forkserver_preload(
[
# Standard library and core dependencies
"sqlite3",
# Third-party libraries commonly used in Frigate
"numpy",
"cv2",
"peewee",
"zmq",
"ruamel.yaml",
# Frigate core modules
"frigate.camera.maintainer",
]
)
2025-06-12 12:12:34 -06:00
mp.set_start_method("forkserver", force=True)
2024-09-17 16:26:25 +03:00
main()