Skip to content
karmueoPublic

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

39 Commits

Folders and files

Repository files navigation

detectUav

基于 Ascend ACL 的无人机检测与单目标跟踪管线。加载检测模型和可选跟踪模型,拉取 RTSP 等输入并将标注后的 H.264 通过 RTSP 推流输出。

硬件绑定:允许的 /etc/machine-id 与指纹(machine-id + product_uuid 的 FNV-1a 64 位哈希,16 进制表示)在 src/main.cpp 内编译固定,未命中时程序直接退出。启动时日志会打印 Hardware fingerprint=...,如需更换机器请调整文件中的 kAllowedMachineIds/kAllowedFingerprints 常量并重新编译。

构建与运行

  1. 安装 Ascend 驱动/工具链,设置 SYSROOT、DDK_PATH、NPU_HOST_LIB(缺省 /usr/local/Ascend/ascend-toolkit/latest)。若需要 RTSP 推流,确保 FFmpeg/OpenCV 与 live555 可用。
  2. 配置并编译(Ninja 示例):
    mkdir -p build && cd build
    cmake -G Ninja ..
    cmake --build . --target main test_mixformerv2_om
  3. 从 build/ 目录使用 JSON 配置运行:
    ./src/out/main ../scripts/test.json

以 systemd 服务方式运行

  1. 确保可执行文件已在 build/src/out/main,并使用绝对路径引用 JSON(避免切换目录导致的相对路径问题)。
  2. 创建服务文件 /etc/systemd/system/detectuav.service(需 sudo):
     [Unit]
     Description=detectUav inference pipeline
    
     [Service]
     Type=simple
     User=root
     Group=root
     WorkingDirectory=/root/work/AntiUav/
     ExecStart=/bin/bash -lc '\
       source /usr/local/Ascend/ascend-toolkit/set_env.sh && \
       echo "[systemd] detectUav starting" >&2 && \
       exec /usr/bin/stdbuf -oL -eL \
         /root/work/AntiUav/main \
         /root/work/AntiUav/scripts/config.json \
         >>/var/log/detectuav.log 2>&1'
     Restart=always
     RestartSec=5
    
     [Install]
     WantedBy=multi-user.target
  3. 重载并启用服务:
    sudo systemctl daemon-reload
    sudo systemctl enable --now detectuav.service
  4. 查看运行状态与日志:
    systemctl status detectuav.service
    cat /var/log/detectuav.log
    如需更新配置或二进制,替换文件后执行 sudo systemctl restart detectuav.service。

JSON 配置说明

程序通过 JSON 描述设备、模型与 IO。样例见 scripts/config.json、scripts/hk_rtsp.json、scripts/test.json。

顶层结构:

{
  "device_config": [
    {
      "device_id": 0,
      "model_config": [
        {
          "model_path": "./model/detect.om",
          "model_width": 640,
          "model_heigth": 352,
          "resize_type": "stretch",
          "use_nms": false,
          "model_batch": 1,
          "frame_decimation": 0,
          "target_class_id": [0],
          "filter_suspect_static_target": true,
          "static_center_threshold": 2.0,
          "static_size_threshold": 2.0,
          "static_frame_threshold": 90,
          "track_config": { ... },
          "io_info": [ { ... 每路流的配置 ... } ]
        }
      ]
    }
  ]
}

字段速查

  • device_config[]:每个条目对应一块 Ascend 设备。
    • device_id:设备编号。
    • model_config[]:该设备上的检测模型列表。
      • model_path:检测 .om 路径(相对路径从运行目录解析)。
      • model_width / model_heigth:模型输入宽高。
      • resize_type(可选,默认 stretch):预处理缩放方式。
      • use_nms(可选,默认 false):是否在后处理中启用 NMS。
      • model_batch(可选,默认 1):batch 大小。
      • postnum(可选,默认 1):后处理线程数。
      • frames_per_second(可选,默认 1000):输入线程节流上限。
      • frame_decimation(可选,默认 0):每处理 1 帧后跳过 N 帧,0 表示不跳帧,可被 io_info 覆盖。
      • target_class_id(可选,默认不过滤):检测后处理的目标类别 ID 数组,仅保留这些类别的检测结果,可被 io_info 覆盖;缺省或空数组表示不过滤。
      • filter_suspect_static_target(可选,默认 false):是否过滤疑似静止目标;对检测与跟踪都有效。
      • static_center_threshold(可选):静止目标判定的中心点位移阈值(像素)。
      • static_size_threshold(可选):静止目标判定的框尺寸变化阈值(像素或比例,取决于实现)。
      • static_frame_threshold(可选):静止判定所需连续帧数,达到后视为静止目标。
      • track_config(可选,模型级默认值):
        • enable_tracking:是否启用跟踪(默认 true)。
        • track_model_path:跟踪 .om 模型路径。
        • tracking_config:跟踪阈值配置,对应 Tracking 的 setter:
          • confidence_active_threshold:进入“稳定跟踪”状态的置信度阈值,越高越保守。
          • confidence_redetect_threshold:触发重检/重定位的置信度阈值,低于该值会尝试重新搜索目标。
          • max_track_loss_frames:连续丢失目标的最大帧数,超过该值认为跟踪失败。
          • enable_tracking_validation:是否启用周期性校验,避免漂移。
          • validation_interval:校验间隔(单位:帧),仅在启用校验时生效。
          • validation_iou_threshold:校验时的 IoU 阈值,低于该值视为校验失败。
          • validation_max_error_count:允许的连续校验失败次数,超过后触发重检或重置。
      • io_info[]:每路输入/输出通道。
        • input_path:来源(如 rtsp://... 或文件)。
        • input_type:来源类型(如 rtsp)。
        • output_path:输出目的地;RTSP 时作为推流基址;HDMI 时为设备名(如 hdmi0)。
        • output_type:输出类型;rtsp 会启用推流线程,hdmi 会启用本地显示。
        • channel_id:通道唯一 ID。
        • frame_decimation(可选):覆盖模型级跳帧。
        • target_class_id(可选):覆盖模型级类别过滤;空数组或缺省表示不过滤。
        • rtsp_config(可选,推流):
          • output_width / output_height:编码尺寸,默认取模型输入尺寸。
          • output_fps:1–60,越界会回退到 25。
          • transport:udp 或 tcp。
          • buffer_size:socket buffer,单位字节。
          • max_delay:RTP 最大延迟,微秒。
        • hdmi_config(可选,本地显示):
          • output_width / output_height:显示尺寸。
          • output_fps:显示帧率。
        • h264_config(可选,编码):
          • gop_size:1–300,越界回退到 16。
          • rc_mode:0=CBR,1=VBR,2=AVBR(>2 会被回退到 2)。
          • max_bitrate:单位 kbps,500–50000,越界回退到 10000。
          • profile:baseline | main | high。
        • 当模型级未提供 track_config 时,enable_tracking / track_model_path / tracking_config 可在通道级提供,含义相同。

如何调整 rtsp_config / h264_config

  • 分辨率与帧率:output_width / output_height 应与业务需要和带宽匹配;output_fps 过高会增压编码与带宽,先用 15–25fps 验证再升高。
  • 分辨率与帧率:output_width / output_height 应与业务需要和带宽匹配;output_fps 过高会增压编码与带宽,先用 15–25fps 验证再升高。
  • 传输方式:transport 选 udp 更低时延但可能丢包;tcp 更稳但时延高。
  • 缓冲与延迟:弱网或远端链路可适当增大 buffer_size,max_delay 影响 RTP 抖动缓冲,过大时延上升,过小易卡顿。
  • GOP 与码率:
    • gop_size 控制关键帧间隔(单位帧),直播常用 1–2 秒对应的帧数(如 25fps 用 25–50)。
    • rc_mode:0 恒定码率方便带宽预算;1 可变码率画质更高但带宽波动;2 AVBR 折中。
    • max_bitrate 直接影响画质与带宽,按上行网络预留 20–30% 裕量设置。
  • Profile:baseline 兼容性最好,main 常用,high 画质好但要求解码端支持。

分辨率与码率/帧率参考(起步值,可按网络和画质再调):

分辨率 建议 fps 建议 max_bitrate (kbps) 建议 gop_size (对应 1–2s)
1280x720 15–20 3000–5000 15–40
1920x1080 15–25 5000–8000 20–50
2560x1440 15–25 8000–12000 25–50
3840x2160 10–20 12000–20000 20–40
  • 若链路较差或延迟敏感,优先降低 fps 或分辨率,再调低 max_bitrate。
  • 关键帧密度越高(更小的 gop_size),seek/恢复更快但码率开销稍升。

RTSP 最小示例

{
  "device_config": [
    {
      "device_id": 0,
      "model_config": [
        {
          "model_path": "./model/yolov11n_110_rgb_640_raw_v0_bs1.om",
          "model_width": 640,
          "model_heigth": 640,
          "model_batch": 1,
          "frame_decimation": 5,
          "track_config": {
            "enable_tracking": true,
            "track_model_path": "./model/mixformerv2_online_small_bs1.om",
            "tracking_config": {
              "confidence_active_threshold": 0.6,
              "confidence_redetect_threshold": 0.6,
              "max_track_loss_frames": 5,
              "score_decay_factor": 0.98
            }
          },
          "io_info": [
            {
              "input_path": "rtsp://<src>",
              "input_type": "rtsp",
              "output_path": "rtsp://<sink>/uav",
              "output_type": "rtsp",
              "channel_id": 0,
              "rtsp_config": {
                "output_width": 1920,
                "output_height": 1080,
                "output_fps": 20,
                "transport": "udp",
                "buffer_size": 1024000,
                "max_delay": 200000
              },
              "h264_config": {
                "gop_size": 25,
                "rc_mode": 2,
                "max_bitrate": 10000,
                "profile": "main"
              }
            }
          ]
        }
      ]
    }
  ]
}

小贴士

  • 相对路径从当前工作目录解析(通常是 build/)。
  • frame_decimation 在处理完 1 帧后生效,例如 5 表示保留 1 帧、跳过后续 5 帧。
  • 若在模型级关闭跟踪(track_config.enable_tracking: false),请确认通道级不会重新打开。
  • 上真实流前,可用本地文件或自建 RTSP 服务先验证 JSON 配置。

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages