COCO(Common Objects in Context)数据集是CV计算机视觉领域中,常用的数据集,其中的标注信息包括:类别、检测框、分割信息等。
下载页面:
其中,Train、Val、Test都是图片信息,标注信息位于Annotations中。
在annotations文件夹中:
- captions是描述
- instances是检测框和分割
- person_keypoints是关键点。
分析:instances_val2017.json,约20M左右
五个类别
在JSON文件中,含有5个字段:
['info', 'licenses', 'images', 'annotations', 'categories']
信息,许可证,图片,标注,类别汇总
复制代码
images图片数是5000,annotations标注数是36781,以检测框bbox为一条标注信息,所以标注数大于图片书。
info和licenses没有什么意义。
images是图片信息,包括:
- 图片名:
file_name
; - 下载地址:
coco_url
和flickr_url
; - 图片长和宽:height和width;
- 图片ID:id,与标注相关;
如下:
{'license': 4, 'file_name': '000000397133.jpg', 'coco_url': 'http://images.cocodataset.org/val2017/000000397133.jpg', 'height': 427, 'width': 640, 'date_captured': '2013-11-14 17:02:52', 'flickr_url': 'http://farm7.staticflickr.com/6116/6255196340_da26cf2c9e_z.jpg', 'id': 397133}
复制代码
categories是类别信息,COCO的80类,包括:
- 父类别:person;
- 类别ID:id,与标注相关;
- 类别含义:name;
[Info] 类别数: 80
[Info] 类别: {'supercategory': 'person', 'id': 1, 'name': 'person'}
复制代码
annotations是标注信息,包括:
- 分割:segmentation;
- 分割的类别:iscrowd,是否拥挤;
- 面积:area;
- 图片ID:image_id,与images中的id相对应;
- 类别ID:category_id,与categories中的id相对应;
- 检测框:bbox;
- 标注的ID:id;
接着,将会重点分析annotation的内容;
[Info] 标注数: 36781
[Info] 标注: {'segmentation': [[510.66, 423.01, 511.72, 420.03, 510.45, 416.0, 510.34, 413.02, 510.77, 410.26, 510.77, 407.5, 510.34, 405.16, 511.51, 402.83, 511.41, 400.49, 510.24, 398.16, 509.39, 397.31, 504.61, 399.22, 502.17, 399.64, 500.89, 401.66, 500.47, 402.08, 499.09, 401.87, 495.79, 401.98, 490.59, 401.77, 488.79, 401.77, 485.39, 398.58, 483.9, 397.31, 481.56, 396.35, 478.48, 395.93, 476.68, 396.03, 475.4, 396.77, 473.92, 398.79, 473.28, 399.96, 473.49, 401.87, 474.56, 403.47, 473.07, 405.59, 473.39, 407.71, 476.68, 409.41, 479.23, 409.73, 481.56, 410.69, 480.4, 411.85, 481.35, 414.93, 479.86, 418.65, 477.32, 420.03, 476.04, 422.58, 479.02, 422.58, 480.29, 423.01, 483.79, 419.93, 486.66, 416.21, 490.06, 415.57, 492.18, 416.85, 491.65, 420.24, 492.82, 422.9, 493.56, 424.39, 496.43, 424.6, 498.02, 423.01, 498.13, 421.31, 497.07, 420.03, 497.07, 415.15, 496.33, 414.51, 501.1, 411.96, 502.06, 411.32, 503.02, 415.04, 503.33, 418.12, 501.1, 420.24, 498.98, 421.63, 500.47, 424.39, 505.03, 423.32, 506.2, 421.31, 507.69, 419.5, 506.31, 423.32, 510.03, 423.01, 510.45, 423.01]], 'area': 702.1057499999998, 'iscrowd': 0, 'image_id': 289343, 'bbox': [473.07, 395.93, 38.65, 28.67], 'category_id': 18, 'id': 1768}
复制代码
源码:
def load_json():
val_file = os.path.join(ROOT_DIR, 'datasets', 'annotations', 'instances_val2017.json')
data_line = read_file_utf8(val_file)[0]
coco_dict = json.loads(data_line)
print('Keys: {}'.format(coco_dict.keys()))
info = coco_dict['info']
licenses = coco_dict['licenses']
images = coco_dict['images']
annotations = coco_dict['annotations']
categories = coco_dict['categories']
print('-' * 50)
print('[Info] info: {}'.format(info)) # 信息
print('-' * 50)
print('[Info] licenses: {}'.format(licenses)) # 8个licenses
print('-' * 50)
print('[Info] 图片数: {}'.format(len(images))) # 图片数
print('[Info] 图片: {}'.format(images[0])) # 图片数
print('-' * 50)
print('[Info] 标注数: {}'.format(len(annotations))) # 标注
print('[Info] 标注: {}'.format(annotations[0])) # 标注
print('-' * 50)
print('[Info] 类别数: {}'.format(len(categories))) # 类别
print('[Info] 类别: {}'.format(categories[0])) # 类别
return images, annotations
复制代码
分割
参考,COCOAPI的demo。
具体含义:
当iscrowd为0时,表示为多边形:
数据:
'segmentation': [[510.66, 423.01, 511.72, 420.03, 510.45, 416.0, 510.34, 413.02, 510.77, 410.26, ...]]
复制代码
两个值一组的多边形,注意图片的坐标系是左上角为(0,0),所以matplot图片与原图颠倒;
源码:
def draw_polygon(seg):
print('[Info] 数据格式: {}'.format(seg))
gemfield_polygons = seg
polygons = []
fig, ax = plt.subplots()
gemfield_polygon = gemfield_polygons[0]
max_value = max(gemfield_polygon) * 1.3
gemfield_polygon = [i * 1.0 / max_value for i in gemfield_polygon]
poly = np.array(gemfield_polygon).reshape((int(len(gemfield_polygon) / 2), 2))
polygons.append(Polygon(poly, True)) # 多边形
p = PatchCollection(polygons, cmap=matplotlib.cm.jet, alpha=0.4)
colors = 100 * np.random.rand(1)
p.set_array(np.array(colors))
ax.add_collection(p)
plt.show()
复制代码
当iscrowd为1时,表示为像素图像,以列依次排列,即先填充列。
数据:
{'counts': [272, 2, 4, 4, 4, 4, 2, 9, 1, 2, ...], 'size': [240, 320]}
复制代码
以列为顺序,第1个是背景,第2个是前景,依次交替;
源码:
def draw_rle(seg):
print('[Info] 数据格式: {}'.format(seg))
rle = seg['counts']
h, w = seg['size']
M = np.zeros(h * w)
N = len(rle)
n = 0
val = 1
for pos in range(N):
val = not val
num = rle[pos]
for c in range(num):
M[n] = val
n += 1
gemfield = M.reshape(([h, w]), order='F')
plt.imshow(gemfield)
plt.show()
复制代码
OK, that's all!