当前位置: 首页> 娱乐> 明星 > 金阊seo网站优化软件_米绘设计师服务平台_seo黑帽技术_关键词优化公司

金阊seo网站优化软件_米绘设计师服务平台_seo黑帽技术_关键词优化公司

时间:2025/7/13 9:10:17来源:https://blog.csdn.net/weixin_44242403/article/details/145864706 浏览次数:0次
金阊seo网站优化软件_米绘设计师服务平台_seo黑帽技术_关键词优化公司

单个数据转voc标准数据集格式

  • 解决独立标注的数据,一键转为标准VOC格式

解决独立标注的数据,一键转为标准VOC格式

file_save = "yolo_format"# 判断是否存在保存文件,如果存在则删除文件
if os.path.exists(file_save):shutil.rmtree(file_save)
# 新建文件
os.makedirs(file_save)# 原始数据集标注文件路径
file_annotation = "Annotations"
file_images = "JPEGImages"
# 原始数据集标签
classes = []
traintxt = []
valtxt = []
train = {'images':[],'type':"instances",'annotations':[],'categories':[]}
val = {'images':[],'type':"instances",'annotations':[],'categories':[]}
# 遍历所有标签文件
for i in os.listdir(file_annotation):if "xml" not in i:continue# 获取该文件最后一个/除后缀名以外的内容temp_cat = {"supercategory":"none","id":0,"name":"080002"}temp_ann = {"area":1164024,"iscrowd":0,"category_id":12,"ignore":0,"image_id":0,"id":0, "bbox":[]}temp_img = {"file_name":"DZB_F_1.jpg","height":1386,"width":3031,"id":0}p = Path(i)name = p.stemstat = random.random()# 解析xml文件tree = ET.parse(os.path.join(file_annotation, i))root = tree.getroot()  # 获取根节点# 遍历子节点sizefor size in root.iter('size'):width = int(size.find('width').text)     # 获取图片的宽度height = int(size.find('height').text)   # 获取图片的高度temp_img["file_name"] = i.replace(".xml",".jpg")temp_img["height"] = heighttemp_img["width"] = widthif stat>0.1:temp_img["id"] = len(train["images"])train["images"].append(copy.deepcopy(temp_img))traintxt.append(temp_img["file_name"].replace(".jpg",""))else:temp_img["id"] = len(val["images"])val["images"].append(copy.deepcopy(temp_img))valtxt.append(temp_img["file_name"].replace(".jpg",""))# 遍历子节点objectfor obj in root.iter('object'):cls = obj.find('name').text  # 获取标签名# 获取当前类别的idif cls not in classes:classes.append(cls)# 添加类型temp_cat["id"] = classes.index(cls)temp_cat["name"] = cls# 同时添加train["categories"].append(copy.deepcopy(temp_cat))val["categories"].append(copy.deepcopy(temp_cat))    cls_id = classes.index(cls)# 获取VOC格式标签的坐标信息xmlbox = obj.find('bndbox')xmin = int(xmlbox.find('xmin').text)ymin = int(xmlbox.find('ymin').text)xmax = int(xmlbox.find('xmax').text)ymax = int(xmlbox.find('ymax').text)#写入jsonif stat>0.1:#temp_ann = {"area":1164024,"iscrowd":0,"category_id":12,"ignore":0,"image_id":0,"id":0, "bbox":[]}temp_ann["area"] = (ymax - ymin) * (xmax - xmin)temp_ann["category_id"] = cls_idtemp_ann["image_id"] = temp_img["id"]temp_ann["id"] = len(train["annotations"])temp_ann["bbox"] = [xmin, ymin, xmax, ymax]train["annotations"].append(copy.deepcopy(temp_ann))else:#temp_ann = {"area":1164024,"iscrowd":0,"category_id":12,"ignore":0,"image_id":0,"id":0, "bbox":[]}temp_ann["area"] = (ymax - ymin) * (xmax - xmin)temp_ann["category_id"] = cls_idtemp_ann["image_id"] = temp_img["id"]temp_ann["id"] = len(val["annotations"])temp_ann["bbox"] = [xmin, ymin, xmax, ymax]val["annotations"].append(copy.deepcopy(temp_ann))# 新建和标签文件同名的txt文件
with open("label.txt", "w") as file:classes = "\n".join(classes)file.write(classes)
with open("train.txt", "w") as file:classes = "\n".join(traintxt)file.write(classes)
with open("val.txt", "w") as file:classes = "\n".join(valtxt)file.write(classes)
#保存json
import json
with open("train.json", "w") as file:file.write(json.dumps(train))
with open("val.json", "w") as file:file.write(json.dumps(val))
关键字:金阊seo网站优化软件_米绘设计师服务平台_seo黑帽技术_关键词优化公司

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: