config.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. from __future__ import annotations
  2. import json
  3. import logging
  4. import os
  5. from enum import Enum
  6. from typing import Dict, List, Optional, Tuple, Union
  7. import matplotlib.pyplot as plt
  8. import numpy as np
  9. import yaml
  10. from pydantic import BaseModel, Extra, Field, validator
  11. from pydantic.fields import PrivateAttr
  12. from frigate.const import BASE_DIR, CACHE_DIR, RECORD_DIR
  13. from frigate.edgetpu import load_labels
  14. from frigate.util import create_mask, deep_merge
  15. logger = logging.getLogger(__name__)
  16. # TODO: Identify what the default format to display timestamps is
  17. DEFAULT_TIME_FORMAT = "%m/%d/%Y %H:%M:%S"
  18. # German Style:
  19. # DEFAULT_TIME_FORMAT = "%d.%m.%Y %H:%M:%S"
  20. FRIGATE_ENV_VARS = {k: v for k, v in os.environ.items() if k.startswith("FRIGATE_")}
  21. DEFAULT_TRACKED_OBJECTS = ["person"]
  22. DEFAULT_DETECTORS = {"cpu": {"type": "cpu"}}
  23. class FrigateBaseModel(BaseModel):
  24. class Config:
  25. extra = Extra.forbid
  26. class DetectorTypeEnum(str, Enum):
  27. edgetpu = "edgetpu"
  28. cpu = "cpu"
  29. class DetectorConfig(FrigateBaseModel):
  30. type: DetectorTypeEnum = Field(default=DetectorTypeEnum.cpu, title="Detector Type")
  31. device: str = Field(default="usb", title="Device Type")
  32. num_threads: int = Field(default=3, title="Number of detection threads")
  33. class MqttConfig(FrigateBaseModel):
  34. host: str = Field(title="MQTT Host")
  35. port: int = Field(default=1883, title="MQTT Port")
  36. topic_prefix: str = Field(default="frigate", title="MQTT Topic Prefix")
  37. client_id: str = Field(default="frigate", title="MQTT Client ID")
  38. stats_interval: int = Field(default=60, title="MQTT Camera Stats Interval")
  39. user: Optional[str] = Field(title="MQTT Username")
  40. password: Optional[str] = Field(title="MQTT Password")
  41. tls_ca_certs: Optional[str] = Field(title="MQTT TLS CA Certificates")
  42. tls_client_cert: Optional[str] = Field(title="MQTT TLS Client Certificate")
  43. tls_client_key: Optional[str] = Field(title="MQTT TLS Client Key")
  44. tls_insecure: Optional[bool] = Field(title="MQTT TLS Insecure")
  45. @validator("password", pre=True, always=True)
  46. def validate_password(cls, v, values):
  47. if (v is None) != (values["user"] is None):
  48. raise ValueError("Password must be provided with username.")
  49. return v
  50. class RetainConfig(FrigateBaseModel):
  51. default: float = Field(default=10, title="Default retention period.")
  52. objects: Dict[str, float] = Field(
  53. default_factory=dict, title="Object retention period."
  54. )
  55. class EventsConfig(FrigateBaseModel):
  56. max_seconds: int = Field(default=300, title="Maximum event duration.")
  57. pre_capture: int = Field(default=5, title="Seconds to retain before event starts.")
  58. post_capture: int = Field(default=5, title="Seconds to retain after event ends.")
  59. required_zones: List[str] = Field(
  60. default_factory=list,
  61. title="List of required zones to be entered in order to save the event.",
  62. )
  63. objects: Optional[List[str]] = Field(
  64. title="List of objects to be detected in order to save the event.",
  65. )
  66. retain: RetainConfig = Field(
  67. default_factory=RetainConfig, title="Event retention settings."
  68. )
  69. class RecordConfig(FrigateBaseModel):
  70. enabled: bool = Field(default=False, title="Enable record on all cameras.")
  71. retain_days: float = Field(default=0, title="Recording retention period in days.")
  72. events: EventsConfig = Field(
  73. default_factory=EventsConfig, title="Event specific settings."
  74. )
  75. class MotionConfig(FrigateBaseModel):
  76. threshold: int = Field(
  77. default=25,
  78. title="Motion detection threshold (1-255).",
  79. ge=1,
  80. le=255,
  81. )
  82. contour_area: Optional[int] = Field(title="Contour Area")
  83. delta_alpha: float = Field(default=0.2, title="Delta Alpha")
  84. frame_alpha: float = Field(default=0.2, title="Frame Alpha")
  85. frame_height: Optional[int] = Field(title="Frame Height")
  86. mask: Union[str, List[str]] = Field(
  87. default="", title="Coordinates polygon for the motion mask."
  88. )
  89. class RuntimeMotionConfig(MotionConfig):
  90. raw_mask: Union[str, List[str]] = ""
  91. mask: np.ndarray = None
  92. def __init__(self, **config):
  93. frame_shape = config.get("frame_shape", (1, 1))
  94. if "frame_height" not in config:
  95. config["frame_height"] = max(frame_shape[0] // 6, 180)
  96. if "contour_area" not in config:
  97. frame_width = frame_shape[1] * config["frame_height"] / frame_shape[0]
  98. config["contour_area"] = (
  99. config["frame_height"] * frame_width * 0.00173611111
  100. )
  101. mask = config.get("mask", "")
  102. config["raw_mask"] = mask
  103. if mask:
  104. config["mask"] = create_mask(frame_shape, mask)
  105. else:
  106. empty_mask = np.zeros(frame_shape, np.uint8)
  107. empty_mask[:] = 255
  108. config["mask"] = empty_mask
  109. super().__init__(**config)
  110. def dict(self, **kwargs):
  111. ret = super().dict(**kwargs)
  112. if "mask" in ret:
  113. ret["mask"] = ret["raw_mask"]
  114. ret.pop("raw_mask")
  115. return ret
  116. class Config:
  117. arbitrary_types_allowed = True
  118. extra = Extra.ignore
  119. class DetectConfig(FrigateBaseModel):
  120. height: int = Field(default=720, title="Height of the stream for the detect role.")
  121. width: int = Field(default=1280, title="Width of the stream for the detect role.")
  122. fps: int = Field(
  123. default=5, title="Number of frames per second to process through detection."
  124. )
  125. enabled: bool = Field(default=True, title="Detection Enabled.")
  126. max_disappeared: Optional[int] = Field(
  127. title="Maximum number of frames the object can dissapear before detection ends."
  128. )
  129. class FilterConfig(FrigateBaseModel):
  130. min_area: int = Field(
  131. default=0, title="Minimum area of bounding box for object to be counted."
  132. )
  133. max_area: int = Field(
  134. default=24000000, title="Maximum area of bounding box for object to be counted."
  135. )
  136. threshold: float = Field(
  137. default=0.7,
  138. title="Average detection confidence threshold for object to be counted.",
  139. )
  140. min_score: float = Field(
  141. default=0.5, title="Minimum detection confidence for object to be counted."
  142. )
  143. mask: Optional[Union[str, List[str]]] = Field(
  144. title="Detection area polygon mask for this filter configuration.",
  145. )
  146. class RuntimeFilterConfig(FilterConfig):
  147. mask: Optional[np.ndarray]
  148. raw_mask: Optional[Union[str, List[str]]]
  149. def __init__(self, **config):
  150. mask = config.get("mask")
  151. config["raw_mask"] = mask
  152. if mask is not None:
  153. config["mask"] = create_mask(config.get("frame_shape", (1, 1)), mask)
  154. super().__init__(**config)
  155. def dict(self, **kwargs):
  156. ret = super().dict(**kwargs)
  157. if "mask" in ret:
  158. ret["mask"] = ret["raw_mask"]
  159. ret.pop("raw_mask")
  160. return ret
  161. class Config:
  162. arbitrary_types_allowed = True
  163. extra = Extra.ignore
  164. # this uses the base model because the color is an extra attribute
  165. class ZoneConfig(BaseModel):
  166. filters: Dict[str, FilterConfig] = Field(
  167. default_factory=dict, title="Zone filters."
  168. )
  169. coordinates: Union[str, List[str]] = Field(
  170. title="Coordinates polygon for the defined zone."
  171. )
  172. objects: List[str] = Field(
  173. default_factory=list,
  174. title="List of objects that can trigger the zone.",
  175. )
  176. _color: Optional[Tuple[int, int, int]] = PrivateAttr()
  177. _contour: np.ndarray = PrivateAttr()
  178. @property
  179. def color(self) -> Tuple[int, int, int]:
  180. return self._color
  181. @property
  182. def contour(self) -> np.ndarray:
  183. return self._contour
  184. def __init__(self, **config):
  185. super().__init__(**config)
  186. self._color = config.get("color", (0, 0, 0))
  187. coordinates = config["coordinates"]
  188. if isinstance(coordinates, list):
  189. self._contour = np.array(
  190. [[int(p.split(",")[0]), int(p.split(",")[1])] for p in coordinates]
  191. )
  192. elif isinstance(coordinates, str):
  193. points = coordinates.split(",")
  194. self._contour = np.array(
  195. [[int(points[i]), int(points[i + 1])] for i in range(0, len(points), 2)]
  196. )
  197. else:
  198. self._contour = np.array([])
  199. class ObjectConfig(FrigateBaseModel):
  200. track: List[str] = Field(default=DEFAULT_TRACKED_OBJECTS, title="Objects to track.")
  201. filters: Optional[Dict[str, FilterConfig]] = Field(title="Object filters.")
  202. mask: Union[str, List[str]] = Field(default="", title="Object mask.")
  203. class BirdseyeModeEnum(str, Enum):
  204. objects = "objects"
  205. motion = "motion"
  206. continuous = "continuous"
  207. class BirdseyeConfig(FrigateBaseModel):
  208. enabled: bool = Field(default=True, title="Enable birdseye view.")
  209. width: int = Field(default=1280, title="Birdseye width.")
  210. height: int = Field(default=720, title="Birdseye height.")
  211. quality: int = Field(
  212. default=8,
  213. title="Encoding quality.",
  214. ge=1,
  215. le=31,
  216. )
  217. mode: BirdseyeModeEnum = Field(
  218. default=BirdseyeModeEnum.objects, title="Tracking mode."
  219. )
  220. FFMPEG_GLOBAL_ARGS_DEFAULT = ["-hide_banner", "-loglevel", "warning"]
  221. FFMPEG_INPUT_ARGS_DEFAULT = [
  222. "-avoid_negative_ts",
  223. "make_zero",
  224. "-fflags",
  225. "+genpts+discardcorrupt",
  226. "-rtsp_transport",
  227. "tcp",
  228. "-stimeout",
  229. "5000000",
  230. "-use_wallclock_as_timestamps",
  231. "1",
  232. ]
  233. DETECT_FFMPEG_OUTPUT_ARGS_DEFAULT = ["-f", "rawvideo", "-pix_fmt", "yuv420p"]
  234. RTMP_FFMPEG_OUTPUT_ARGS_DEFAULT = ["-c", "copy", "-f", "flv"]
  235. RECORD_FFMPEG_OUTPUT_ARGS_DEFAULT = [
  236. "-f",
  237. "segment",
  238. "-segment_time",
  239. "10",
  240. "-segment_format",
  241. "ts",
  242. "-reset_timestamps",
  243. "1",
  244. "-strftime",
  245. "1",
  246. "-c",
  247. "copy",
  248. ]
  249. class FfmpegOutputArgsConfig(FrigateBaseModel):
  250. detect: Union[str, List[str]] = Field(
  251. default=DETECT_FFMPEG_OUTPUT_ARGS_DEFAULT,
  252. title="Detect role FFmpeg output arguments.",
  253. )
  254. record: Union[str, List[str]] = Field(
  255. default=RECORD_FFMPEG_OUTPUT_ARGS_DEFAULT,
  256. title="Record role FFmpeg output arguments.",
  257. )
  258. rtmp: Union[str, List[str]] = Field(
  259. default=RTMP_FFMPEG_OUTPUT_ARGS_DEFAULT,
  260. title="RTMP role FFmpeg output arguments.",
  261. )
  262. class FfmpegConfig(FrigateBaseModel):
  263. global_args: Union[str, List[str]] = Field(
  264. default=FFMPEG_GLOBAL_ARGS_DEFAULT, title="Global FFmpeg arguments."
  265. )
  266. hwaccel_args: Union[str, List[str]] = Field(
  267. default_factory=list, title="FFmpeg hardware acceleration arguments."
  268. )
  269. input_args: Union[str, List[str]] = Field(
  270. default=FFMPEG_INPUT_ARGS_DEFAULT, title="FFmpeg input arguments."
  271. )
  272. output_args: FfmpegOutputArgsConfig = Field(
  273. default_factory=FfmpegOutputArgsConfig,
  274. title="FFmpeg output arguments per role.",
  275. )
  276. class CameraRoleEnum(str, Enum):
  277. record = "record"
  278. rtmp = "rtmp"
  279. detect = "detect"
  280. class CameraInput(FrigateBaseModel):
  281. path: str = Field(title="Camera input path.")
  282. roles: List[CameraRoleEnum] = Field(title="Roles assigned to this input.")
  283. global_args: Union[str, List[str]] = Field(
  284. default_factory=list, title="FFmpeg global arguments."
  285. )
  286. hwaccel_args: Union[str, List[str]] = Field(
  287. default_factory=list, title="FFmpeg hardware acceleration arguments."
  288. )
  289. input_args: Union[str, List[str]] = Field(
  290. default_factory=list, title="FFmpeg input arguments."
  291. )
  292. class CameraFfmpegConfig(FfmpegConfig):
  293. inputs: List[CameraInput] = Field(title="Camera inputs.")
  294. @validator("inputs")
  295. def validate_roles(cls, v):
  296. roles = [role for i in v for role in i.roles]
  297. roles_set = set(roles)
  298. if len(roles) > len(roles_set):
  299. raise ValueError("Each input role may only be used once.")
  300. if not "detect" in roles:
  301. raise ValueError("The detect role is required.")
  302. return v
  303. class SnapshotsConfig(FrigateBaseModel):
  304. enabled: bool = Field(default=False, title="Snapshots enabled.")
  305. clean_copy: bool = Field(
  306. default=True, title="Create a clean copy of the snapshot image."
  307. )
  308. timestamp: bool = Field(
  309. default=False, title="Add a timestamp overlay on the snapshot."
  310. )
  311. bounding_box: bool = Field(
  312. default=True, title="Add a bounding box overlay on the snapshot."
  313. )
  314. crop: bool = Field(default=False, title="Crop the snapshot to the detected object.")
  315. required_zones: List[str] = Field(
  316. default_factory=list,
  317. title="List of required zones to be entered in order to save a snapshot.",
  318. )
  319. height: Optional[int] = Field(title="Snapshot image height.")
  320. retain: RetainConfig = Field(
  321. default_factory=RetainConfig, title="Snapshot retention."
  322. )
  323. quality: int = Field(
  324. default=70,
  325. title="Quality of the encoded jpeg (0-100).",
  326. ge=0,
  327. le=100,
  328. )
  329. class ColorConfig(FrigateBaseModel):
  330. red: int = Field(default=255, ge=0, le=255, title="Red")
  331. green: int = Field(default=255, ge=0, le=255, title="Green")
  332. blue: int = Field(default=255, ge=0, le=255, title="Blue")
  333. class TimestampPositionEnum(str, Enum):
  334. tl = "tl"
  335. tr = "tr"
  336. bl = "bl"
  337. br = "br"
  338. class TimestampEffectEnum(str, Enum):
  339. solid = "solid"
  340. shadow = "shadow"
  341. class TimestampStyleConfig(FrigateBaseModel):
  342. position: TimestampPositionEnum = Field(
  343. default=TimestampPositionEnum.tl, title="Timestamp position."
  344. )
  345. format: str = Field(default=DEFAULT_TIME_FORMAT, title="Timestamp format.")
  346. color: ColorConfig = Field(default_factory=ColorConfig, title="Timestamp color.")
  347. thickness: int = Field(default=2, title="Timestamp thickness.")
  348. effect: Optional[TimestampEffectEnum] = Field(title="Timestamp effect.")
  349. class CameraMqttConfig(FrigateBaseModel):
  350. enabled: bool = Field(default=True, title="Send image over MQTT.")
  351. timestamp: bool = Field(default=True, title="Add timestamp to MQTT image.")
  352. bounding_box: bool = Field(default=True, title="Add bounding box to MQTT image.")
  353. crop: bool = Field(default=True, title="Crop MQTT image to detected object.")
  354. height: int = Field(default=270, title="MQTT image height.")
  355. required_zones: List[str] = Field(
  356. default_factory=list,
  357. title="List of required zones to be entered in order to send the image.",
  358. )
  359. quality: int = Field(
  360. default=70,
  361. title="Quality of the encoded jpeg (0-100).",
  362. ge=0,
  363. le=100,
  364. )
  365. class RtmpConfig(FrigateBaseModel):
  366. enabled: bool = Field(default=True, title="RTMP restreaming enabled.")
  367. class CameraLiveConfig(FrigateBaseModel):
  368. height: int = Field(default=720, title="Live camera view height")
  369. quality: int = Field(default=8, ge=1, le=31, title="Live camera view quality")
  370. class CameraConfig(FrigateBaseModel):
  371. name: Optional[str] = Field(title="Camera name.")
  372. ffmpeg: CameraFfmpegConfig = Field(title="FFmpeg configuration for the camera.")
  373. best_image_timeout: int = Field(
  374. default=60,
  375. title="How long to wait for the image with the highest confidence score.",
  376. )
  377. zones: Dict[str, ZoneConfig] = Field(
  378. default_factory=dict, title="Zone configuration."
  379. )
  380. record: RecordConfig = Field(
  381. default_factory=RecordConfig, title="Record configuration."
  382. )
  383. rtmp: RtmpConfig = Field(
  384. default_factory=RtmpConfig, title="RTMP restreaming configuration."
  385. )
  386. live: CameraLiveConfig = Field(
  387. default_factory=CameraLiveConfig, title="Live playback settings."
  388. )
  389. snapshots: SnapshotsConfig = Field(
  390. default_factory=SnapshotsConfig, title="Snapshot configuration."
  391. )
  392. mqtt: CameraMqttConfig = Field(
  393. default_factory=CameraMqttConfig, title="MQTT configuration."
  394. )
  395. objects: ObjectConfig = Field(
  396. default_factory=ObjectConfig, title="Object configuration."
  397. )
  398. motion: Optional[MotionConfig] = Field(title="Motion detection configuration.")
  399. detect: DetectConfig = Field(
  400. default_factory=DetectConfig, title="Object detection configuration."
  401. )
  402. timestamp_style: TimestampStyleConfig = Field(
  403. default_factory=TimestampStyleConfig, title="Timestamp style configuration."
  404. )
  405. def __init__(self, **config):
  406. # Set zone colors
  407. if "zones" in config:
  408. colors = plt.cm.get_cmap("tab10", len(config["zones"]))
  409. config["zones"] = {
  410. name: {**z, "color": tuple(round(255 * c) for c in colors(idx)[:3])}
  411. for idx, (name, z) in enumerate(config["zones"].items())
  412. }
  413. super().__init__(**config)
  414. @property
  415. def frame_shape(self) -> Tuple[int, int]:
  416. return self.detect.height, self.detect.width
  417. @property
  418. def frame_shape_yuv(self) -> Tuple[int, int]:
  419. return self.detect.height * 3 // 2, self.detect.width
  420. @property
  421. def ffmpeg_cmds(self) -> List[Dict[str, List[str]]]:
  422. ffmpeg_cmds = []
  423. for ffmpeg_input in self.ffmpeg.inputs:
  424. ffmpeg_cmd = self._get_ffmpeg_cmd(ffmpeg_input)
  425. if ffmpeg_cmd is None:
  426. continue
  427. ffmpeg_cmds.append({"roles": ffmpeg_input.roles, "cmd": ffmpeg_cmd})
  428. return ffmpeg_cmds
  429. def _get_ffmpeg_cmd(self, ffmpeg_input: CameraInput):
  430. ffmpeg_output_args = []
  431. if "detect" in ffmpeg_input.roles:
  432. detect_args = (
  433. self.ffmpeg.output_args.detect
  434. if isinstance(self.ffmpeg.output_args.detect, list)
  435. else self.ffmpeg.output_args.detect.split(" ")
  436. )
  437. ffmpeg_output_args = (
  438. [
  439. "-r",
  440. str(self.detect.fps),
  441. "-s",
  442. f"{self.detect.width}x{self.detect.height}",
  443. ]
  444. + detect_args
  445. + ffmpeg_output_args
  446. + ["pipe:"]
  447. )
  448. if "rtmp" in ffmpeg_input.roles and self.rtmp.enabled:
  449. rtmp_args = (
  450. self.ffmpeg.output_args.rtmp
  451. if isinstance(self.ffmpeg.output_args.rtmp, list)
  452. else self.ffmpeg.output_args.rtmp.split(" ")
  453. )
  454. ffmpeg_output_args = (
  455. rtmp_args + [f"rtmp://127.0.0.1/live/{self.name}"] + ffmpeg_output_args
  456. )
  457. if "record" in ffmpeg_input.roles and self.record.enabled:
  458. record_args = (
  459. self.ffmpeg.output_args.record
  460. if isinstance(self.ffmpeg.output_args.record, list)
  461. else self.ffmpeg.output_args.record.split(" ")
  462. )
  463. ffmpeg_output_args = (
  464. record_args
  465. + [f"{os.path.join(CACHE_DIR, self.name)}-%Y%m%d%H%M%S.ts"]
  466. + ffmpeg_output_args
  467. )
  468. # if there arent any outputs enabled for this input
  469. if len(ffmpeg_output_args) == 0:
  470. return None
  471. global_args = ffmpeg_input.global_args or self.ffmpeg.global_args
  472. hwaccel_args = ffmpeg_input.hwaccel_args or self.ffmpeg.hwaccel_args
  473. input_args = ffmpeg_input.input_args or self.ffmpeg.input_args
  474. global_args = (
  475. global_args if isinstance(global_args, list) else global_args.split(" ")
  476. )
  477. hwaccel_args = (
  478. hwaccel_args if isinstance(hwaccel_args, list) else hwaccel_args.split(" ")
  479. )
  480. input_args = (
  481. input_args if isinstance(input_args, list) else input_args.split(" ")
  482. )
  483. cmd = (
  484. ["ffmpeg"]
  485. + global_args
  486. + hwaccel_args
  487. + input_args
  488. + ["-i", ffmpeg_input.path]
  489. + ffmpeg_output_args
  490. )
  491. return [part for part in cmd if part != ""]
  492. class DatabaseConfig(FrigateBaseModel):
  493. path: str = Field(
  494. default=os.path.join(BASE_DIR, "frigate.db"), title="Database path."
  495. )
  496. class ModelConfig(FrigateBaseModel):
  497. path: Optional[str] = Field(title="Custom Object detection model path.")
  498. labelmap_path: Optional[str] = Field(title="Label map for custom object detector.")
  499. width: int = Field(default=320, title="Object detection model input width.")
  500. height: int = Field(default=320, title="Object detection model input height.")
  501. labelmap: Dict[int, str] = Field(
  502. default_factory=dict, title="Labelmap customization."
  503. )
  504. _merged_labelmap: Optional[Dict[int, str]] = PrivateAttr()
  505. _colormap: Dict[int, Tuple[int, int, int]] = PrivateAttr()
  506. @property
  507. def merged_labelmap(self) -> Dict[int, str]:
  508. return self._merged_labelmap
  509. @property
  510. def colormap(self) -> Dict[int, tuple[int, int, int]]:
  511. return self._colormap
  512. def __init__(self, **config):
  513. super().__init__(**config)
  514. self._merged_labelmap = {
  515. **load_labels(config.get("labelmap_path", "/labelmap.txt")),
  516. **config.get("labelmap", {}),
  517. }
  518. cmap = plt.cm.get_cmap("tab10", len(self._merged_labelmap.keys()))
  519. self._colormap = {}
  520. for key, val in self._merged_labelmap.items():
  521. self._colormap[val] = tuple(int(round(255 * c)) for c in cmap(key)[:3])
  522. class LogLevelEnum(str, Enum):
  523. debug = "debug"
  524. info = "info"
  525. warning = "warning"
  526. error = "error"
  527. critical = "critical"
  528. class LoggerConfig(FrigateBaseModel):
  529. default: LogLevelEnum = Field(
  530. default=LogLevelEnum.info, title="Default logging level."
  531. )
  532. logs: Dict[str, LogLevelEnum] = Field(
  533. default_factory=dict, title="Log level for specified processes."
  534. )
  535. class FrigateConfig(FrigateBaseModel):
  536. mqtt: MqttConfig = Field(title="MQTT Configuration.")
  537. database: DatabaseConfig = Field(
  538. default_factory=DatabaseConfig, title="Database configuration."
  539. )
  540. environment_vars: Dict[str, str] = Field(
  541. default_factory=dict, title="Frigate environment variables."
  542. )
  543. model: ModelConfig = Field(
  544. default_factory=ModelConfig, title="Detection model configuration."
  545. )
  546. detectors: Dict[str, DetectorConfig] = Field(
  547. default={name: DetectorConfig(**d) for name, d in DEFAULT_DETECTORS.items()},
  548. title="Detector hardware configuration.",
  549. )
  550. logger: LoggerConfig = Field(
  551. default_factory=LoggerConfig, title="Logging configuration."
  552. )
  553. record: RecordConfig = Field(
  554. default_factory=RecordConfig, title="Global record configuration."
  555. )
  556. snapshots: SnapshotsConfig = Field(
  557. default_factory=SnapshotsConfig, title="Global snapshots configuration."
  558. )
  559. live: CameraLiveConfig = Field(
  560. default_factory=CameraLiveConfig, title="Global live configuration."
  561. )
  562. rtmp: RtmpConfig = Field(
  563. default_factory=RtmpConfig, title="Global RTMP restreaming configuration."
  564. )
  565. birdseye: BirdseyeConfig = Field(
  566. default_factory=BirdseyeConfig, title="Birdseye configuration."
  567. )
  568. ffmpeg: FfmpegConfig = Field(
  569. default_factory=FfmpegConfig, title="Global FFmpeg configuration."
  570. )
  571. objects: ObjectConfig = Field(
  572. default_factory=ObjectConfig, title="Global object configuration."
  573. )
  574. motion: Optional[MotionConfig] = Field(
  575. title="Global motion detection configuration."
  576. )
  577. detect: DetectConfig = Field(
  578. default_factory=DetectConfig, title="Global object tracking configuration."
  579. )
  580. cameras: Dict[str, CameraConfig] = Field(title="Camera configuration.")
  581. timestamp_style: TimestampStyleConfig = Field(
  582. default_factory=TimestampStyleConfig,
  583. title="Global timestamp style configuration.",
  584. )
  585. @property
  586. def runtime_config(self) -> FrigateConfig:
  587. """Merge camera config with globals."""
  588. config = self.copy(deep=True)
  589. # MQTT password substitution
  590. if config.mqtt.password:
  591. config.mqtt.password = config.mqtt.password.format(**FRIGATE_ENV_VARS)
  592. # Global config to propegate down to camera level
  593. global_config = config.dict(
  594. include={
  595. "record": ...,
  596. "snapshots": ...,
  597. "live": ...,
  598. "rtmp": ...,
  599. "objects": ...,
  600. "motion": ...,
  601. "detect": ...,
  602. "ffmpeg": ...,
  603. "timestamp_style": ...,
  604. },
  605. exclude_unset=True,
  606. )
  607. for name, camera in config.cameras.items():
  608. merged_config = deep_merge(camera.dict(exclude_unset=True), global_config)
  609. camera_config: CameraConfig = CameraConfig.parse_obj(
  610. {"name": name, **merged_config}
  611. )
  612. # Default max_disappeared configuration
  613. max_disappeared = camera_config.detect.fps * 5
  614. if camera_config.detect.max_disappeared is None:
  615. camera_config.detect.max_disappeared = max_disappeared
  616. # FFMPEG input substitution
  617. for input in camera_config.ffmpeg.inputs:
  618. input.path = input.path.format(**FRIGATE_ENV_VARS)
  619. # Add default filters
  620. object_keys = camera_config.objects.track
  621. if camera_config.objects.filters is None:
  622. camera_config.objects.filters = {}
  623. object_keys = object_keys - camera_config.objects.filters.keys()
  624. for key in object_keys:
  625. camera_config.objects.filters[key] = FilterConfig()
  626. # Apply global object masks and convert masks to numpy array
  627. for object, filter in camera_config.objects.filters.items():
  628. if camera_config.objects.mask:
  629. filter_mask = []
  630. if filter.mask is not None:
  631. filter_mask = (
  632. filter.mask
  633. if isinstance(filter.mask, list)
  634. else [filter.mask]
  635. )
  636. object_mask = (
  637. camera_config.objects.mask
  638. if isinstance(camera_config.objects.mask, list)
  639. else [camera_config.objects.mask]
  640. )
  641. filter.mask = filter_mask + object_mask
  642. # Set runtime filter to create masks
  643. camera_config.objects.filters[object] = RuntimeFilterConfig(
  644. frame_shape=camera_config.frame_shape,
  645. **filter.dict(exclude_unset=True),
  646. )
  647. # Convert motion configuration
  648. if camera_config.motion is None:
  649. camera_config.motion = RuntimeMotionConfig(
  650. frame_shape=camera_config.frame_shape
  651. )
  652. else:
  653. camera_config.motion = RuntimeMotionConfig(
  654. frame_shape=camera_config.frame_shape,
  655. raw_mask=camera_config.motion.mask,
  656. **camera_config.motion.dict(exclude_unset=True),
  657. )
  658. config.cameras[name] = camera_config
  659. return config
  660. @validator("cameras")
  661. def ensure_zones_and_cameras_have_different_names(cls, v: Dict[str, CameraConfig]):
  662. zones = [zone for camera in v.values() for zone in camera.zones.keys()]
  663. for zone in zones:
  664. if zone in v.keys():
  665. raise ValueError("Zones cannot share names with cameras")
  666. return v
  667. @classmethod
  668. def parse_file(cls, config_file):
  669. with open(config_file) as f:
  670. raw_config = f.read()
  671. if config_file.endswith(".yml"):
  672. config = yaml.safe_load(raw_config)
  673. elif config_file.endswith(".json"):
  674. config = json.loads(raw_config)
  675. return cls.parse_obj(config)