COCO 与VOC格式转化 -目标识别
目录
一.Coco标注数据介绍
1. Json detection标签文件解读
2.代码读取json文件:
二. Voc 标注数据介绍
1. xml文件解读:
2. 代码读取xml文件
三.Coco和Voc 数据相互转换
1. json ---> xml
2. xml ---> json
一.Coco标注数据介绍
1. Json detection标签文件解读
coco数据集的所有标注信息包含在一个json文件中,json文件中以字典格式保存信息. 总的json文件中有五类信息: info. licenses. images. annotations. categories .
我们主要关注后三类信息, 即图片的基本信息(name, height,width) ,每张图片的标注bbox和bbox的类别(category_id),以及总的类别id对应名称.
{ #image:图片信息 annotations:标注数据,categories:类别信息#---------------------------------------------------"info":info,"licenses": [licenses],"images":["file_name": "000000037777.jpg","height": 230,"width": 352,"id": 37777]"annotations":["image_id":289343,#[xmin,ymin,width,height]"bbox": [473.07,395.93,38.65,28.67],"category_id": 18,"id": 1768],"categories":[{"person","id": 1},{"id": 17,"name": "cat"},{"id": 18,"name": "dog"}]
}
2.代码读取json文件:
import json
annotate_path = "/home/wtj/Data/Coco2017/annotations/instances_train2017.json"
annos = json.loads(open(annotate_path).read())
print(type(annos))
print(len(annos))
print(annos.keys())
#print(annos["info"])
#print(annos["licenses"])
print(annos["images"][0]["file_name"], annos["images"][0]["width"],annos["images"][0]["height"],annos["images"][0]["id"])
print(annos["annotations"][0]["image_id"],annos["annotations"][0]["bbox"],annos["annotations"][0]["category_id"],annos["annotations"][0]["id"])
x = annos["annotations"][0]["category_id"]
print(annos["categories"][x]["name"])
二. Voc 标注数据介绍
1. xml文件解读:
如框:即标注对应的图片名称,图片的shape,以及标注的目标的类别和位置.
2. 代码读取xml文件
import xml.etree.ElementTree as ETxml_path = '/home/wtj/Data/Coco2017_pet/annotations/train2017/000000000042.xml'
tree = ET.ElementTree(file = xml_path)
root = tree.getroot() #获取跟节点
print(root)
name = root.find("filename").text
print(name)for size in root.findall("size"):width = size.find("width").textheight = size.find("height").textdepth = size.find("depth").text
print(width, height, depth)
for x in root.findall("object"):cate = x.find("name").textxmin = x.find("bndbox")[0].textymin = x.find("bndbox")[1].textxmax = x.find("bndbox")[2].textymax = x.find("bndbox")[3].textprint(name, cate, xmin, ymin, xmax, ymax)
三.Coco和Voc 数据相互转换
1. json ---> xml
未更新完,有空再加
2. xml ---> json
import logging
import xml.etree.ElementTree as ET
import os
import jsonclass xml_to_coco():def __init__(self,class_names,file_path):self.class_names = class_namesself.file_path = file_pathself.coco_dict = dict()self.coco_dict["info"] = {"description":"COCO Dataset ", "version": "1.0", "year":2022}self.coco_dict["licenses"] ={"1":"GPL","None":"good good study,day day up @-@"}self.coco_dict["images"] = []self.coco_dict["annotations"] = []self.coco_dict["categories"] = []def xml_to_json(self):xml_names = []logging.info(" loading xml ...........")for f in os.listdir(self.file_path):if not f.endswith('.xml'):continueelse:xml_names.append(f)# --->coco["categories"]for idx, category in enumerate(self.class_names):self.coco_dict["categories"].append({"supercategory":category ,"id": idx, "name":category})# print(self.coco["categories"])anno_id = 0for idx, xml_name in enumerate(xml_names):xml_path = os.path.join(self.file_path,xml_name)tree = ET.ElementTree(file=xml_path)root = tree.getroot() #获取跟节点# --->coco["images"]coco_image = dict()coco_image["file_name"] = root.find("filename").textfor size in root.findall("size"):coco_image["height"] = size.find("height").textcoco_image["width"] = size.find("width").textcoco_image["id"] = idxself.coco_dict["images"].append(coco_image)#---->coco["annotations"]for obj in root.findall("object"):coco_annotation = dict()coco_annotation["image_id"] = idxobj_name = obj.find("name").textfor cat_idx, cat_g in enumerate(self.class_names):if obj_name == cat_g:xmin = int(obj.find("bndbox")[0].text)ymin = int(obj.find("bndbox")[1].text)w = int(obj.find("bndbox")[2].text) - xminh = int(obj.find("bndbox")[3].text) - ymin coco_annotation["bbox"] =[xmin, ymin,w,h]coco_annotation["category_id"] = cat_idxcoco_annotation["id"] = anno_idanno_id = anno_id + 1self.coco_dict["annotations"].append(coco_annotation)else:continuereturn self.coco_dictif __name__ == '__main__':file_path = '/home/wtj/Data/Coco2017_pet/annotations/val2017'json_file = './val.json'class_names = ("dog","cat")a =xml_to_coco(class_names, file_path) #xml读取并存在字典中coco_d=a.xml_to_json()json_path = open(json_file, 'w')json.dump(coco_d, json_path)json_path.close()
COCO 与VOC格式转化 -目标识别相关推荐
- 多个xml文件转coco格式、coco转VOC格式(xml)
1.多个xml文件转coco格式 bug已改,已经试过可以用. 只需要修改三个地方: 将 xml_folder 变量中的路径改为你自己的 xml 文件所在文件夹路径. 将 class_name 变量中 ...
- DAGM2007数据集扩增(COCO格式以及VOC格式的灰度图数据集都可用)
文章目录 前言 COCO格式的数据集扩增 代码 AugmentMethodClass.py DatasetAugment.py VOC格式的数据集扩增 代码 AugmentMethodClass.py ...
- 使用python将COCO格式的json文件转化为VOC格式的xml文件
起因是得到一批标准的COCO数据集(json文件),自己后来又手动标了一批数据,但是标注出来的文件是VOC格式,现在想要把这些数据合并到一起,再转成标准的COCO数据集用于训练. 个人觉得json转x ...
- VOC和COCO数据集标注格式的介绍
COCO数据集标注格式的介绍: https://blog.csdn.net/m0_37970224/article/details/89211122 VOC数据集标注格式的介绍: https://bl ...
- 利用COCO API测试自己数据集训练的YOLOv3模型的mAP(VOC格式数据集)
目录 工具 前言 生成标注集的json文件 数据集准备 将voc注解格式数据集的注解转换成txt注解格式 自定义数据集的注解转换成coco的注解格式 生成结果集的json文件 安装darknet 获取 ...
- coco数据集转voc格式(附pycocotools下载方法)
1.coco数据集高速下载 我下载的是train2017.val2017和annotations_trainval2017,即coco2017的训练集(118287张图片).测试集(5000张图片)和 ...
- 100种目标检测数据集【voc格式yolo格式json格式coco格式】+YOLO系列算法源码及训练好的模型
提示:本文介绍并分享了应用于各行业.各领域非常有用的目标检测数据集(感谢您的关注+三连,数据集持续更新中-),其中绝大部分数据集作者已应用于各种实际落地项目,数据集整体质量好,标注精确,数据的多样性充 ...
- 建立自己的voc数据集_一次将自己的数据集制作成PASCAL VOC格式的惨痛经历
因为准备训练keras-yolo3,开源代码上给出了voc_annotation.py文件,只要将自己的数据格式处理成PASCAL VOC格式,那么运行voc_annotation.py就可以将自己的 ...
- 把LabelImg标注的YOLO格式标签转化为VOC格式标签 和 把VOC格式标签转化为YOLO格式标签
把LabelImg标注的YOLO格式标签转化为VOC格式标签 和 把VOC格式标签转化为YOLO格式标签 文章目录: 1 用LabelImgvoc和yolo标注标签格式说明 1.1 LabelImg标 ...
最新文章
- 推荐8个非常有逼格的实用软件,让你的办公更高效
- CF Round #426 (Div. 2) The Useless Toy 思维 水题
- SD-关于定价日期的设置
- c++类与类的泛化(Generalization)关系
- 中小企业CRM评测-八百客
- 探讨.NET Core的未来
- java.lang.NoClassDefFoundError:如何解决–第1部分
- Cisco TrustSec(理解)
- 德勤收购MSSP厂商Vigilant
- 【ios】如何成为一名ios开发
- mqtt服务器收不到设备信息,在我的终端(mosquitto)上显示来自mqtt服务器的json消息...
- ieee802.11数据radiotap介绍
- 高性能红黑二叉树实现
- 提高设计档次的8个方法
- word2vec原理_初识word2vec词向量
- CRT、CER、PEM、DER编码、JKS、KeyStore等格式证书说明及转换
- T检验、F检验和统计学意义(P值或sig值)
- ei加声调怎么加_大班拼音ei的教案
- pyhton爬虫爬取100首诗
- 牛客练习赛53E 老瞎眼 pk 小鲜肉(线段树)