行人计数分析 deepsort--track.py

这个模块主要是用于跟踪算法的,用的是kalman filter+级联检测目标,基本算法是画轨迹,根据轨迹的情况,判断是否新的id,比如轨迹断开了30帧以上,判断这个轨迹delete了;(如果我躲在树后面几分钟再出来,会不会判断我是两个人? 不会,除了轨迹的级联匹配,还用到了tf的特征判断,会把每个人的特征cache保存下来,用来比较是否两个人,一个id对应一个特征,再用tensorflow进行相似度比较,tensorflow的数据集释Mars的,有100W张图片,ID SW的错误比较低,除非是模糊的人像 )

# vim: expandtab:ts=4:sw=4

#单个目标跟踪状态的枚举类型。
新建的跟踪是被归类为“暂定”直到收集到足够的证据。
然后,轨道状态更改为“确认”。
不再活着的轨迹被分类为'删除',以标记他们从一组活跃删除。

class TrackState:
    """
    Enumeration type for the single target track state. Newly created tracks are
    classified as `tentative` until enough evidence has been collected. Then,
    the track state is changed to `confirmed`. Tracks that are no longer alive
    are classified as `deleted` to mark them for removal from the set of active
    tracks.

    """
# 三种状态   尝试性的,确定的,被删除的
    Tentative = 1
    Confirmed = 2
    Deleted = 3


class Track:
    """
    # 具有状态空间的单个目标轨道(x,y,A,H)和相关联速度,
      其中“(x,y)”是bbox的中心,A是高宽比和“H”是高度。
    
    A single target track with state space `(x, y, a, h)` and associated
    velocities, where `(x, y)` is the center of the bounding box, `a` is the
    aspect ratio and `h` is the height.

    Parameters
    ----------
mean :
        # 初始状态分布的平均向量。
        Mean vector of the initial state distribution.

covariance :  #协方差
        # 初始状态分布的协方差矩阵
        Covariance matrix of the initial state distribution.

track_id : int
        # 唯一的轨迹ID
        A unique track identifier.
    
n_init : int
        # 在轨道设置为confirmed之前的连续检测帧数。
        当一个miss发生时,轨道状态设置为Deleted帧。
        Number of consecutive detections before the track is confirmed. 
The track state is set to `Deleted` if a miss occurs within the first
 `n_init` frames.
    
max_age : int
        # 在侦测状态设置成Deleted前,最大的连续miss数。
        The maximum number of consecutive misses before the track state is set to `Deleted`.

feature : Optional[ndarray]
        # 特征向量检测的这条轨道的起源。
          如果为空,则这个特性被添加到'特性'缓存中。
        Feature vector of the detection this track originates from. If not None,
        this feature is added to the `features` cache.

trackid:
      轨迹ID。
---------
Attributes #属性
   
 mean : ndarray  #均值:
        # 初始分布均值向量。
        Mean vector of the initial state distribution.
   
 covariance : ndarray  # 协方差:
       # 初始分布协方差矩阵。
        Covariance matrix of the initial state distribution.
   
 track_id : int
        A unique track identifier.
   
 hits : int
        #测量更新的总数。
        Total number of measurement updates.
   
 hit_streak : int
        # 自上次miss之后,连续测量更新的总数。(更新一次+1)
        Total number of consective measurement updates since last miss.
    age : int
        #从开始的总帧数
        Total number of frames since first occurance.

    time_since_update : int
        # 从上次的测量更新完后,统计的总帧数
        Total number of frames since last measurement update.

  state : TrackState
        # 当前的侦测状态
        The current track state.
   
 features : List[ndarray]
        # 特性的缓存。在每个度量更新中,相关的特性
向量添加到这个列表中。
        A cache of features. On each measurement update, the associated feature
        vector is added to this list.

    """
# 初始化各参数
    def __init__(self, mean, covariance, track_id, n_init, max_age,
                 feature=None):
        self.mean = mean
        self.covariance = covariance
        self.track_id = track_id
        self.hits = 1
        self.hit_streak = 1
        self.age = 1
        self.time_since_update = 0

        self.state = TrackState.Tentative
        self.features = []
        if feature is not None:
            self.features.append(feature)

        self._n_init = n_init
        self._max_age = max_age

# 将bbox转换成xywh
    def to_tlwh(self):
        """Get current position in bounding box format `(top left x, top left y,
        width, height)`.

        Returns
        -------
        ndarray
            The bounding box.

        """
        ret = self.mean[:4].copy()
        ret[2] *= ret[3]
        ret[:2] -= ret[2:] / 2
        return ret

# 获取当前位置以某种格式,不太明白
    def to_tlbr(self):
        """Get current position in bounding box format `(min x, miny, max x, max y)`.

        Returns
        -------
        ndarray
            The bounding box.

        """
        ret = self.to_tlwh()
        ret[2:] = ret[:2] + ret[2:]
        return ret

# 预测,基于kalman filter
    def predict(self, kf):
        """Propagate the state distribution to the current time step using a
        Kalman filter prediction step.

        Parameters
        ----------
        kf : kalman_filter.KalmanFilter
            The Kalman filter.

        """
        self.mean, self.covariance = kf.predict(self.mean, self.covariance)
        self.age += 1
        self.time_since_update += 1

# 更新。 主要是步进和特征,检测方法为级联检测
    def update(self, kf, detection):
        """Perform Kalman filter measurement update step and update the feature
        cache.

        Parameters
        ----------
        kf : kalman_filter.KalmanFilter
            The Kalman filter.
        detection : Detection
            The associated detection.

        """
        self.mean, self.covariance = kf.update(
            self.mean, self.covariance, detection.to_xyah())
        self.features.append(detection.feature)

        self.hit_streak += 1
        self.hits += 1
        self.time_since_update = 0
        if self.state == TrackState.Tentative and self.hits >= self._n_init:
            self.state = TrackState.Confirmed

# 标记已经miss的,如果从更新起miss了_max_age(30)帧以上,
设置为Deleted
    def mark_missed(self):
        """Mark this track as missed (no association at the current time step).
        """
        if self.state == TrackState.Tentative:
            self.state = TrackState.Deleted
        elif self.time_since_update > self._max_age:
            self.state = TrackState.Deleted
        self.hit_streak = 0

# 设置三种状态
    def is_tentative(self):
        """Returns True if this track is tentative (unconfirmed).
        """
        return self.state == TrackState.Tentative

    def is_confirmed(self):
        """Returns True if this track is confirmed."""
        return self.state == TrackState.Confirmed

    def is_deleted(self):
        """Returns True if this track is dead and should be deleted."""
        return self.state == TrackState.Deleted

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,496评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,407评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,632评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,180评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,198评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,165评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,052评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,910评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,324评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,542评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,711评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,424评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,017评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,668评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,823评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,722评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,611评论 2 353

推荐阅读更多精彩内容

  • 阶段一、人工智能基础 — 高等数学必知必会 本阶段主要从数据分析、概率论和线性代数及矩阵和凸优化这四大块讲解基础,...
    每天都有新的太阳阅读 1,493评论 1 6
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,651评论 18 139
  • Machinarium(机械迷城)由瑞典游戏开发公司Amanita Design于2009年开发的一款解谜游戏,游...
    暗影M_Shadow阅读 723评论 0 3
  • 辞职后创业,到现在已经一年多了。在这一年的时间了发生过很多的事情,我也想在这个时间好好的总结以下我创业这1年来的大...
    职场逻辑阅读 698评论 4 16
  • 我只想努力的看看这个世界 看遍每一朵鲜花 体会不同的生活; 我就想尽情的爱一次 用力的痛一次; 我啊,就只是想知道...
    踏着朝阳阅读 210评论 0 3