"""全局配置:路径、模型、16 个检测项注册表、默认 ROI / 阈值。""" from __future__ import annotations import os from pathlib import Path BASE_DIR = Path(__file__).resolve().parent.parent MODELS_DIR = BASE_DIR / "models" ASSETS_DIR = BASE_DIR / "assets" STORAGE_DIR = Path(os.environ.get("DCCAI_STORAGE", BASE_DIR / "storage")) UPLOAD_DIR = STORAGE_DIR / "uploads" EVENT_DIR = STORAGE_DIR / "events" CACHE_DIR = STORAGE_DIR / "cache" FRONTEND_DIR = BASE_DIR / "frontend" for _d in (UPLOAD_DIR, EVENT_DIR, CACHE_DIR): _d.mkdir(parents=True, exist_ok=True) # ---------------------------------------------------------------- 模型配置 SAFETY_MODEL = os.environ.get("DCCAI_SAFETY_MODEL", str(MODELS_DIR / "yolo11m_safety.pt")) GENERIC_MODEL = os.environ.get("DCCAI_GENERIC_MODEL", str(MODELS_DIR / "yolo11n.pt")) # 可选扩展模型(缺失时对应检测项标记为「需模型」,不会导致启动失败) OPTIONAL_MODELS = { "shoe": str(MODELS_DIR / "shoe.pt"), # 安全鞋 "harness": str(MODELS_DIR / "harness.pt"), # 安全带 "plate": str(MODELS_DIR / "plate.pt"), # 车牌检测 } SAFETY_CLASSES = {0: "hat", 1: "nohat", 2: "novest", 3: "person", 4: "vest"} # COCO 中与车辆 / 物资相关的类别 VEHICLE_CLASSES = { "car": "小型车", "bus": "客车", "truck": "货车", "motorcycle": "摩托车", "bicycle": "非机动车", } MATERIAL_CLASSES = {"backpack": "背包", "suitcase": "行李箱", "handbag": "手提包"} DEVICE = os.environ.get("DCCAI_DEVICE", "auto") # auto | cpu | mps | cuda | cuda:0 IMG_SIZE = int(os.environ.get("DCCAI_IMGSIZE", "960")) # ---------------------------------------------------------------- 阈值 DEFAULT_THRESHOLDS = { "person_conf": 0.45, # 人 "ppe_conf": 0.50, # 安全帽 / 反光衣 "vehicle_conf": 0.40, # 车辆 "generic_conf": 0.35, # 通用目标 # 跌倒:人框宽高比 w/h 超过该值、且此前曾直立,并持续 fall_min_frames 帧 "fall_aspect_ratio": 1.35, "fall_upright_ratio": 0.95, "fall_window": 40, "fall_min_frames": 6, # 人群聚集:区域内人数超过阈值并持续 crowd_seconds 秒 "crowd_count": 4, "crowd_seconds": 3.0, # 物资异常移动:目标质心位移超过像素阈值 "move_pixels": 60, # 越线计数:滞回死区(像素)与同目标两次越线的最小间隔(秒) "line_margin": 25, "line_cooldown": 3.0, # 事件去抖:同类事件在该秒数内不重复触发 "event_cooldown": 12.0, # 已确认事件后,同类事件再次触发的静默期(同一目标) "track_event_cooldown": 30.0, } # ---------------------------------------------------------------- 检测大类 # need: 依赖的模型 key;None 表示仅靠规则/主模型即可 CATEGORY_GROUPS = [ { "id": "labor", "name": "劳动安全", "items": [ {"id": "fall", "name": "识别劳动人员跌倒事件", "need": None, "desc": "基于人体框宽高比 + 持续帧数的姿态判据"}, {"id": "no_helmet", "name": "识别工人未佩戴安全帽", "need": None, "desc": "安全模型 nohat 类别"}, {"id": "no_vest", "name": "识别工人未穿反光背心", "need": None, "desc": "安全模型 novest 类别"}, {"id": "no_shoe", "name": "识别工人未穿安全鞋", "need": "shoe", "desc": "需 models/shoe.pt 权重(当前无开源权重)"}, {"id": "no_harness", "name": "识别高处作业未系安全带", "need": "harness", "desc": "需 models/harness.pt + 高处 ROI(当前无开源权重)"}, {"id": "machinery_no_guard", "name": "识别工程机械区域未配备防护设备", "need": None, "desc": "机械 ROI 内出现未戴帽/未穿反光衣人员"}, ], }, { "id": "security", "name": "安保秩序", "items": [ {"id": "intrusion", "name": "检测人员非法闯入禁区/未经许可区域", "need": None, "desc": "人员质心落入禁区多边形"}, {"id": "object_move", "name": "识别物资/设备异常移动", "need": None, "desc": "背包/行李箱等物资目标位移超过阈值"}, {"id": "crowd", "name": "识别异常人群聚集", "need": None, "desc": "聚焦区内人数超阈值并持续"}, {"id": "plate", "name": "识别进出车辆车牌", "need": None, "desc": "车牌定位(模型或颜色+轮廓)+ EasyOCR 识别"}, ], }, { "id": "vehicle", "name": "车辆及物资管理", "items": [ {"id": "vehicle_class", "name": "对进出车辆进行分类", "need": None, "desc": "COCO car/bus/truck/motorcycle 分类归类"}, {"id": "vehicle_count", "name": "统计进出车辆数量", "need": None, "desc": "越线计数(可配置计数线)"}, {"id": "material_check", "name": "运输物资车辆与进出记录核对", "need": None, "desc": "货车通行单与申报清单比对"}, {"id": "vehicle_report", "name": "按时间段/位置生成车辆进出报告", "need": None, "desc": "按时段聚合的进出统计"}, ], }, { "id": "people", "name": "人员管理", "items": [ {"id": "people_count", "name": "统计施工/仓储区域人员数量", "need": None, "desc": "按区域 ROI 实时人数"}, {"id": "people_report", "name": "按时间段/位置生成人员统计报告", "need": None, "desc": "按时段聚合的人数峰值/均值"}, ], }, ] ALL_CATEGORY_IDS = [i["id"] for g in CATEGORY_GROUPS for i in g["items"]] # ---------------------------------------------------------------- 默认区域 def default_regions(width: int = 1920, height: int = 1080) -> dict: """默认区域:施工区=左侧 2/3,仓储区=右侧 1/3,禁区=右下角,机械区=左下角,计数线=画面中部竖线。""" w, h = width, height return { "construction": [[0, 0], [int(w * 0.66), 0], [int(w * 0.66), h], [0, h]], "warehouse": [[int(w * 0.66), 0], [w, 0], [w, h], [int(w * 0.66), h]], "restricted": [[int(w * 0.60), int(h * 0.55)], [int(w * 0.98), int(h * 0.55)], [int(w * 0.98), int(h * 0.95)], [int(w * 0.60), int(h * 0.95)]], "machinery": [[int(w * 0.02), int(h * 0.55)], [int(w * 0.40), int(h * 0.55)], [int(w * 0.40), int(h * 0.95)], [int(w * 0.02), int(h * 0.95)]], "high_work": [[0, 0], [w, 0], [w, int(h * 0.35)], [0, int(h * 0.35)]], "count_line": [[int(w * 0.5), 0], [int(w * 0.5), h]], } # 需要区域判定的事件 -> region key EVENT_REGION = { "machinery_no_guard": "machinery", "intrusion": "restricted", "crowd": "restricted", "no_harness": "high_work", } # 区域中文名 REGION_NAMES = { "construction": "施工区域", "warehouse": "仓储区域", "restricted": "禁区", "machinery": "工程机械区域", "high_work": "高处作业区", } # 检测项中文名(用于事件标题) CATEGORY_NAMES = {i["id"]: i["name"] for g in CATEGORY_GROUPS for i in g["items"]} CATEGORY_GROUP_OF = {i["id"]: g["name"] for g in CATEGORY_GROUPS for i in g["items"]} # ---------------------------------------------------------------- i18n 辅助 # 说明:语言包与前端共用 frontend/i18n.json;缺失时回退到本文件里的中文表。 from . import i18n as _i18n # noqa: E402 (放末尾,避免与 config 的其他定义产生顺序问题) def category_name(lang: str, cid: str) -> str: return _i18n.tr(lang, f"cat.{cid}", CATEGORY_NAMES.get(cid, cid)) def category_desc(lang: str, cid: str) -> str: return _i18n.tr(lang, f"cat.desc.{cid}", next( (i.get("desc", "") for g in CATEGORY_GROUPS for i in g["items"] if i["id"] == cid), "")) def group_name(lang: str, gid: str) -> str: zh = next((g["name"] for g in CATEGORY_GROUPS if g["id"] == gid), gid) return _i18n.tr(lang, f"group.{gid}", zh) def region_name(lang: str, key: str) -> str: return _i18n.tr(lang, f"region.{key}", REGION_NAMES.get(key, key or "")) def event_title(lang: str, cid: str) -> str: return _i18n.tr(lang, f"evt.{cid}", CATEGORY_NAMES.get(cid, cid)) def class_name(lang: str, code: str, fallback: str = "") -> str: """检测/车型/物资类名(cls.*)。""" return _i18n.tr(lang, f"cls.{code}", fallback or code)