其他分享
首页 > 其他分享> > 什么是多目标跟踪中的Interpolation / OC-SORT

什么是多目标跟踪中的Interpolation / OC-SORT

作者:互联网

什么是多目标跟踪中的Interpolation

解释

如果每个track_id属于多个Tracklet/segment,并且段[i]和段[i+1]在小于N_ DTI的间隔内,段[i]和段[i+1]之间的帧(未分配给该track_id)将被分配。

这属于一种后处理手段,作用是连接局部中断,但是具有相同track_id的跟踪片段

代码

核心:跟踪框插值

curr_bbox = (curr_frame - left_frame_id) * (right_bbox - left_bbox)/(right_frame_id - left_frame_id) + left_bbox

https://github.com/noahcao/OC_SORT/blob/43d2f29d3df8ecccf1dfaa2e466c6911f5c74b27/tools/interpolation.py

import numpy as np
from tqdm import tqdm
import os
import glob
import sys 

""" 
Linear Interpolation
for each track_id, if it belongs to multiple tracklets/segments,
if segments[i] and segments[i+1] are within an interval less than N_DTI, 
then 
"""

def write_results_score(filename, results):
    save_format = '{frame}, {id}, {x1}, {y1}, {w}, {h}, {s}, -1, -1, -1\n'
    with open(filename, 'w') as f:
        for i in range(results.shape[0]):
            frame_data = results[i]
            frame_id = int(frame_data[0])
            track_id = int(frame_data[1])
            x1, y1, w, h = frame_data[2:6]
            score = frame_data[6]
            line = save_format.format(frame=frame_id, id=track_id, x1=x1, y1=y1, w=w, h=h, s=-1)
            f.write(line)


def dti(txt_path, save_path, n_min=25, n_dti=20):
    seq_txts = sorted(glob.glob(os.path.join(txt_path, '*.txt')))
    for seq_txt in tqdm(seq_txts):
        seq_name = seq_txt.split('/')[-1]
        seq_data = np.loadtxt(seq_txt, dtype=np.float64, delimiter=',')
        min_track_id = int(np.min(seq_data[:, 1]))
        max_track_id = int(np.max(seq_data[:, 1]))
        seq_results = np.zeros((1, 10), dtype=np.float64)
        for track_id in range(min_track_id, max_track_id + 1):
            index = (seq_data[:, 1] == track_id)
            tracklet = seq_data[index]
            tracklet_dti = tracklet
            if tracklet.shape[0] == 0:
                continue
            n_frame = tracklet.shape[0]
            n_conf = np.sum(tracklet[:, 6] > 0.5)
            if n_frame > n_min:
                frame_idxs = tracklet[:, 0]
                frames_interpolated = {}
                for i in range(0, n_frame):
                    right_frame_id = frame_idxs[i]
                    if i > 0:
                        left_frame_id = frame_idxs[i - 1]
                    else:
                        left_frame_id = frame_idxs[i]
                    interval = right_frame_id-left_frame_id
                    if interval>1:
                        print(interval)
                    # disconnected track interpolation
                    if 1 < right_frame_id - left_frame_id < n_dti:
                        num_bi = int(right_frame_id - left_frame_id - 1)
                        right_bbox = tracklet[i, 2:6]
                        left_bbox = tracklet[i - 1, 2:6]
                        for j in range(1, num_bi + 1):
                            curr_frame = j + left_frame_id
                            curr_bbox = (curr_frame - left_frame_id) * (right_bbox - left_bbox) / \
                                        (right_frame_id - left_frame_id) + left_bbox
                            frames_interpolated[curr_frame] = curr_bbox
                num_itpl = len(frames_interpolated.keys())
                if num_itpl > 0:
                    data_dti = np.zeros((num_itpl, 10), dtype=np.float64)
                    # for each interpolated frame
                    for n in range(num_itpl):
                        data_dti[n, 0] = list(frames_interpolated.keys())[n]
                        data_dti[n, 1] = track_id
                        data_dti[n, 2:6] = frames_interpolated[list(frames_interpolated.keys())[n]]
                        data_dti[n, 6:] = [1, -1, -1, -1]
                    tracklet_dti = np.vstack((tracklet, data_dti))
            seq_results = np.vstack((seq_results, tracklet_dti))
        save_seq_txt = os.path.join(save_path, seq_name)
        seq_results = seq_results[1:]
        # seq_results = seq_results[seq_results[:, 0].argsort()]
        seq_results = seq_results[np.lexsort((seq_results[:,0],seq_results[:,1]))]
        write_results_score(save_seq_txt, seq_results)



if __name__ == '__main__':
    # txt_path, save_path = sys.argv[1], sys.argv[2]
    NAME = 'ocsort_640_960'
    txt_path =  f'XXX/{NAME}/data'
    save_path = f'XXX/{NAME}/interpolated'
    os.makedirs(save_path,exist_ok=True)
    dti(txt_path, save_path, n_min=30, n_dti=20)

标签:SORT,seq,frame,OC,results,left,data,id,Interpolation
来源: https://www.cnblogs.com/zxyfrank/p/16615634.html