File size: 1,684 Bytes
af70f62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Optional
from core.config_manager import ConfigManager
from core.state import FrameContext


class BaseModelWrapper(ABC):
    """Uniform adapter contract for HailoRT (.hef), ONNX (.onnx), MediaPipe (.task), or PC mocks."""

    @abstractmethod
    def load(self) -> None:
        """Load pre-cached offline model weights into NPU/CPU memory."""

    @abstractmethod
    def process(self, context: FrameContext) -> FrameContext:
        """Run inference and enrich FrameContext in-place."""

    def close(self) -> None:
        """Release hardware/NPU handles cleanly on shutdown."""


class PipelineModule(ABC):
    """Plug-and-play Lego brick governed by ConfigManager Zero-Deletion Toggling."""

    def __init__(

        self,

        name: str,

        config: ConfigManager,

        branch: Optional[str] = None,

        wrapper: Optional[BaseModelWrapper] = None,

    ) -> None:
        self.name = name
        self.config = config
        self.branch = branch
        self.wrapper = wrapper

    def initialize(self) -> None:
        if self.wrapper is not None:
            self.wrapper.load()

    def execute(self, context: FrameContext) -> FrameContext:
        if not self.config.is_active(self.name, self.branch):
            return context
        return self.run(context)

    @abstractmethod
    def run(self, context: FrameContext) -> FrameContext:
        """Module-specific business logic operating on FrameContext."""

    def shutdown(self) -> None:
        if self.wrapper is not None:
            self.wrapper.close()