You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow Lite目标检测:仅跟踪首个检测到的单个人员方案求助

单人员持续跟踪实现方案(基于TensorFlow Lite)

核心实现思路

  1. 检测结果筛选:利用已自定义的TFLite人员检测模型,每帧推理后过滤出置信度达标(比如≥0.5)的人员检测框;若存在多个框,按需求选取首个检测到的框(或置信度最高的框,可根据场景调整)。
  2. 单目标跟踪逻辑:采用轻量的IOU(交并比)匹配算法维护跟踪状态——将当前帧的候选检测框与上一帧的跟踪框计算IOU,超过阈值则判定为同一目标,更新跟踪框;若当前帧无有效检测框,用上一帧跟踪框做简单位置预测(如保持原位置或匀速平移),避免短期遮挡导致跟踪中断。
  3. 状态变量维护:需全局维护上一帧的跟踪框坐标、置信度等信息,用于跨帧匹配。

代码示例(Python)

import tensorflow as tf
import cv2
import numpy as np

# 初始化TFLite模型
interpreter = tf.lite.Interpreter(model_path="your_person_detector.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# 跟踪状态变量
last_track_box = None  # 存储上一帧跟踪框:[x_min, y_min, x_max, y_max]
CONF_THRESHOLD = 0.5
IOU_THRESHOLD = 0.3

def calculate_iou(box1, box2):
    # 计算两个框的IOU
    x1 = max(box1[0], box2[0])
    y1 = max(box1[1], box2[1])
    x2 = min(box1[2], box2[2])
    y2 = min(box1[3], box2[3])
    inter_area = max(0, x2 - x1) * max(0, y2 - y1)
    box1_area = (box1[2]-box1[0])*(box1[3]-box1[1])
    box2_area = (box2[2]-box2[0])*(box2[3]-box2[1])
    return inter_area / (box1_area + box2_area - inter_area)

def process_frame(frame):
    global last_track_box
    # 预处理帧:适配模型输入尺寸
    input_shape = input_details[0]['shape']
    resized_frame = cv2.resize(frame, (input_shape[1], input_shape[2]))
    input_data = np.expand_dims(resized_frame, axis=0).astype(np.uint8)  # 按需调整数据类型

    # 运行TFLite推理
    interpreter.set_tensor(input_details[0]['index'], input_data)
    interpreter.invoke()

    # 解析输出(以SSD类模型为例,输出格式需匹配你的模型)
    boxes = interpreter.get_tensor(output_details[0]['index'])[0]  # 检测框坐标
    scores = interpreter.get_tensor(output_details[2]['index'])[0]  # 置信度

    # 筛选合格的人员检测框
    valid_boxes = []
    for i in range(len(scores)):
        if scores[i] >= CONF_THRESHOLD:
            # 转换为原帧尺寸的坐标
            h, w = frame.shape[:2]
            x_min = int(boxes[i][1] * w)
            y_min = int(boxes[i][0] * h)
            x_max = int(boxes[i][3] * w)
            y_max = int(boxes[i][2] * h)
            valid_boxes.append([x_min, y_min, x_max, y_max, scores[i]])

    current_track_box = None
    if valid_boxes:
        # 选取首个检测框(或按置信度排序取最高)
        candidate_box = valid_boxes[0][:4]
        if last_track_box is not None:
            iou = calculate_iou(last_track_box, candidate_box)
            if iou >= IOU_THRESHOLD:
                current_track_box = candidate_box
            else:
                # 若IOU不达标,重置跟踪(可选:也可保留原跟踪框)
                current_track_box = candidate_box
        else:
            # 首次检测到目标,初始化跟踪框
            current_track_box = candidate_box
    else:
        # 当前帧无检测框,用上一帧跟踪框做简单预测
        if last_track_box is not None:
            current_track_box = last_track_box  # 也可加入匀速预测逻辑

    # 更新跟踪状态
    if current_track_box is not None:
        last_track_box = current_track_box
        # 在帧上绘制跟踪框
        cv2.rectangle(frame, (current_track_box[0], current_track_box[1]), 
                      (current_track_box[2], current_track_box[3]), (0, 255, 0), 2)

    return frame

# 测试视频流
cap = cv2.VideoCapture(0)  # 或视频文件路径
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break
    processed_frame = process_frame(frame)
    cv2.imshow('Single Person Tracking', processed_frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

关键优化与注意事项

  • 置信度与IOU阈值调整:根据实际场景(如室内/室外、光照条件)调整CONF_THRESHOLD和IOU_THRESHOLD,避免误检或跟踪丢失。
  • 端侧性能优化:若部署在嵌入式设备,可采用量化TFLite模型、降低输入分辨率(如320x320),减少推理耗时。
  • 遮挡处理:可加入消失计数逻辑,连续N帧未检测到目标时才重置跟踪框,避免短暂遮挡导致跟踪中断。
  • 坐标转换:确保TFLite输出的归一化坐标正确转换为原帧的像素坐标,避免跟踪框位置偏移。

内容的提问来源于stack exchange,提问作者Srinithin S B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 03:05:32