我正在使用MS-COCO数据集,我想提取与背包(类别ID:27)和笔记本电脑(类别ID:73)类别对应的图像的边界框和标签,并将它们存储到不同的文本文件中,以便以后训练基于神经网络的模型。在
我已经提取了与上述两个类别相对应的图像,并在一个单独的文件夹中创建了空的注释文件,我希望在其中存储注释和标签(注释文件的格式类似于:labelx y wh,其中w和h表示检测到的类别)。我建立在dataset/cocoapi/tree/master/PythonAPI/pycocotools" rel="nofollow noreferrer">COCO-API之上(可可豆精确地说)提取图像并创建空文本注释文件。在
下面是我在coco.py
之上编写的主要函数:
if __name__ == "__main__":
littleCo = COCO('/home/r.bohare/coco_data/annotations/instances_train2014.json')
#id_laptop = littleCo.getCatIds('laptop')
"""Extracting image ids corresponding to backpack and laptop images."""
bag_img_ids = littleCo.getImgIds(catIds=[27])
laptop_img_ids = littleCo.getImgIds(catIds=[73])
#print "IDs of bag images:", bag_img_ids
#print "IDs of laptop imgs:", laptop_img_ids
"""Extracting annotation ids corresponding to backpack and laptop images."""
bag_ann_ids = littleCo.getAnnIds(catIds=[27])
laptop_ann_ids = littleCo.getAnnIds(catIds=[73])
#print "Annotation IDs of bags:", bag_ann_ids
#print "Annotation IDs of laptops:", laptop_ann_ids
"""Extracting image names corresponding to bag and laptop categories."""
bag_imgs = littleCo.loadImgs(ids=bag_img_ids)
laptop_imgs = littleCo.loadImgs(ids=laptop_img_ids)
#print "Bag images:", bag_imgs
#print "Laptop images:", laptop_imgs
bag_img_names = [image['file_name'] for image in bag_imgs]
laptop_img_names = [image['file_name'] for image in laptop_imgs]
print "Bag Images:", len(bag_img_names), bag_img_names[:5]
print "Laptop Images:", len(laptop_img_names), laptop_img_names[:5]
"""Extracting annotations corresponding to bag and laptop images."""
bag_ann = littleCo.loadAnns(ids=bag_ann_ids)
laptop_ann = littleCo.loadAnns(ids=laptop_ann_ids)
bag_bbox = [ann['bbox'] for ann in bag_ann]
laptop_bbox = [ann['bbox'] for ann in laptop_ann]
print "Bags' bounding boxes:", len(bag_ann), bag_bbox[:5]
print "Laptops' bounding boxes:", len(laptop_bbox), laptop_bbox[:5]
"""Saving files corresponding to bags and laptop category in a directory."""
import shutil
#path_to_imgs = "/export/work/Data Pool/coco_data/train2014/"
#path_to_subset_imgs = "/export/work/Data Pool/coco_subset_data/"
path_to_ann = "/export/work/Data Pool/coco_subset_data/annotations/"
dirs_list = [("/export/work/Data Pool/coco_data/train2014/", "/export/work/Data Pool/coco_subset_data/")]
for source_folder, destination_folder in dirs_list:
for img in bag_img_names:
shutil.copy(source_folder + img, destination_folder + img)
print "Bag images copied!"
for img in laptop_img_names:
shutil.copy(source_folder + img, destination_folder + img)
print "Laptop images copied!"
"""Creating empty files for annotation."""
for f in os.listdir("/export/work/Data Pool/coco_subset_data/images/"):
if f.endswith('.jpg'):
open(os.path.join(path_to_ann, f.replace('.jpg', '.txt')), 'w+').close()
print "Done creating empty annotation files."
我在这里只提供了main函数,因为代码的其余部分是COCO-API中的coco.py文件。在
我调试了代码,发现有不同的数据结构:
cats
,一个字典,它将类别id映射到它们的超类别和类别名称(标签)。在imgToAnns
,这也是一个字典,它将每个图像ID映射到它的分割背景真实性、边界框背景真实性、类别ID等等,我想我需要用这本词典把我在bag\u img永names和笔记本电脑名列表中的图像名称映射到它们的标签和边界框中,但我无法正确地思考如何访问这本词典(在可可豆直接返回)。在imgs
,另一个字典,提供有关所有图像的元信息,如图像名称、图像url、捕获日期等最后,我知道这是一个非常具体的问题。请随时告诉我这是否属于stackoverflow以外的社区(stats.stackexchange.com网站例如),我将删除它。还有,我可能漏掉了一些重要的信息。如果我能想到,或者有人要求,我会提供的。在
我只是一个Python的初学者,所以如果我错过了一些显而易见的东西,请原谅我。在
任何帮助都是非常感谢的。谢谢您。在
两年过去了。现在可可豆你已经可以做你正在做的事情了,通过在末尾添加一些函数来将注释(转换为RLE格式)映射到图像。看看这个cocoapi.
相关问题 更多 >
编程相关推荐