外贸企业网站制作,股票交易网站开发,单页面网站源码,橙色网站后台模板背景#xff1a;
通常搜集完数据图片后#xff0c;我们会用labelimg进行图片标注#xff0c;比较高版本的labelimg支持的标注格式有三种#xff0c;PascalVOC、YOLO、CreateML#xff0c;标注的时候可以根据自己的算法模型数据集需求选择相应的格式#xff0c;当然…背景
通常搜集完数据图片后我们会用labelimg进行图片标注比较高版本的labelimg支持的标注格式有三种PascalVOC、YOLO、CreateML标注的时候可以根据自己的算法模型数据集需求选择相应的格式当然也可以三种方式同时标注不过会耗时间一些。有时候我们仅仅标注了一种格式转而实际算法建模的时候可能需要对相应的格式进行转换。
xml转json:
默认选用PascalVOC方式的话标注的数据集格式为XML实例如下(2.xml)
annotationfolderDesktop/folderfilenameng2.png/filenamepathC:\Users\Xiao\Desktop\ng2.png/pathsourcedatabaseUnknown/database/sourcesizewidth1892/widthheight851/heightdepth3/depth/sizesegmented0/segmentedobjectname1/nameposeUnspecified/posetruncated0/truncateddifficult0/difficultbndboxxmin60/xminymin381/yminxmax354/xmaxymax583/ymax/bndbox/object
/annotation 将该文档转换为json格式并保存的代码如下
import xml.etree.ElementTree as ET
import jsondef xml_to_json(xml_file, json_file):tree ET.parse(xml_file)root tree.getroot()data []for obj in root.findall(object):obj_data {}obj_data[name] obj.find(name).textobj_data[bbox] {xmin: int(obj.find(bndbox/xmin).text),ymin: int(obj.find(bndbox/ymin).text),xmax: int(obj.find(bndbox/xmax).text),ymax: int(obj.find(bndbox/ymax).text)}data.append(obj_data)json_data {filename: root.find(filename).text,size: {width: int(root.find(size/width).text),height: int(root.find(size/height).text),depth: int(root.find(size/depth).text)},objects: data}with open(json_file, w) as f:json.dump(json_data, f, indent4)# Example usage
xml_file rC:\Users\Xiao\Desktop\tools\2.xml
json_file rC:\Users\Xiao\Desktop\tools\2.json
xml_to_json(xml_file, json_file)
print(数据转换完成)
实际使用的时候需要适当修改一下文档路径才可以。
转换完之后的json内容如下(2.json)
{filename: ng2.png,size: {width: 1892,height: 851,depth: 3},objects: [{name: 1,bbox: {xmin: 60,ymin: 381,xmax: 354,ymax: 583}}]
}