Skip to content

Commit

Permalink
simplify config changes further
Browse files Browse the repository at this point in the history
  • Loading branch information
dennispg committed Dec 10, 2022
1 parent 03e6c8a commit 490717c
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 96 deletions.
2 changes: 1 addition & 1 deletion benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import multiprocessing as mp
import numpy as np
import datetime
from frigate.detectors import DetectorTypeEnum
from frigate.config import DetectorTypeEnum
from frigate.object_detection import (
LocalObjectDetector,
ObjectDetectProcess,
Expand Down
71 changes: 68 additions & 3 deletions frigate/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
get_ffmpeg_arg_list,
escape_special_characters,
load_config_with_no_duplicates,
load_labels,
)
from frigate.ffmpeg_presets import (
parse_preset_hardware_acceleration,
parse_preset_input,
parse_preset_output_record,
parse_preset_output_rtmp,
)
from frigate.detectors import DetectorTypeEnum
from frigate.version import VERSION
from frigate.detectors.config import ModelConfig, DetectorConfig


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -714,6 +715,70 @@ class DatabaseConfig(FrigateBaseModel):
)


class PixelFormatEnum(str, Enum):
rgb = "rgb"
bgr = "bgr"
yuv = "yuv"


class InputTensorEnum(str, Enum):
nchw = "nchw"
nhwc = "nhwc"


class ModelConfig(FrigateBaseModel):
path: Optional[str] = Field(title="Custom Object detection model path.")
labelmap_path: Optional[str] = Field(title="Label map for custom object detector.")
width: int = Field(default=320, title="Object detection model input width.")
height: int = Field(default=320, title="Object detection model input height.")
labelmap: Dict[int, str] = Field(
default_factory=dict, title="Labelmap customization."
)
input_tensor: InputTensorEnum = Field(
default=InputTensorEnum.nhwc, title="Model Input Tensor Shape"
)
input_pixel_format: PixelFormatEnum = Field(
default=PixelFormatEnum.rgb, title="Model Input Pixel Color Format"
)
_merged_labelmap: Optional[Dict[int, str]] = PrivateAttr()
_colormap: Dict[int, Tuple[int, int, int]] = PrivateAttr()

@property
def merged_labelmap(self) -> Dict[int, str]:
return self._merged_labelmap

@property
def colormap(self) -> Dict[int, Tuple[int, int, int]]:
return self._colormap

def __init__(self, **config):
super().__init__(**config)

self._merged_labelmap = {
**load_labels(config.get("labelmap_path", "/labelmap.txt")),
**config.get("labelmap", {}),
}

cmap = plt.cm.get_cmap("tab10", len(self._merged_labelmap.keys()))

self._colormap = {}
for key, val in self._merged_labelmap.items():
self._colormap[val] = tuple(int(round(255 * c)) for c in cmap(key)[:3])


class DetectorConfig(BaseModel):
type: str = Field(default=DetectorTypeEnum.cpu, title="Detector Type")
device: Optional[str] = Field(default="usb", title="Device Type")
num_threads: Optional[int] = Field(default=3, title="Number of detection threads")
model: ModelConfig = Field(
default=None, title="Detector specific model configuration."
)

class Config:
extra = Extra.allow
arbitrary_types_allowed = True


class LogLevelEnum(str, Enum):
debug = "debug"
info = "info"
Expand Down Expand Up @@ -825,10 +890,10 @@ class FrigateConfig(FrigateBaseModel):
)
ui: UIConfig = Field(default_factory=UIConfig, title="UI configuration.")
model: ModelConfig = Field(
default_factory=ModelConfig, title="Default detection model configuration."
default_factory=ModelConfig, title="Detection model configuration."
)
detectors: Dict[str, DetectorConfig] = Field(
default=DEFAULT_DETECTORS,
default={name: DetectorConfig(**d) for name, d in DEFAULT_DETECTORS.items()},
title="Detector hardware configuration.",
)
logger: LoggerConfig = Field(
Expand Down
3 changes: 1 addition & 2 deletions frigate/detectors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

from .detection_api import DetectionApi
from .detector_types import DetectorTypeEnum, api_types
from .config import ModelConfig, DetectorConfig


logger = logging.getLogger(__name__)


def create_detector(detector_config: DetectorConfig):
def create_detector(detector_config):
if detector_config.type == DetectorTypeEnum.cpu:
logger.warning(
"CPU detectors are not recommended and should only be used for testing or for trial purposes."
Expand Down
73 changes: 0 additions & 73 deletions frigate/detectors/config.py

This file was deleted.

12 changes: 0 additions & 12 deletions frigate/enums.py

This file was deleted.

2 changes: 1 addition & 1 deletion frigate/object_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import numpy as np
from setproctitle import setproctitle

from frigate.enums import InputTensorEnum
from frigate.config import InputTensorEnum
from frigate.detectors import create_detector

from frigate.util import EventsPerSecond, SharedMemoryFrameManager, listen, load_labels
Expand Down
3 changes: 1 addition & 2 deletions frigate/test/test_object_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

import numpy as np

from frigate.enums import InputTensorEnum
from frigate.config import DetectorConfig, InputTensorEnum, ModelConfig
from frigate.detectors import DetectorTypeEnum
from frigate.detectors.config import DetectorConfig, ModelConfig
import frigate.detectors as detectors
import frigate.object_detection

Expand Down
3 changes: 1 addition & 2 deletions frigate/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
import cv2
from setproctitle import setproctitle

from frigate.config import CameraConfig, DetectConfig
from frigate.config import CameraConfig, DetectConfig, PixelFormatEnum
from frigate.const import CACHE_DIR
from frigate.enums import PixelFormatEnum
from frigate.object_detection import RemoteObjectDetector
from frigate.log import LogPipe
from frigate.motion import MotionDetector
Expand Down

0 comments on commit 490717c

Please sign in to comment.