COCO数据集的标签整理使用
网上下载coco2017数据集,对目标检测来说,最主要是annotations, train2017, val2017三个文件夹
初次使用需要安装coco API接口,使用如下命令即可安装
pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI
目标检测,标签数据即用矩形框来表示,一般用左上角坐标和宽高来表示,常见是(x,y,w,h),训练时需要将ground truth box整理成xmin, ymin, xmax, ymax, label_idx或者[x_center,y_center,w,h,label_idx](yolov3格式),比较方便时可将每张图像的label对应到一个txt文件(最好以图像文件名命名,方便对应查找),文件中每行一个ground truth标签,下次其他模型训练时直接读取图像和对应的txt文件即可。
可使用如下代码获取原始下载的每张图像上ground truth的label信息,在此基础上你可以构建模型的数据处理部分。
import os.path as osp
import cv2
import numpy as np
import random
from pycocotools.coco import COCO
def get_label_map(label_file):
label_map = {
}
labels = open(label_file, 'r')
for line in labels:
ids = line.split(',')
label_map[int(ids[0])] = int(</