Files
operating-room-monitor-server/backend/app/baked/algorithm.py
Kevin 1af442481e 重组为 backend/clients/docs 三层结构,并清理 git 污染。
将后端迁入 backend/,完善根目录 .gitignore,删除误提交的 .mypy_cache 缓存文件。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-21 16:02:25 +08:00

122 lines
5.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""YOLO 耗材/手部 + configured reference ActionFormer 实时段落检测默认配置。"""
from __future__ import annotations
from pathlib import Path
from app.algorithm_runner.reference_bundle_runtime import default_reference_bundle_dir
_PACKAGE_DIR = Path(__file__).resolve().parent.parent
def default_consumable_classifier_weights_path() -> str:
return str(_PACKAGE_DIR / "resources" / "consumable_classifier.pt")
def default_consumable_classifier_labels_yaml_path() -> str:
return str(_PACKAGE_DIR / "resources" / "consumable_classifier_labels.yaml")
def default_or_site_config_sample_path() -> str:
"""站点配置示例video_rtsp_urls + voice_or_room_bindings。"""
return str(_PACKAGE_DIR / "resources" / "or_site_config.sample.json")
def default_actionformer_hand_det_weights_path() -> str:
return str(default_reference_bundle_dir() / "weights" / "hand_detect.pt")
def default_actionformer_goodbad_weights_path() -> str:
return str(default_reference_bundle_dir() / "weights" / "goodbad_frame.pt")
def default_actionformer_haocai_weights_path() -> str:
return str(default_reference_bundle_dir() / "weights" / "haocai_classify.pt")
def default_actionformer_ckpt_path() -> str:
return str(default_reference_bundle_dir() / "weights" / "actionformer_epoch_045.pth.tar")
def default_actionformer_tear_weights_path() -> str:
return str(default_reference_bundle_dir() / "weights" / "tear_classify.pt")
# --- 耗材分类YOLO-cls---
CONSUMABLE_CLASSIFIER_WEIGHTS: str = default_consumable_classifier_weights_path()
CONSUMABLE_CLASSIFIER_IMGSZ: int = 224
CONSUMABLE_CLASSIFIER_DEVICE: str = ""
CONSUMABLE_CLASSIFIER_TOPK: int = 5
CONSUMABLE_MIN_CLS_CONFIDENCE: float = 0.5
CONSUMABLE_CLASSIFIER_LABELS_YAML_PATH: str = default_consumable_classifier_labels_yaml_path()
CONSUMABLE_VISION_WINDOW_SEC: float = 15.0
# --- 手部检测;全空路径则整帧分类 ---
HAND_DETECTION_WEIGHTS: str = ""
HAND_DETECTION_IMGSZ: int = 640
HAND_DETECTION_CONF: float = 0.25
HAND_DETECTION_PAD_RATIO: float = 0.30
HAND_DETECTION_MIN_CROP_PX: int = 64
HAND_DETECTION_DEVICE: str = ""
# --- refs/5.15 ActionFormer 实时版VideoSwin → ActionFormer + 手检/好坏帧/耗材投票)---
# 默认开启并与常见 demo 主摄 id 对齐;若缺少配置引用包 weights 下权重,开录时该路会记录异常。
ACTIONFORMER_ENABLED: bool = True
# 检查期:单路算法固定拉流 or-cam-03恢复按请求首路时请改回 "" 或按需配置)。
ACTIONFORMER_PRIMARY_CAMERA_ID: str = "or-cam-03"
# RTSP 帧率假设(用于 frame_idx → t_abs 推算与 ActionFormer fps 入参;线上需与摄像机一致)。
ACTIONFORMER_ASSUMED_FPS: float = 25.0
# 与 refs/5.15 ``run_haocai_actionformer_consumables_e2e`` 写出的 infer_single.yaml 同语义的 VideoSwin 参数:
# clip_len 32, frame_stride 2, feat_stride_frames 32 → feat_num_frames=64。
VIDEOSWIN_CLIP_LEN: int = 32
VIDEOSWIN_FRAME_STRIDE: int = 2
VIDEOSWIN_FEAT_STRIDE_FRAMES: int = 32
VIDEOSWIN_INPUT_DIM: int = 768
VIDEOSWIN_IMAGE_SIZE: int = 224
# 模型权重(默认读取配置引用包 weights文件名与 configs/default_config.yaml 对齐)
ACTIONFORMER_CKPT: str = default_actionformer_ckpt_path()
ACTIONFORMER_HAND_DET_WEIGHTS: str = default_actionformer_hand_det_weights_path()
ACTIONFORMER_GOODBAD_WEIGHTS: str = default_actionformer_goodbad_weights_path()
ACTIONFORMER_HAOCAI_WEIGHTS: str = default_actionformer_haocai_weights_path()
ACTIONFORMER_TEAR_WEIGHTS: str = default_actionformer_tear_weights_path()
ACTIONFORMER_NUM_CLASSES: int = 1 # 单类Action划段与 e2e infer_single.yaml 对齐
# 与 refs/5.15 e2e 默认对齐的运行时阈值
ACTIONFORMER_DET_CONF: float = 0.5
ACTIONFORMER_PAD_RATIO: float = 0.30
ACTIONFORMER_DET_IMGSZ: int = 640
ACTIONFORMER_GOODBAD_IMGSZ: int = 224
ACTIONFORMER_HAOCAI_IMGSZ: int = 224
ACTIONFORMER_GOOD_TOP1_CONF_THRESHOLD: float = 0.9
ACTIONFORMER_HAOCAI_MIN_CONF: float = 0.8
# ActionFormer 候选段 score 下限(不含等于);与 e2e ``--af-min-score`` 默认对齐
ACTIONFORMER_AF_MIN_SCORE: float = 0.1
# 候选耗材 → softmax mask按会话候选耗材索引集做 mask避免命中候选外类别
ACTIONFORMER_WHITELIST_SOFTMAX: bool = True
# 周期性 ActionFormer + 稳定段发布
# 周期间隔(墙钟秒):每过这么久才考虑跑一次 ActionFormer避免逐帧打 forward
ACTIONFORMER_PERIOD_SEC: float = 10.0
# 至少累积多少特征才有第一次有效推理32 帧/特征 @ 25 fps ≈ 1.28 秒/特征)
ACTIONFORMER_MIN_FEATURES_FOR_INFER: int = 8
# 段尾时间 + lookahead < now 才视为「冻结」,可以增量落库;越大越接近全量批处理。
ACTIONFORMER_INCREMENTAL_STABLE_FLUSH: bool = True
ACTIONFORMER_FREEZE_LOOKAHEAD_S: float = 12.0
# 新旧配置兼容;未显式使用时以 ACTIONFORMER_FREEZE_LOOKAHEAD_S 为准。
ACTIONFORMER_STABLE_FLUSH_MARGIN_S: float = ACTIONFORMER_FREEZE_LOOKAHEAD_S
# 已发布段去重:新段与已发布段的较短段覆盖率超过该阈值时视为同一段。
ACTIONFORMER_DEDUPE_OVERLAP_RATIO: float = 0.5
# 设备:空字符串走 resolve_inference_devicecuda 优先、mac 上再 mps、否则 CPU
ACTIONFORMER_VIDEOSWIN_DEVICE: str = ""
ACTIONFORMER_DEVICE: str = ""
ACTIONFORMER_HAND_DET_DEVICE: str = ""
ACTIONFORMER_GOODBAD_DEVICE: str = ""
ACTIONFORMER_HAOCAI_DEVICE: str = ""