从Faster RCNN开始学python 篇外卷pycocotools(二)

在Faster RCNN中有使用一些额外的程序,类等等,为了使文章整体内容不过于分散,这里作为篇外卷学习。
文件地址:\py-faster-rcnn\lib\pycocotools
在这篇文章中讲解pycocotools中的coco.py,在所有代码中有两个coco.py代码
整个代码的主要作用就是数据的读取,下载,信息的分类。

先导入一些标准库和自定义的程序,其中mask在上篇文章中已经解析过了。

import json
import datetime
import time
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
import numpy as np
from skimage.draw import polygon
import urllib
import copy
import itertools
import mask
import os

定义类:COCO,先对类进行初始化,在这个初始化过程中,如果annotation_file有输入,那么就读取相应的数据集(json.load(open(annotation_file, ‘r’)) ),并且赋值给self.dataset同时调用子函数createIndex(self)

class COCO:
    def __init__(self, annotation_file=None):
        # load dataset
        self.dataset = {}
        self.anns = []
        self.imgToAnns = {}
        self.catToImgs = {}
        self.imgs = {}
        self.cats = {}
        if not annotation_file == None:
            print 'loading annotations into memory...'
            tic = time.time()
            dataset = json.load(open(annotation_file, 'r'))
            print 'Done (t=%0.2fs)'%(time.time()- tic)
            self.dataset = dataset
            self.createIndex()

定义子函数createIndex(self),

    def createIndex(self):
        # create index
        print 'creating index...'
        anns = {}
        imgToAnns = {}
        catToImgs = {}
        cats = {}
        imgs = {}
        if 'annotations' in self.dataset:
            imgToAnns = {ann['image_id']: [] for ann in self.dataset['annotations']}
            anns =      {ann['id']:       [] for ann in self.dataset['annotations']}
            for ann in self.dataset['annotations']:
  
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值