我正在执行对象检测,并想从7481个文本文件中生成地面真相.mat文件。这些文件的内容均采用以下格式:
car 0.00 0 -1.82 804.97 167.34 995.43 327.94 1.63 1.48 2.37 3.23 1.59 8.55 -1.47
misc 0.00 5 2.35 254.24 -2 305.25 7.6 4.58 5.35 2.35 1.35 2.35 3.36 1.56
bicycle 0.00 1 2 3 1 2.3 4.25 3.1 2 1 2.4 1.25 46.5 1.54
don't know 0.00 2.21 5.32 1.23 5.25 9.46 4.35 1.25 5 1 3 2 4 1.54
也就是说,在每个文本文件中,都有几行(不同文件中的行数是不同的),并且在每一行中,第一个术语是类型(car / misc / people / van / no ...)。 ),紧随其后的是由空格分隔符分隔的14个双精度数字。我想做以下事情:
现在我的代码就像:
clc;
clear all;
DetDir = '/scratch/yangj/project/car_dataset/training/label/';
F = dir([DetDir,'/*.txt']);
for frameNum = 1:7481
detFile = [DetDir,F(frameNum).name];
fid = fopen(detFile);
while 1
tline = fgetl(fid);
if ~ischar(tline), break, end
str = tline;
end
fclose (fid);
end
我认为我应该在while循环中进行类型检查和数字提取,但是我不知道如何编写代码来实现我的目标。
你能帮我吗?
如果您的定界符是一个空格,那么该don't know
语句就很烦人了。.我建议首先使用此好函数(Perl)来解决此问题,该函数replaceinfile
可以将更don't know
改为example don't_know
。
如果这是固定的,则应该可以进行以下操作:
N = numel(F);
C = cell(N,1);
for idx = 1:N
% get the data
fid = fopen([DetDir F(idx).name]);
data = textscan(fid,'%s %f %f %f %f %f %f %f %f %f %f %f %f %f %f');
fclose(fid);
% combine all numeric data
M = horzcat(data{2:end});
% check for a string match
b = cellfun(@(type) strcmp(data{1}, type), {'car','van','misc','tram'}, 'uni', 0);
% keep only the interesting part of the numeric data
C{idx} = M(any(horzcat(b{:}),2),[4 5 6 7 14]);
end
% combine and save
gt = vertcat(C{:});
save('gt.mat', 'gt');
如果您不更改don't know
文件中的语句,则该代码实际上仍将运行,但是(通常)不会生成所需的gt
矩阵。
要回答有关添加其他内容的问题:
构建完成后M
,只需添加:
M(:,end+1) = M(:,6)-M(:,4); % this becomes the 15-th value
包括文件号是通过更改C{idx} = M(any(horzcat(b{:}),2),[4 5 6 7 14]);
为
fnr = (idx-1) * ones(sum(sum(horzcat(b{:}),2)),1);
C{idx} = [fnr M(any(horzcat(b{:}),2),[4 5 7 14 15])];
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句