最近需要用到BITVehicle_Dataset数据集但是,他的标注结果是mat形式,在matlab中先将mat格式的标注数据转为voc格式,然后再用python从xml中提取标注框的信息,生成最终的训练数据。BITVehicle_Dataset数据集是{‘Bus’, ‘Truck’, ‘SUV’, ‘Microbus’, ‘Sedan’, ‘Minivan’}这几种车型,这样后面需要用到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
亲测有效!!!感谢UP主!!!
原文链接: https://blog.csdn.net/wolf1132/article/details/89076660
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/99871.html