博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
目标检测-数据准备系列(二)--coco2voc
阅读量:2051 次
发布时间:2019-04-28

本文共 4868 字,大约阅读时间需要 16 分钟。

直接贴源码:

# @TIME  :2019/7/21 15:34# @File  :coco2xml.py"""从coco instance.json生成voc--xml文件""""""首先得下载编译cocoapipip install cythongit clone https://github.com/cocodataset/cocoapi.gitcd coco/PythonAPImake"""import sysbag_path = "../cocoapi-master/PythonAPI/"if not bag_path in sys.path:    sys.path.append(bag_path)from pycocotools.coco import COCOimport osimport shutilfrom tqdm import tqdmimport skimage.io as ioimport matplotlib.pyplot as pltimport cv2from PIL import Image, ImageDraw# the path you want to save your results for coco to vocsavepath = "../result/"#保存生成文件路径img_dir = savepath + 'images/'#保存生成jpg文件路径anno_dir = savepath + 'Annotations/'#保存生成xml文件路径# datasets_list=['train2014', 'val2014']datasets_list = ['train2017']#coco数据集里面图片# Store annotations and train2014/val2014/... in this folderdataDir = '../../coco/coco2017/'#coco数据集整体文件,里面包含annotations和图片文件夹#path = os.path.abspath(dataDir)#print(path)headstr = """\
VOC
%s
My Database
COCO
flickr
NULL
NULL
company
%d
%d
%d
0
"""objstr = """\
%s
Unspecified
0
0
%d
%d
%d
%d
"""tailstr = '''\
'''# if the dir is not exists,make it,else delete itdef mkr(path): if os.path.exists(path): shutil.rmtree(path) os.mkdir(path) else: os.mkdir(path)mkr(img_dir)mkr(anno_dir)def id2name(coco): classes = dict() for cls in coco.dataset['categories']: classes[cls['id']] = cls['name'] return classesdef write_xml(anno_path, head, objs, tail): f = open(anno_path, "w") f.write(head) for obj in objs: f.write(objstr % (obj[0], obj[1], obj[2], obj[3], obj[4])) f.write(tail)def save_annotations_and_imgs(coco, dataset, filename, objs): # eg:COCO_train2014_000000196610.jpg-->COCO_train2014_000000196610.xml anno_path = anno_dir + filename[:-3] + 'xml' img_path = dataDir + dataset + '/' + filename #print(img_path) dst_imgpath = img_dir + filename img = cv2.imread(img_path) if (img.shape[2] == 1): #print(filename + " not a RGB image") return shutil.copy(img_path, dst_imgpath) head = headstr % (filename, img.shape[1], img.shape[0], img.shape[2]) tail = tailstr write_xml(anno_path, head, objs, tail)def showimg(coco, dataset, img, classes, cls_id, show=True): global dataDir I = Image.open('%s/%s/%s' % (dataDir, dataset, img['file_name'])) # 通过id,得到注释的信息 annIds = coco.getAnnIds(imgIds=img['id'], catIds=cls_id, iscrowd=None) # print(annIds) anns = coco.loadAnns(annIds) # print(anns) # coco.showAnns(anns) objs = [] for ann in anns: class_name = classes[ann['category_id']] if 'bbox' in ann: bbox = ann['bbox'] xmin = int(bbox[0]) ymin = int(bbox[1]) xmax = int(bbox[2] + bbox[0]) ymax = int(bbox[3] + bbox[1]) obj = [class_name, xmin, ymin, xmax, ymax] objs.append(obj) draw = ImageDraw.Draw(I) draw.rectangle([xmin, ymin, xmax, ymax]) if show: plt.figure() plt.axis('off') plt.imshow(I) plt.show() return objsfor dataset in datasets_list: # ./COCO/annotations/instances_train2014.json annFile = '{}/annotations/instances_{}.json'.format(dataDir, dataset) # COCO API for initializing annotated data coco = COCO(annFile) ''' COCO 对象创建完毕后会输出如下信息: loading annotations into memory... Done (t=0.81s) creating index... index created! 至此, json 脚本解析完毕, 并且将图片和对应的标注数据关联起来. ''' # show all classes in coco classes = id2name(coco) classes_names = [] for key, value in classes.items(): classes_names.append(value) classes_ids = coco.getCatIds(catNms=classes_names) img_ids_totoal =[] for cls in classes_names: # Get ID number of this class cls_id = coco.getCatIds(catNms=[cls]) img_ids = coco.getImgIds(catIds=cls_id) for img_id in img_ids: img_ids_totoal.append(img_id) print(len(img_ids_totoal)) tem=set(img_ids_totoal) temp = list(tem) temp.sort() print(len(tem)) print(len(temp)) for imgId in tqdm(temp): img = coco.loadImgs(imgId)[0] filename = img['file_name'] # print(filename) objs = showimg(coco, dataset, img, classes, classes_ids, show=False) # print(objs) save_annotations_and_imgs(coco, dataset, filename, objs)

参考:

 

转载地址:http://wrzlf.baihongyu.com/

你可能感兴趣的文章
Leetcode C++ 剑指 Offer 09. 用两个栈实现队列
查看>>
Leetcode C++《每日一题》20200707 112. 路径总和
查看>>
云原生 第十一章 应用健康
查看>>
Leetcode C++ 《第202场周赛》
查看>>
云原生 第十二章 可观测性:监控与日志
查看>>
Leetcode C++ 《第203场周赛》
查看>>
云原生 第十三章 Kubernetes网络概念及策略控制
查看>>
《redis设计与实现》 第一部分:数据结构与对象 || 读书笔记
查看>>
《redis设计与实现》 第二部分(第9-11章):单机数据库的实现
查看>>
算法工程师 面经2019年5月
查看>>
搜索架构师 一面面经2019年6月
查看>>
稻草人手记
查看>>
第一次kaggle比赛 回顾篇
查看>>
leetcode 50. Pow(x, n)
查看>>
leetcode 130. Surrounded Regions
查看>>
【托业】【全真题库】TEST2-语法题
查看>>
博客文格式优化
查看>>
【托业】【新托业全真模拟】疑难语法题知识点总结(01~05)
查看>>
【SQL】group by 和order by 的区别。
查看>>
【F12】谷歌浏览器--前台效果可以在不访问服务器的前提下直接改样式看效果是否是预期值。...
查看>>