资源简介
COCO格式转VOC格式
代码片段和文件信息
# -*- coding: utf-8 -*-
“““
Created on Thu Jul 11 15:34:57 2019
@author: Kevin
“““
from pycocotools.coco import COCO
import os
import shutil
from tqdm import tqdm
import skimage.io as io
import matplotlib.pyplot as plt
import cv2
from PIL import Image ImageDraw
#the path you want to save your results for coco to voc
savepath=“F:/coco2014/result/“
img_dir=savepath+‘images/‘
anno_dir=savepath+‘Annotations/‘
# datasets_list=[‘train2014‘ ‘‘]
datasets_list=[‘val2014‘]
classes_names = [‘person‘ ‘bicycle‘ ‘car‘ ‘motorcycle‘ ‘airplane‘ ‘bus‘ ‘train‘ ‘truck‘ ‘boat‘ ‘traffic light‘ ‘fire hydrant‘ ‘stop sign‘ ‘parking meter‘ ‘bench‘ ‘bird‘ ‘cat‘ ‘dog‘ ‘horse‘ ‘sheep‘ ‘cow‘ ‘elephant‘ ‘bear‘ ‘zebra‘ ‘giraffe‘ ‘backpack‘ ‘umbrella‘ ‘handbag‘ ‘tie‘ ‘suitcase‘ ‘frisbee‘ ‘skis‘ ‘snowboard‘ ‘sports ball‘ ‘kite‘ ‘baseball bat‘ ‘baseball glove‘ ‘skateboard‘ ‘surfboard‘ ‘tennis racket‘ ‘bottle‘ ‘wine glass‘ ‘cup‘ ‘fork‘ ‘knife‘ ‘spoon‘ ‘bowl‘ ‘banana‘ ‘apple‘ ‘sandwich‘ ‘orange‘ ‘broccoli‘ ‘carrot‘ ‘hot dog‘ ‘pizza‘ ‘donut‘ ‘cake‘ ‘chair‘ ‘couch‘ ‘potted plant‘ ‘bed‘ ‘dining table‘ ‘toilet‘ ‘tv‘ ‘laptop‘ ‘mouse‘ ‘remote‘ ‘keyboard‘ ‘cell phone‘ ‘microwave‘ ‘oven‘ ‘toaster‘ ‘sink‘ ‘refrigerator‘ ‘book‘ ‘clock‘ ‘vase‘ ‘scissors‘ ‘teddy bear‘ ‘hair drier‘ ‘toothbrush‘]
#Store annotations and train2014/val2014/... in this folder
dataDir= ‘F:/coco2014/‘
headstr = “““\
VOC
%s
NULL
company
%d
%d
%d
0
“““
objstr = “““\
ject>
%s
Unspecified
0
0
%d
%d
%d
%d
ject>
“““
tailstr = ‘‘‘\
‘‘‘
#if the dir is not existsmake itelse delete it
def 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 classes
def write_xml(anno_pathhead objs tail):
f = open(anno_path “w“)
f.write(head)
for obj in objs:
f.write(objstr%(obj[0]ob
评论
共有 条评论