最近在做一个无人机车辆巡检的项目,需要用到大量的汽车相关的视频图片数据,前几天参考一些资料训练了bdd100k数据集,但是无人机巡检犹豫在高空中拍摄,清晰度不高,同时角度是俯视,这种角度的数据很难找,不过做实验阶段先找开源的数据吧,同时需要自己标注大量的数据进行补充。这里将BITVehicle_Dataset数据集与bdd100k数据集联合训练,但是BITVehicle_Dataset数据集提供的标注文件是一个matlab的mat文件,处理bdd100k数据集时已经用python实现了json格式到xml格式的转换,并从xml格式数据中组装darknet需要的数据格式(txt)格式,BITVehicle_Dataset数据集是{'Bus', 'Truck', 'SUV', 'Microbus', 'Sedan', 'Minivan'}这几种车型,bdd100k力度比较粗,没有细化到品牌,我这里的解决办法是在matlab中先将mat格式的标注数据转为voc格式,然后再用python从xml中提取标注框的信息,生成最终的训练数据。类别的转换使用一个python中的映射字典完成,这样后面需要用到BITVehicle_Dataset数据集时更加方便
下面是matlab提取BITVehicle_Dataset标注信息,转成xml的代码,已经带有详细注释。其他的脚本陆陆续续放上来,备忘。每天写一点吧项目还没做完。
data=load('./BITVehicle_Dataset/VehicleInfo.mat')
cars=data.VehicleInfo;
for n=1:length(cars)
n;
car=cars(n);
%%
%每一辆车新建一个xml文件存储车的信息
carName=car.name;
%图片的高度
carHeight=car.height;
%图片的宽度
carWidth=car.width;
%新建xml文件
annotation = com.mathworks.xml.XMLUtils.createDocument('annotation');
annotationRoot = annotation.getDocumentElement;
%定义子节点,xml的存储路径
folder=annotation.createElement('folder');
folder.appendChild(annotation.createTextNode(sprintf('%s','H:\车辆目标检测项目\数据集\BITVehicle_xml')));%这里为xml存放的目录
annotationRoot.appendChild(folder);
%图片的名称,不包含后缀名
jpgName=annotation.createElement('filename');
jpgName.appendChild(annotation.createTextNode(sprintf('%s',carName(1:end-4))));
annotationRoot.appendChild(jpgName);
%source就不添加了
%添加图片的size
jpgSize=annotation.createElement('size');
annotationRoot.appendChild(jpgSize);
%定义size的子节点
%图片宽度
width=annotation.createElement('width');
width.appendChild(annotation.createTextNode(sprintf('%i',carWidth)));
jpgSize.appendChild(width);
%图片高度
height=annotation.createElement('height');
height.appendChild(annotation.createTextNode(sprintf('%i',carHeight)));
jpgSize.appendChild(height);
%图片深度,彩色图片3
depth=annotation.createElement('depth');
depth.appendChild(annotation.createTextNode(sprintf('%i',3)));
jpgSize.appendChild(depth);
segmented=annotation.createElement('segmented');
segmented.appendChild(annotation.createTextNode(sprintf('%i',0)));%表示已经标注过了
annotationRoot.appendChild(segmented);
%接下来是每一辆车的标注信息
%%
carVehicles=car.vehicles;
L=length(carVehicles);
if L>1
carName
L
end
for nn=1:length(carVehicles)
vehicle=carVehicles(nn);
%标注框的最左侧坐标
vLeft=vehicle.left;
%标注框的最上边坐标
vTop=vehicle.top;
%标注框的最右侧坐标
vRight=vehicle.right;
%标注框最下面坐标
vBottom=vehicle.bottom;
%车的类别
vCategory=vehicle.category;
%图片中有多少个标注对象
carNvehicles=car.nVehicles;
%在这里生成每一符图片的txt文件,用于
%%注意一张图片中可能会有多个对象
%%matlab直接生成xml对象???
%将这一辆车的信息注入xml中
object=annotation.createElement('object');
annotationRoot.appendChild(object);
%标注框类别名称
categoryName=annotation.createElement('name');
categoryName.appendChild(annotation.createTextNode(sprintf('%s',vCategory)));
object.appendChild(categoryName);
pose=annotation.createElement('pose');
pose.appendChild(annotation.createTextNode(sprintf('%s','Unspecified')));
object.appendChild(pose);
truncated=annotation.createElement('truncated');
truncated.appendChild(annotation.createTextNode(sprintf('%i',0)));
object.appendChild(truncated);
Difficult=annotation.createElement('Difficult');
Difficult.appendChild(annotation.createTextNode(sprintf('%i',0)));
object.appendChild(Difficult);
bndbox=annotation.createElement('bndbox');
object.appendChild(bndbox);
xmin=annotation.createElement('xmin');
xmin.appendChild(annotation.createTextNode(sprintf('%i',vLeft)));
bndbox.appendChild(xmin);
ymin=annotation.createElement('ymin');
ymin.appendChild(annotation.createTextNode(sprintf('%i',vTop)));
bndbox.appendChild(ymin);
xmax=annotation.createElement('xmax');
xmax.appendChild(annotation.createTextNode(sprintf('%i',vRight)));
bndbox.appendChild(xmax);
ymax=annotation.createElement('ymax');
ymax.appendChild(annotation.createTextNode(sprintf('%i',vBottom)));
bndbox.appendChild(ymax);
end
%存储xml
savePath=['H:\车辆目标检测项目\数据集\BITVehicle_xml\',carName(1:end-3),'xml'];
xmlwrite(savePath,annotation);
end