coco数据集不仅可以用于做目标检测还可以用于进行语义分割,这里主要讲解基于yolov3进行目标检测的数据准备。由于coco数据集的标签信息是以.json的格式存储的,因此有必要转换为具体的格式来进行模型训练与评估。具体的实现方式如下:
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 2 11:41:44 2019
@author: Administrator
"""
#from __feature__ import print_function
import os,sys,zipfile
import json
import glob
def convert(size,box):
dw = 1. / (size[0])
dh = 1. / (size[1])
x = box[0] + box[2] / 2.0
y = box[1] + box[3] / 2.0
w = box[2]
h = box[3]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x,y,w,h)
def get_coco_data(path):
data = json.load(open(path,'r'))
imgs = data['images']
annotations = data['annotations']
print(len(imgs))
for img in imgs:
filename = img['file_name']
img_w = img['width']