COCO

实验环境: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 注释掉

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值