做目标跟踪框图实验多算法对比过程中,
将txt数据转换为mat格式
代码1:亲测可行,
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 = 'E:\\000Code\\000AAA_paper_code\\OTB100\\GroundTruth' #3 修改.txt文件的地址
savefile = 'E:\\000Code\\000AAA_paper_code\\OTB100\\GroundTruthmat\\' # 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 + '_GroundTruth.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)
可能出现下面报错
x[3] = float(x[3][:-1])
IndexError: list index out of range
解决办法:
txt文件数据中可能没有处理好,文件中可能存在空格的文件数据,需要将空格转化为,再进行处理。
txt转化为mat文件,代码2(matlab版本):
clear;clc;
close all;
% txt路径
txt_path = 'E:\000Code\000AAA_paper_code\OTB100\GroundTruth\';
% mat路径
mat_path = 'E:\000Code\000AAA_paper_code\OTB100\GroundTruthmat\';
mkdir(mat_path);
% 跟踪器名字
tracker='GroundTruth';
txt_files = dir([txt_path '*.txt']);
txt_files={txt_files.name};
for i=1:numel(txt_files)
region=csvread([txt_path txt_files{i}]);
results{1}.type = 'rect';
results{1}.res = region;
results{1}.len = length(region);
videoname=txt_files{i};
videoname=videoname(1:length(videoname)-4);
save([mat_path videoname '_' tracker '.mat'], 'results');
end
记录bug的一天!