常用数据集简介
# COCO格式数据集
# 01、COCO 格式数据集标注解析
分为四部分信息
"info": {
"description": "CowboySuit",
"url": "http://github.com/dmlc/gluon-cv",
"version": "1.0",
"year": 2021,
"contributor": "GluonCV/AutoGluon",
"date_created": "2021/07/01"
},
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
- info 主要描述数据的元信息,一般就是标注数据的版权和来源之类的
"images": [
{
"id": 11684018977583795263,
"file_name": "a225f95b03ce0c3f.jpg",
"neg_category_ids": [
120
],
"pos_category_ids": [
69,
216,
308,
433
],
"width": 1024,
"height": 683,
"source": "OpenImages"
},
...
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- images 是一个列表,列表里的每个实例都描述一张图像,图像比较重要的属性值如下
- id
- file_name
- height
- weight
"annotations":[
{
"id": 9502719,
"image_id": 11684018977583795263,
"freebase_id": "/m/032b3c",
"category_id": 588,
"iscrowd": false,
"bbox": [
524.16,
255.0,
365.44,
411.98
],
"area": 150553.52
}
..
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- annotations 则是对应图像的标注,标注比较重要的属性值如下
- image_id
- category_id
- bbox: [x, y, width, height]
- bbox 的格式是目标框左上角的坐标以及该框的宽和高
"categories": [
{
"id": 87,
"name": "belt",
"freebase_id": "/m/0176mf"
},
...
]
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
- categories 则是所有的类别,比较重要的属性值如下
- id
- name
# 02、COCO 格式数据集标注可视化
# 2.1 目标框的标注可视化
# import cv2
import json
import os
import mmcv
import numpy as np
from pycocotools.coco import COCO
def visualization_bbox(num_image, json_path, img_path): # 需要画的第num副图片, 对应的json路径和图片路径
with open(json_path) as annos:
annotation_json = json.load(annos)
print('the annotation_json num_key is:', len(annotation_json)) # 统计json文件的关键字长度
print('the annotation_json key is:', annotation_json.keys()) # 读出json文件的关键字
print('the annotation_json num_images is:', len(annotation_json['images'])) # json文件中包含的图片数量
label_id_cls_dict = dict()
for i in range(len(annotation_json['categories'][::])):
id = annotation_json['categories'][i]['id']
name = annotation_json['categories'][i]['name']
label_id_cls_dict[id] = name
print('the categories id to name dict is', label_id_cls_dict)
id = annotation_json['images'][num_image - 1]['id'] # 读取图片id
image_name = annotation_json['images'][num_image - 1]['file_name'] # 读取图片名
image_path = os.path.join(img_path, str(image_name).zfill(5)) # 拼接图像路径
image = mmcv.imread(image_path)
bboxes = []
labels = []
for i in range(len(annotation_json['annotations'][::])):
if annotation_json['annotations'][i]['image_id'] == id:
x, y, w, h = annotation_json['annotations'][i]['bbox'] # 读取边框
label_id = annotation_json['annotations'][i]['category_id']
label = label_id_cls_dict[label_id]
# 将左上和右下的坐标加入列表
bboxes.append([int(x), int(y), int(x + w), int(y + h)])
labels.append(label)
print('The num_bbox of the display image is:', len(bboxes))
mmcv.imshow_det_bboxes(image, np.array(bboxes), np.array(labels))
def visualization_bbox_coco_api(num_image, json_path, img_path):
coco = COCO(json_path)
img = list(coco.imgs.items())[num_image][1]
image_id = img['id'] # 读取图像id
image_name = img['file_name'] # 读取图像名字
image = mmcv.imread(os.path.join(img_path, image_name)) # 读取图像
annIds = coco.getAnnIds(imgIds=image_id)
anns = coco.loadAnns(annIds)
bboxes = []
labels = []
for i in range(len(annIds)):
x, y, w, h = anns[i]['bbox'] # 读取边框
label_id = anns[i]['category_id']
label = coco.cats[label_id]['name']
bboxes.append([int(x), int(y), int(x + w), int(y + h)])
labels.append(label)
print('The num_bbox of the display image is:', len(bboxes))
mmcv.imshow_det_bboxes(image, np.array(bboxes), np.array(labels))
if __name__ == "__main__":
train_json = '/home/muyun99/competition/detection/data/kaggle_cowboyoutfits/annotations.json'
train_path = '/home/muyun99/competition/detection/data/kaggle_cowboyoutfits/images'
for i in range(10):
visualization_bbox(i, train_json, train_path)
visualization_bbox_coco_api(i, train_json, train_path)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
上次更新: 2021/08/08, 21:56:44
- 02
- README 美化05-20
- 03
- 常见 Tricks 代码片段05-12