实验环境:Ubuntu16,python2

COCO数据集链接:http://cocodataset.org/#download

COCO官方API链接:https://github.com/nightrome/cocoapi

第一步 下载COCO官方API 到Ubuntu桌面 解压

得到文件目录/root/Desktop/cocostuffapi-master/

第二步 安装COCO python API

pip install pycocotools

在/root/Desktop/cocostuffapi-master/PythonAPI/目录下以次输入:

python setup.py build_ext --inplace

python setup.py build_ext install

完成API工具安装

第三步:查看COCO物体类别(即协同显著数据集组名):

COCO categories: 
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

COCO supercategories: 
outdoor food indoor appliance sports person animal vehicle furniture accessory electronic kitchen

第四步:获得某一物体类别的所有图片列表,并保存为txt:

from pycocotools.coco import COCO
import numpy as np
import pandas as pd

annFile = '/root/Desktop/annotations/instances_train2017.json'
coco=COCO(annFile)

catIds = coco.getCatIds(catNms=['boat'])

imgIds = coco.getImgIds(catIds=catIds)

f = open('boat.txt','w')

for l in imgIds:
    f.write(str(l).zfill(12) + '\n')

f.close()

第五步:读取txt文件,从原始数据集中生成新的数据集

import os
import shutil

Set_Data_Dir = 'D:/COCO/annotations/COCO Group Label/'

for group_file in os.listdir(Set_Data_Dir):

    Group_File_Dir = Set_Data_Dir + group_file
    with open(Group_File_Dir) as f:
        lines = f.read().splitlines()

    for line in lines:
        origin_image = 'D:/COCO/train2017/' + line + '.jpg'
        origin_gt = 'D:/COCO/train2017/' + line + '.png'

        new_fold = 'D:/COCO/annotations/COCO-CoSaliency/' + str(group_file[:-4])
        if not os.path.exists(new_fold):
            os.mkdir(new_fold)

        new_image = 'D:/COCO/annotations/COCO-CoSaliency/' + str(group_file[:-4]) + '/' + line + '.jpg'
        new_gt = 'D:/COCO/annotations/COCO-CoSaliency/' + str(group_file[:-4]) + '/' + line + '.png'

        shutil.copyfile(origin_image, new_image)
        shutil.copyfile(origin_gt, new_gt)

第六步 将json格式文件转为真值图

(1)进入 PythonAPI/pycocotools/cocostuffhelper.py

找到第141行:

labelMap = cocoSegmentationToSegmentationMap(coco, imgId, includeCrowd=includeCrowd)

修改为:

labelMap = cocoSegmentationToSegmentationMap(coco, imgId, checkUniquePixelLabel=False, includeCrowd=includeCrowd)

在142行后添加:

labelMap = labelMap + 91

(2)进入 PythonAPI/

make

(3)修改路径,运行

使用 PythonAPI/cocostuff/cocoSegmentationToPngDemo.py ,设置好 annPath 为你自己的 coco/annotations/instances_train2017.json 路径;然后把 Line75-Line88 注释掉

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐