| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- """Mock 检测器:无模型也能跑通全链路(联调 PLC、验证状态机、跑回归测试)。
- 脚本用 **归一化坐标**(0..1)描述,``infer`` 时按实际帧尺寸展开,因此与分辨率无关。
- 默认脚本按"车斗 → 平板车 → 只有箱(不在车上)→ 空场景"循环,方便现场演示三种反馈值。
- """
- from __future__ import annotations
- from typing import Dict, List, Optional, Sequence, Tuple
- import numpy as np
- from ..config import Config
- from ..schema import Box
- from .base import Detector
- __all__ = ["MockDetector", "build_demo_script"]
- #: 每个剧本元素:(类别名, 归一化 x1, y1, x2, y2, 分数)
- ScriptFrame = List[Tuple[str, float, float, float, float, float]]
- def build_demo_script() -> List[ScriptFrame]:
- """默认演示剧本,覆盖 1 / 2 / 0 / UNKNOWN 四种情况。"""
- return [
- # 车斗:集装箱坐在带斗车厢上
- [
- ("container", 0.30, 0.28, 0.72, 0.62, 0.91),
- ("bed", 0.27, 0.62, 0.75, 0.88, 0.86),
- ],
- # 平板车:集装箱坐在平板车上
- [
- ("container", 0.32, 0.30, 0.70, 0.60, 0.89),
- ("flatbed", 0.28, 0.60, 0.74, 0.84, 0.83),
- ],
- # 只有集装箱(被吊起/在卡车上)→ 0
- [
- ("container", 0.34, 0.22, 0.68, 0.50, 0.88),
- ],
- # 空场景 → UNKNOWN(可配置成 0)
- [],
- ]
- class MockDetector(Detector):
- """按剧本循环输出检测框,用于自检与演示。"""
- name = "mock"
- def __init__(self, cfg: Config, script: Optional[Sequence[ScriptFrame]] = None) -> None:
- super().__init__(cfg)
- self.script: List[ScriptFrame] = [list(frame) for frame in (script or build_demo_script())]
- if not self.script:
- self.script = [[]]
- self._cursor = 0
- def infer(self, frame: np.ndarray) -> List[Box]:
- height, width = frame.shape[:2]
- raw = self.script[self._cursor % len(self.script)]
- self._cursor += 1
- boxes: List[Box] = []
- for name, x1, y1, x2, y2, score in raw:
- class_id = self.labels.index(name) if name in self.labels else -1
- boxes.append(
- Box(
- x1 * width,
- y1 * height,
- x2 * width,
- y2 * height,
- score=float(score),
- class_id=class_id,
- class_name=str(name),
- )
- )
- return boxes
- def reset(self) -> None:
- self._cursor = 0
- def describe(self) -> Dict[str, object]:
- info = super().describe()
- info["script_frames"] = len(self.script)
- return info
|