本文的视频讲解目标跟踪_OTB数据集和VOT数据集评估图的绘制
一、准备工作
在GitHub上面下载下载一个SiamFC的代码,自己配置好深度学习环境,然后运行获得测试结果。
准备好pycharm跑出来的.txt结果文件
二、利用Python代码进行转换
转换代码如下:
import os
import scipy.io as scio
import numpy as np
# 1 首先,将jogging.1.txt,jogging.2.txt,Skating2.1.txt,Skating2.2.txt修改成
# jogging-1.txt,jogging-2.txt,Skating2-1.txt,Skating2-2.txt
# 2 打开tiger1.txt文件,并删除前5行行数据,只留下349行数据
file = 'D:\\000MyFiles\Tracking\\txt_to_mat-OTB--master\\ThisWork' #3 修改.txt文件的地址
savefile = 'D:\\000MyFiles\Tracking\\txt_to_mat-OTB--master\\ThisWorkmat\\' # 4 修改.mat文件的存放地址
if not os.path.exists(savefile):
os.mkdir(savefile)
pathlist = os.listdir(file)
# print(pathlist)
for i in range(len(pathlist)):
path = os.path.join(file, pathlist[i])
for j in range(len(pathlist[i])):
if pathlist[i][j] == '.':
save = pathlist[i][:j]
print(save)
break
savepath = savefile + save + '_ThisWork.mat' ##5 修改 .mat文件转换后的算法名
f = open(path)
data = f.readlines()
result = []
for j in range(len(data)):
x = data[j].split(',')
x[3] = float(x[3][:-1])
x[0] = float(x[0])
x[1] = float(x[1])
x[2] = float(x[2])
result.append(x)
res = {}
res['res'] = result
res['type'] = 'rect'
Mat = {'res': res['res'], 'type': 'rect', 'len': len(res['res']), 'annoBegin': 1, 'startFrame': 1}
M = np.array([Mat])
Mat2 = {'__header__': 'b', '__version__': 1.0, '__globals__': [], 'results': M}
scio.savemat(savepath, Mat2)
# print(data)
三、转换结果
转换后的结果如图所示:
四、.mat文件转换为.txt格式
参考博文目标跟踪——OTB平台的Python版tracker使用
这个用得比较少,有时候需要的话,也可以用上。
将.mat文件转换成.txt文件,代码如下:
import scipy.io as scio
import pandas as pd
import numpy as np
import os
import h5py
import hdf5storage
# ------------------------------
# 1. .mat文件转换成.txt文件
# ------------------------------
# 注意:下面的代码根据不同类型的.mat有所不同 可输出.mat中的keys查看
path_matfile = "E:\\0_OTB_benchmark\\SiamRPN\\SiamRPN_OTB100"
outpath = "E:\\0_OTB_benchmark\\SiamRPN\\SiamRPN_OTB100_txt"
all_matfile_list = os.listdir(path_matfile)
each_matfile_path = [os.path.join(path_matfile, matfile) for matfile in all_matfile_list]
videos_name_txt = [video_name.replace('_SIAMRPN', '').replace('.mat', '').capitalize() + '.txt' for video_name in all_matfile_list]
for i, matfile_path in enumerate(each_matfile_path):
# data = scio.loadmat(matfile_path) # 有些类型的mat文件不能读取
data = hdf5storage.loadmat(matfile_path) # 兼容mat文件类型较多
# 下面两行有些.mat不同
track_result = data['results'] # 有些tracker的键值为'result'
track_result = track_result[0][0][0][0][0] # 有些为track_result[0][0][0][1]
print(videos_name_txt[i])
np.savetxt(os.path.join(outpath, videos_name_txt[i]), track_result, delimiter=',', fmt='%d')
# -----------------------
# 2. 文件重命名
# -----------------------
# 有些trackers在OTB100或OTB50中生成的是小写字母开头的跟踪结果,需要把首字母转化成大写字母,以匹配otb100.json或otb50.json(因为我下载的.json文件视频的首字母都是大写的)
# 代码依实际情况变化
src_path = "E:\\0_OTB_benchmark\\Pysot\\pysot-toolkit\\result\\Results_OTB100\\DaSiamRPN"
dst_path = "E:\\0_OTB_benchmark\\Pysot\\pysot-toolkit\\result\\Results_OTB100\\DaSiamRPN_upper"
all_filename = os.listdir(src_path)
all_filename_path_old = [os.path.join(src_path, filename) for filename in all_filename if '.txt' in filename]
all_filename_path_new = [os.path.join(dst_path, filename.capitalize()) for filename in all_filename] # .capitalize()将字符串首字母变为大写
for (old_file, new_file) in zip(all_filename_path_old, all_filename_path_new):
os.renames(old_file, new_file) # 好像把原来的文件夹覆盖掉了,先拷贝一份,以防万一
# 从OTB100中转移OTB50数据
src_otb100_path = "E:\\0_OTB_benchmark\\Pysot\\pysot-toolkit\\result\\Results_OTB100"
dst_otb50_path = "E:\\0_OTB_benchmark\\Pysot\\pysot-toolkit\\result\\Results_OTB50"
template_tracker = os.path.join(dst_otb50_path, 'Initial_Method')
template_videos = os.listdir(template_tracker)
all_trackername = os.listdir(src_otb100_path)
src_trackers = [os.path.join(src_otb100_path, src_tracker) for src_tracker in all_trackername]
for tracker_file in all_trackername:
if tracker_file not in os.listdir(dst_otb50_path):
os.mkdir(os.path.join(dst_otb50_path, tracker_file))
print(template_videos)
print(all_trackername)
for i, tracker in enumerate(src_trackers):
each_tracker_videos = os.listdir(tracker)
for video in template_videos:
shutil.copyfile(os.path.join(tracker, video), os.path.join(dst_otb50_path, all_trackername[i], video))