python提取数据集的字段名称,python实现提取COCO,VOC数据集中特定的类

1.python提取COCO数据集中特定的类

pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI

提取特定的类别如下:

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="/media/huanglong/Newsmy/COCO/" #保存提取类的路径,我放在同一路径下

img_dir=savepath+'images/'

anno_dir=savepath+'Annotations/'

# datasets_list=['train2014', 'val2014']

datasets_list=['train2014']

classes_names = ['person'] #coco有80类,这里写要提取类的名字,以person为例

#Store annotations and train2014/val2014/... in this folder

dataDir= '/media/huanglong/Newsmy/COCO/' #原coco数据集

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 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_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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值