config.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. """全局配置:路径、模型、16 个检测项注册表、默认 ROI / 阈值。"""
  2. from __future__ import annotations
  3. import os
  4. from pathlib import Path
  5. BASE_DIR = Path(__file__).resolve().parent.parent
  6. MODELS_DIR = BASE_DIR / "models"
  7. ASSETS_DIR = BASE_DIR / "assets"
  8. STORAGE_DIR = Path(os.environ.get("DCCAI_STORAGE", BASE_DIR / "storage"))
  9. UPLOAD_DIR = STORAGE_DIR / "uploads"
  10. EVENT_DIR = STORAGE_DIR / "events"
  11. CACHE_DIR = STORAGE_DIR / "cache"
  12. FRONTEND_DIR = BASE_DIR / "frontend"
  13. for _d in (UPLOAD_DIR, EVENT_DIR, CACHE_DIR):
  14. _d.mkdir(parents=True, exist_ok=True)
  15. # ---------------------------------------------------------------- 模型配置
  16. SAFETY_MODEL = os.environ.get("DCCAI_SAFETY_MODEL", str(MODELS_DIR / "yolo11m_safety.pt"))
  17. GENERIC_MODEL = os.environ.get("DCCAI_GENERIC_MODEL", str(MODELS_DIR / "yolo11n.pt"))
  18. # 可选扩展模型(缺失时对应检测项标记为「需模型」,不会导致启动失败)
  19. OPTIONAL_MODELS = {
  20. "shoe": str(MODELS_DIR / "shoe.pt"), # 安全鞋
  21. "harness": str(MODELS_DIR / "harness.pt"), # 安全带
  22. "plate": str(MODELS_DIR / "plate.pt"), # 车牌检测
  23. }
  24. SAFETY_CLASSES = {0: "hat", 1: "nohat", 2: "novest", 3: "person", 4: "vest"}
  25. # COCO 中与车辆 / 物资相关的类别
  26. VEHICLE_CLASSES = {
  27. "car": "小型车",
  28. "bus": "客车",
  29. "truck": "货车",
  30. "motorcycle": "摩托车",
  31. "bicycle": "非机动车",
  32. }
  33. MATERIAL_CLASSES = {"backpack": "背包", "suitcase": "行李箱", "handbag": "手提包"}
  34. DEVICE = os.environ.get("DCCAI_DEVICE", "auto") # auto | cpu | mps | cuda | cuda:0
  35. IMG_SIZE = int(os.environ.get("DCCAI_IMGSIZE", "960"))
  36. # ---------------------------------------------------------------- 阈值
  37. DEFAULT_THRESHOLDS = {
  38. "person_conf": 0.45, # 人
  39. "ppe_conf": 0.50, # 安全帽 / 反光衣
  40. "vehicle_conf": 0.40, # 车辆
  41. "generic_conf": 0.35, # 通用目标
  42. # 跌倒:人框宽高比 w/h 超过该值、且此前曾直立,并持续 fall_min_frames 帧
  43. "fall_aspect_ratio": 1.35,
  44. "fall_upright_ratio": 0.95,
  45. "fall_window": 40,
  46. "fall_min_frames": 6,
  47. # 人群聚集:区域内人数超过阈值并持续 crowd_seconds 秒
  48. "crowd_count": 4,
  49. "crowd_seconds": 3.0,
  50. # 物资异常移动:目标质心位移超过像素阈值
  51. "move_pixels": 60,
  52. # 越线计数:滞回死区(像素)与同目标两次越线的最小间隔(秒)
  53. "line_margin": 25,
  54. "line_cooldown": 3.0,
  55. # 事件去抖:同类事件在该秒数内不重复触发
  56. "event_cooldown": 12.0,
  57. # 已确认事件后,同类事件再次触发的静默期(同一目标)
  58. "track_event_cooldown": 30.0,
  59. }
  60. # ---------------------------------------------------------------- 检测大类
  61. # need: 依赖的模型 key;None 表示仅靠规则/主模型即可
  62. CATEGORY_GROUPS = [
  63. {
  64. "id": "labor",
  65. "name": "劳动安全",
  66. "items": [
  67. {"id": "fall", "name": "识别劳动人员跌倒事件", "need": None,
  68. "desc": "基于人体框宽高比 + 持续帧数的姿态判据"},
  69. {"id": "no_helmet", "name": "识别工人未佩戴安全帽", "need": None,
  70. "desc": "安全模型 nohat 类别"},
  71. {"id": "no_vest", "name": "识别工人未穿反光背心", "need": None,
  72. "desc": "安全模型 novest 类别"},
  73. {"id": "no_shoe", "name": "识别工人未穿安全鞋", "need": "shoe",
  74. "desc": "需 models/shoe.pt 权重(当前无开源权重)"},
  75. {"id": "no_harness", "name": "识别高处作业未系安全带", "need": "harness",
  76. "desc": "需 models/harness.pt + 高处 ROI(当前无开源权重)"},
  77. {"id": "machinery_no_guard", "name": "识别工程机械区域未配备防护设备", "need": None,
  78. "desc": "机械 ROI 内出现未戴帽/未穿反光衣人员"},
  79. ],
  80. },
  81. {
  82. "id": "security",
  83. "name": "安保秩序",
  84. "items": [
  85. {"id": "intrusion", "name": "检测人员非法闯入禁区/未经许可区域", "need": None,
  86. "desc": "人员质心落入禁区多边形"},
  87. {"id": "object_move", "name": "识别物资/设备异常移动", "need": None,
  88. "desc": "背包/行李箱等物资目标位移超过阈值"},
  89. {"id": "crowd", "name": "识别异常人群聚集", "need": None,
  90. "desc": "聚焦区内人数超阈值并持续"},
  91. {"id": "plate", "name": "识别进出车辆车牌", "need": None,
  92. "desc": "车牌定位(模型或颜色+轮廓)+ EasyOCR 识别"},
  93. ],
  94. },
  95. {
  96. "id": "vehicle",
  97. "name": "车辆及物资管理",
  98. "items": [
  99. {"id": "vehicle_class", "name": "对进出车辆进行分类", "need": None,
  100. "desc": "COCO car/bus/truck/motorcycle 分类归类"},
  101. {"id": "vehicle_count", "name": "统计进出车辆数量", "need": None,
  102. "desc": "越线计数(可配置计数线)"},
  103. {"id": "material_check", "name": "运输物资车辆与进出记录核对", "need": None,
  104. "desc": "货车通行单与申报清单比对"},
  105. {"id": "vehicle_report", "name": "按时间段/位置生成车辆进出报告", "need": None,
  106. "desc": "按时段聚合的进出统计"},
  107. ],
  108. },
  109. {
  110. "id": "people",
  111. "name": "人员管理",
  112. "items": [
  113. {"id": "people_count", "name": "统计施工/仓储区域人员数量", "need": None,
  114. "desc": "按区域 ROI 实时人数"},
  115. {"id": "people_report", "name": "按时间段/位置生成人员统计报告", "need": None,
  116. "desc": "按时段聚合的人数峰值/均值"},
  117. ],
  118. },
  119. ]
  120. ALL_CATEGORY_IDS = [i["id"] for g in CATEGORY_GROUPS for i in g["items"]]
  121. # ---------------------------------------------------------------- 默认区域
  122. def default_regions(width: int = 1920, height: int = 1080) -> dict:
  123. """默认区域:施工区=左侧 2/3,仓储区=右侧 1/3,禁区=右下角,机械区=左下角,计数线=画面中部竖线。"""
  124. w, h = width, height
  125. return {
  126. "construction": [[0, 0], [int(w * 0.66), 0], [int(w * 0.66), h], [0, h]],
  127. "warehouse": [[int(w * 0.66), 0], [w, 0], [w, h], [int(w * 0.66), h]],
  128. "restricted": [[int(w * 0.60), int(h * 0.55)], [int(w * 0.98), int(h * 0.55)],
  129. [int(w * 0.98), int(h * 0.95)], [int(w * 0.60), int(h * 0.95)]],
  130. "machinery": [[int(w * 0.02), int(h * 0.55)], [int(w * 0.40), int(h * 0.55)],
  131. [int(w * 0.40), int(h * 0.95)], [int(w * 0.02), int(h * 0.95)]],
  132. "high_work": [[0, 0], [w, 0], [w, int(h * 0.35)], [0, int(h * 0.35)]],
  133. "count_line": [[int(w * 0.5), 0], [int(w * 0.5), h]],
  134. }
  135. # 需要区域判定的事件 -> region key
  136. EVENT_REGION = {
  137. "machinery_no_guard": "machinery",
  138. "intrusion": "restricted",
  139. "crowd": "restricted",
  140. "no_harness": "high_work",
  141. }
  142. # 区域中文名
  143. REGION_NAMES = {
  144. "construction": "施工区域",
  145. "warehouse": "仓储区域",
  146. "restricted": "禁区",
  147. "machinery": "工程机械区域",
  148. "high_work": "高处作业区",
  149. }
  150. # 检测项中文名(用于事件标题)
  151. CATEGORY_NAMES = {i["id"]: i["name"] for g in CATEGORY_GROUPS for i in g["items"]}
  152. CATEGORY_GROUP_OF = {i["id"]: g["name"] for g in CATEGORY_GROUPS for i in g["items"]}
  153. # ---------------------------------------------------------------- i18n 辅助
  154. # 说明:语言包与前端共用 frontend/i18n.json;缺失时回退到本文件里的中文表。
  155. from . import i18n as _i18n # noqa: E402 (放末尾,避免与 config 的其他定义产生顺序问题)
  156. def category_name(lang: str, cid: str) -> str:
  157. return _i18n.tr(lang, f"cat.{cid}", CATEGORY_NAMES.get(cid, cid))
  158. def category_desc(lang: str, cid: str) -> str:
  159. return _i18n.tr(lang, f"cat.desc.{cid}", next(
  160. (i.get("desc", "") for g in CATEGORY_GROUPS for i in g["items"] if i["id"] == cid), ""))
  161. def group_name(lang: str, gid: str) -> str:
  162. zh = next((g["name"] for g in CATEGORY_GROUPS if g["id"] == gid), gid)
  163. return _i18n.tr(lang, f"group.{gid}", zh)
  164. def region_name(lang: str, key: str) -> str:
  165. return _i18n.tr(lang, f"region.{key}", REGION_NAMES.get(key, key or ""))
  166. def event_title(lang: str, cid: str) -> str:
  167. return _i18n.tr(lang, f"evt.{cid}", CATEGORY_NAMES.get(cid, cid))
  168. def class_name(lang: str, code: str, fallback: str = "") -> str:
  169. """检测/车型/物资类名(cls.*)。"""
  170. return _i18n.tr(lang, f"cls.{code}", fallback or code)