pascal行人voc_PASCAL Annotation轉換為VOC xml格式數據

该代码将PASCAL Annotation Version 1.00的.txt数据转换为VOC格式的.xml数据,适用于INRIA数据集。通过PASCALreadrecord.m读取数据,然后创建XML文件,包含图像信息、目标对象及其边界框。
摘要由CSDN通过智能技术生成

本代碼可以把PASCAL Annottation  Version 1.00的 .txt 數據轉換為VOC格式的. xml數據。

前提:得到 PASCAL Annotation Version 1.00 ,需要使用到其中的PASCALreadrecord.m文件。同時,我這里使用的數據集是INRIA數據集,所以只有一類目標(行人)。

代碼如下:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% PAS2XML.m% 作者:connie69% 博客:http://blog.csdn.net/connie69 % 郵箱:conniechen9469@gmail.com% 時間:2016/8/16%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 得到所有文件列表addpath('annotations'); %添加你存放標記文件的路徑annotList = dir('annotations/*.txt'); %得到該路徑下的所有txt文件annotListLen = length(annotList); % txt文件的數目for i = 1:annotListLen % 由 PASCAL Annotaions Ver. 1.00 工具讀取數據 fn = strcat('annotations/', annotList(i).name); %需要讀取的文件名 record = PASreadrecord(fn); %讀取數據 % 提取你所需的數據 imgname = record.imgname; idx = regexp(imgname,'/'); imgsize = record.imgsize; db = 'INRIA'; obj = record.objects; object_num = length(obj); % 建立一個xml文件,存儲數據、 %這是你要存儲的xml文件路徑,比如我這個代碼是存在當前工作目錄下的‘Gen/Test/pos/文件名.xml’ path_data = strcat('Gen/Test/pos/', annotList(i).name(1:end-4)); path_data = strcat(path_data, '.xml'); %下面建立節點,我用縮進代表層級 %建立根節點,可以看作是第一級 annotation = com.mathworks.xml.XMLUtils.createDocument('annotation'); annotationRoot = annotation.getDocumentElement; %建立子節點,可以看作是第二級 folder = annotation.createElement('folder'); folder.appendChild(annotation.createTextNode(db)); annotationRoot.appendChild(folder); filename = annotation.createElement('filename'); filename.appendChild(annotation.createTextNode(imgname(idx(2)+1:end))); annotationRoot.appendChild(filename); source = annotation.createElement('source'); annotationRoot.appendChild(source); %建立該點的子節點,建立時用根節點建立,但是要依附到父節點而不是根節點,這里可以看作第三級 database = annotation.createElement('database');%用根節點建立 database.appendChild(annotation.createTextNode(db)); source.appendChild(database);%依附到父節點 annotaion_src = annotation.createElement('annotation'); annotaion_src.appendChild(annotation.createTextNode('PASCAL Annotaion Version 1.00')); source.appendChild(annotaion_src);% image = annotation.createElement('image'); image.appendChild(annotation.createTextNode('null')); source.appendChild(image);% flickrid_src = annotation.createElement('flickrid'); flickrid_src.appendChild(annotation.createTextNode('null')); source.appendChild(flickrid_src);% owner = annotation.createElement('owner'); annotationRoot.appendChild(owner); flickrid_own = annotation.createElement('flickrid'); flickrid_own.appendChild(annotation.createTextNode('null')); owner.appendChild(flickrid_own);% name = annotation.createElement('name'); name.appendChild(annotation.createTextNode('null')); owner.appendChild(name);% size = annotation.createElement('size'); annotationRoot.appendChild(size); width = annotation.createElement('width'); width.appendChild(annotation.createTextNode(num2str(imgsize(1)))); size.appendChild(width);% height = annotation.createElement('height'); height.appendChild(annotation.createTextNode(num2str(imgsize(2)))); size.appendChild(height);% depth = annotation.createElement('depth'); depth.appendChild(annotation.createTextNode(num2str(imgsize(3)))); size.appendChild(depth);% segmented = annotation.createElement('segmented'); segmented.appendChild(annotation.createTextNode('0')); annotationRoot.appendChild(segmented); for j = 1:object_num %%一張圖中可能有多個目標,需要循環建立多個object標注 object = annotation.createElement('object'); annotationRoot.appendChild(object); name_obj = annotation.createElement('name'); name_obj.appendChild(annotation.createTextNode('person')); object.appendChild(name_obj);% pose = annotation.createElement('pose'); pose.appendChild(annotation.createTextNode(obj(j).orglabel)); object.appendChild(pose);% truncated = annotation.createElement('truncated'); truncated.appendChild(annotation.createTextNode('0')); object.appendChild(truncated);% difficult = annotation.createElement('difficult'); difficult.appendChild(annotation.createTextNode('0')); object.appendChild(difficult);% bndbox = annotation.createElement('bndbox'); object.appendChild(bndbox);% xmin = annotation.createElement('xmin'); xmin.appendChild(annotation.createTextNode(num2str(obj(j).bbox(1)))); bndbox.appendChild(xmin);% ymin = annotation.createElement('ymin'); ymin.appendChild(annotation.createTextNode(num2str(obj(j).bbox(2)))); bndbox.appendChild(ymin);% xmax = annotation.createElement('xmax'); xmax.appendChild(annotation.createTextNode(num2str(obj(j).bbox(3)))); bndbox.appendChild(xmax);% ymax = annotation.createElement('ymax'); ymax.appendChild(annotation.createTextNode(num2str(obj(j).bbox(4)))); bndbox.appendChild(ymax);% end xmlwrite(path_data,annotation); %% 寫入xml文件end

生成的一個例子:

INRIA

crop001001.png

INRIA

PASCAL Annotaion Version 1.00

null

null

null

null

818

976

3

0

person

UprightPerson

0

0

261

109

511

705

person

UprightPerson

0

0

31

326

209

712

person

UprightPerson

0

0

148

179

290

641

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值